Note that there are some explanatory texts on larger screens.

plurals
  1. PODisabling/Disallowing items in ComboBox to be selected depending on selection ( C# )
    text
    copied!<p>I have two comboBoxes, one that lists the 7 days of the week and the other with a set of times (7 different times, each as a string).</p> <p>The problem i'm having is i want to be able to stop the user from being able to select certain items depending on the day/time selected. </p> <p>An example being that on a Monday the 7-9am comboBox option should not be available due to it being "Pre-Booked", if that makes sense.</p> <p>The days and times are used in relation to a 7x7 array, each index in the combo boxes relating to an element in the array so depending on the selected index in the combo boxes i can use the correct array element.</p> <p>What would be the best way to go about it? I've tried a series of if statements but it got to a point where it would just slip through them and continue to process data when its not meant to.</p> <p>EDIT:</p> <p>Added example attempt for jmatthews3865</p> <pre><code>private void buttonOK_Click(object sender, EventArgs e) { if (comboBoxTime.SelectedIndex != 0 &amp;&amp; comboBoxDay.SelectedIndex != 5 | comboBoxDay.SelectedIndex != 6) { if (comboBoxTime.SelectedIndex != 1 | comboBoxTime.SelectedIndex != 2 | comboBoxTime.SelectedIndex != 3 &amp;&amp; comboBoxDay.SelectedIndex != 0 | comboBoxDay.SelectedIndex != 1 | comboBoxDay.SelectedIndex != 2 | comboBoxDay.SelectedIndex != 3 | comboBoxDay.SelectedIndex != 4) { if (comboBoxTime.SelectedIndex != 5 | comboBoxTime.SelectedIndex != 6 &amp;&amp; comboBoxDay.SelectedIndex != 5 | comboBoxDay.SelectedIndex != 6) { lock (this) { int date = this.comboBoxDay.SelectedIndex; int time = this.comboBoxTime.SelectedIndex; if (IsUser) { string fName = textBoxFName.Text; string lName = textBoxLName.Text; string pCode = textBoxPCode.Text; AddUserBooking(date, time, fName, lName, pCode); } else { int mNo = int.Parse(textBoxMNo.Text); AddMemberBooking(date, time, mNo); } } CloseBookingForm(); } } } else { MessageBox.Show("Select a valid Date/Time and try again"); } } </code></pre> <p>It seems to work fine on the first if statement block, fails past that though.</p> <p>If the selected day is Saturday/Sunday ([5],[6]) then the times [0] and [5],[6] are unavailable. From Monday to Friday the times including [1] to [3] are unavailable.</p> <p>If it wasn't for my extremely short deadline and the lecturer who can't make a sensible assignment then i'd gladly refactor all of it.</p> <p>EDIT2:</p> <p>I've managed to implement a system that seems to be working fairly well. It's based on Steven Adams solution which essentially uses an if statament in a boolean method. I call the method before processing the booking and if it returns true then the booking can be made, otherwise a message box is shown with the appropiate error.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload