LostFocus event of ComboBox fires twice

M

Ming

I have implemented a combo box to display an Info property
of myClass as below:

In the form:

private void comboBox1_LostFocus(object sender,
System.EventArgs e)
{
Debug.WriteLine(comboBox1.Text);
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add(new myClass("1","aaa"));
comboBox1.Items.Add(new myClass("2","bbb"));
comboBox1.Items.Add(new myClass("3","ccc"));
comboBox1.Items.Add(new myClass("4","ddd"));
comboBox1.DisplayMember ="Info";
comboBox1.SelectedIndex = 0;
this.comboBox1.LostFocus+=new EventHandler
(comboBox1_LostFocus);
}

public class myClass
{
private string _id;
private string _name;

public myClass(string id, string name)
{
_id = id;
_name = name;
}

public string Info
{
get {return _id + " and " + _name;}
}
}

the LostFocus event is fired when the combo box RECEIVE
FOCUS(this is weird), as well as lost focus (that is
expected).

Can somebody help explain why this happen, and how to get
around it?

Thanks
Ming
 
M

Martin

why should it be weird?
when a form receives focus, it actually has three got focus and two lost
focus events
not everything is fine and nice
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top