User selects Item from Combobox prompts population of other combobox [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Given two comboboxes (dropdown) called cbClient and cbAssignment.
What I want is the following:

- on load cbClient is populated from a dataset with the list of CLIENTS and cbAssignment is initially disabled (I have this working perfectly)

- when the user selects a client from the cbClient Combobox (dropdown) it should trigger an event that I can use - in this event I will load the assignments corresponding to the selected CLIENT in the cbAssignment combobox and then enable it

This is what I am unable to accomplish, my first problem (if you see others please point them out) is simple - I am using the cbClient_SelectedIndexChanged event (also tried the "SelectedValueChanged event) to trigger when the user selects a CLIENT from the list and my problem is when my cbClient is loaded (as shown in the code below in the form_load before the user selects anything) it launches the event and messes everything up.

I tried to add a contigency so the code in the event would not load if if the cbClient index wasn't valid but that doesn't work either, if I use != -1 then while loading it is =0 and this goes, if I use !=0 when I set the .SelectedIndex it loads.

Code:
... in the form_load event ...
	// Fill Client ComboBox
	cbAssignment.Enabled = false;		
	cbClient.DataSource = ds;
	... configure cbClient ...
	cbClient.SelectedIndex = -1;
}


private void cbClient_SelectedIndexChanged(object sender, System.EventArgs e)
{
	if (cbClient.SelectedIndex != -1)	// this doesn't work unless I block both -1 and 0 which might not be a good idea
	{
		// Load all corresponding Assignments
		... load the data into cbAssignment depending on the current selected value in cbClient
		cbAssignment.Enabled = true;
	}
}

Kind of stuck, maybe someone could provide a better solution to accomplish my goal... Any help/hints would be greatly appreciated
Thanks.
 
I would suggest something like:

C#:
//global variable
Boolean blLoading = false;


// Fill Client ComboBox
	cbAssignment.Enabled = false;		
             blLoading=true;
	cbClient.DataSource = ds;
	... configure cbClient ...
	cbClient.SelectedIndex = -1;
             blLoading=false;
}


private void cbClient_SelectedIndexChanged(object sender, System.EventArgs e)
{
	if (!blLoading)	
	{
		// Load all corresponding Assignments
		... load the data into cbAssignment depending on the current selected value in cbClient
		cbAssignment.Enabled = true;
	}
}

You have to excuse any bad syntax as VB is my game ;)

:)
 
Back
Top