I have a form with a ComboBox
that provides a dropdownlist. On the comboBox's SelectedIndexChanged event
, am running some code, but I don't want that code to run when the form loads. Unfortunately, when I load the form (before I make a selection in the combobox), SelectedIndexChanged
of the combobox fires (I think when the combobox is databinding
). Is there a way of avoiding such behaviour?

- 19,232
- 5
- 47
- 69

- 9,190
- 36
- 114
- 202
-
Not sure about on the desktop, but in .NET Compact, this event actually appears to fire before the Form_Load event, which is really problematic - nothing on the form is even set up yet when this code gets hit. On CF, the solution is unfortunately a form variable that gets set at the end of Load, and then the event handler checks for this variable before it fires. – SqlRyan Feb 11 '13 at 02:19
7 Answers
If you want to react only when the user change the selected item in the combo box, then it is better to subscribe to SelectionChangeCommitted.

- 30,738
- 21
- 105
- 131

- 9,447
- 1
- 32
- 43
-
6
-
@Arijit Mukherjee right. When typing a value into textbox instead of selecting directly. It won't trigger SelectionChangeCommitted event – thoitbk Jan 06 '15 at 13:08
-
1Damn, I have been searching the web for quite a while to find a simple solution like this. I have been using the SelectionChanged event and it worked in VB.Net - WPF and now using it on C# - WinForms and it doesnt work or maybe it is just me. Thanks! – Stephan Aug 29 '15 at 20:58
-
Akams Razor - the simplest solution is the most likely (and the best in this case). – Destek Jul 30 '20 at 12:45
You can simply unbind the SelectedIndexChanged
event, call your fill
function and bind the SelectedIndexChanged
event again. Unfortunately, this doesn't work with a grid.
For example:
this.cmb.SelectionChanged -= new System.EventHandler(this.cmb_SelectionChanged);
cmb.fill(); //Your function
this.cmb.SelectionChanged += new System.EventHandler(this.cmb_SelectionChanged);

- 30,738
- 21
- 105
- 131

- 20,688
- 54
- 182
- 286
Be sure to set the DataSource
property in your onload()
function after assigning the ValueMember
and Datamember
properties.
This will help you to solve your problem!

- 13,872
- 15
- 57
- 93

- 111
- 1
- 2
Why not have a boolean
flag that indicates when your Form
has finished loading?
In your SelectionChanged
event, check if the boolean
flag is true
. If it is true
then handle the event, otherwise ignore it.
-
2
-
4There is already a boolean flag ` Control.Created` telling you, that the form has finished loading, which is also valid for forms. – user1734987 Oct 12 '12 at 06:54
-
`ToolStripComboBox` variation does not have a `SelectionChangeCommitted` event. So, this method will have to do. Or, to prevent race conditions, use `lock` and `Monitor.TryEnter`. – spoulson May 23 '13 at 18:47
-
@user1734987: That's not the same thing. There could be initialization code that needs to be completed and Control.Created won't fire that. – May 23 '13 at 19:05
Here is a simple solution that leaves your code almost untouched:
In the SelectedIndexChanged event, check if the myComboBox handle is created using the (IsHandleCreated) method. Another added check is to check if the user is actually focusing your combobox control to change selected index.
private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myComboBox.IsHandleCreated && myComboBox.Focused)
{
// Do something here
}
}

- 819
- 9
- 11
-
You need to add a SelectedValue != null check as well to this, and then it's a perfect solution. – Mecanik May 18 '21 at 06:07
-
-
VB
RemoveHandler lbxNomes.SelectedIndexChanged, AddressOf lbxNomes_SelectedIndexChanged
lbxNomes.DataSource = dst
Label1.Text = String.Format("Encontrados {0} Sócios nesta pesquisa", dst.Rows.Count)
Label1.Visible = True
AddHandler lbxNomes.SelectedIndexChanged, AddressOf lbxNomes_SelectedIndexChanged

- 31,141
- 6
- 88
- 120

- 163
- 1
- 4
It worked for me in a way with the following code:
private void ddlChapter_SelectionChangeCommitted(object sender, EventArgs e)
{
if (ddlChapter.SelectedValue != null)
{
// Do something here
}
}

- 30,738
- 21
- 105
- 131

- 4,090
- 41
- 39
-
SelectionChangeCommitted will stop to call index change items, once all items loaded into dropbox then you can select one by one only, it will increase your loading speed also. – Anjan Kant Aug 16 '16 at 07:06