Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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.

 

... 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.

Posted

I would suggest something like:

 

//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 ;)

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...