Reputation: 9966
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).
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.
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.
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.
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.
EDIT:
Added example attempt for jmatthews3865
private void buttonOK_Click(object sender, EventArgs e)
{
if (comboBoxTime.SelectedIndex != 0 && comboBoxDay.SelectedIndex != 5 | comboBoxDay.SelectedIndex != 6)
{
if (comboBoxTime.SelectedIndex != 1 | comboBoxTime.SelectedIndex != 2 | comboBoxTime.SelectedIndex != 3
&& comboBoxDay.SelectedIndex != 0 | comboBoxDay.SelectedIndex != 1 | comboBoxDay.SelectedIndex != 2
| comboBoxDay.SelectedIndex != 3 | comboBoxDay.SelectedIndex != 4)
{
if (comboBoxTime.SelectedIndex != 5 | comboBoxTime.SelectedIndex != 6 && 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");
}
}
It seems to work fine on the first if statement block, fails past that though.
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.
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.
EDIT2:
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.
Upvotes: 1
Views: 7842
Reputation: 702
i would second cdonners comment. having a combobox with disabled options could be a pretty confusing situation for users. isn't there a way to filter the options based on whether the time slots are already booked?
some sort of custom control which displays time ranges would be ideal form something like this. this way you could load existing events so that users would be able to see why they aren't able to select a specific time range. i understand that this may go a little further than what you want.
something like this is what i'm thinking... (assuming asp.net, not winforms)
but if you're intent on using the combobox, i would go with cdonners suggestion, or you could handle the OnSelectedIndexChanged event in order to validate that the selected choice is available and notify the user if not. sounds like this might be what you're trying to do now. could you post the code that isn't working?
edit:
it looks like you're making this much more complicated that it has to be. but since it seems like this is homework (correct me if i'm wrong) i don't want to just write out a solution. also, you should tag your question as homework if appropriate.
try setting up the available values of each combo into two separate enums and process based on that rather than hard coding the vlaues.
Upvotes: 2
Reputation: 37668
The most direct way to success is probably populating the time list in an OnSelectedIndexChanged event handler of the day list. You can determine which day is selected, query your data source for available time slots on that day, and then bind the result to the time combo box.
Here is a (not very good) example that uses countries and states.
Upvotes: 1