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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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);
回答3:
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.
回答4:
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!
回答5:
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
回答6:
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
}
}