tags:

views:

757

answers:

2

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.

+1  A: 

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.

cdonner
Unfortunatly i have a 7x7 array with the days and times (think timetable) and depending on the comboBox element selected it will know which element to use in the array.If i was to dynamically update the comboBox then i think it would break the system i currently have.Shame there's not a .Enabled for individual items =[
Jamie Keeling
I think I understand what you want to do, but I don't understand why what I suggested does not work. You have a dependency between day and time. First the user selects a day, then you know which times to display for selection. Your code snippet is very difficult to understand, so that a red flag right there.
cdonner
I agree with the code being slightly unreadable as it was created in a rush, unfortunately i need to be able to have all the selections available in the comboBox yet only have a few selectable (Grey the invalid ones out) due to the way my collection works.If i was to redo it then i'd definitely do your way.
Jamie Keeling
Then you need to use another control. The DropdownList does not support unselectable items. http://forums.asp.net/t/1132654.aspx
cdonner
That's in ASP.Net, I'm using C# 3.5
Jamie Keeling
I see, I should have seen that, but I don't think the recommendation changes, as the Combobox.Objectcollection does not support disabled items, either. http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection_members.aspx. Since you are already done with your assignment, it seems like a moot point, though.
cdonner
I'm always looking to make improvements in my applications and so i'll try and implement this, thanks for the link
Jamie Keeling
+1  A: 

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.

  1. Grab selected index values of each combo
  2. Ensure that the selected index values are valid together
  3. If the combination is not, then notify the user
  4. Otherwise process accordingly based on whether the current context is for a user or a member.

try setting up the available values of each combo into two separate enums and process based on that rather than hard coding the vlaues.

I've added an example and a bit more information =]
Jamie Keeling
Oh no its an assignment i've already completed, i'm just trying to add some validation to make it easier for the user.If i had the time i'd probably redo the entire thing from the ground up. I'll try your suggestion as it seems to be similar to what im trying to achieve myself anyway.
Jamie Keeling