Windows Develop Bookmark and Share   
 index > Windows Forms Designer > creating custom events for combo box
 

creating custom events for combo box

Hello,

I am creating a mobile 5 application using CF 3.5.

I have a combo box that keeps on firing the SelectionIndexChanged event. I need this event to get an item from the combo box. However, when the combo datasource is set it will fire this. I have resolved the issue with a simple bool test. See code below. However I have noticed that when debugging the event often fires. Its hard to track down what is causing this to fire.

Code Snippet

//Set to false to prevent pre-firing of selectValue changed event before datasource is set.

this.cboDataSourceSet = false;

this.cboRecentNumbers.DataSource = this.bsRedialedNumbers;

this.cboRecentNumbers.DisplayMember = "Name";

this.cboRecentNumbers.ValueMember = "ID";

this.cboDataSourceSet = true;

And in the event itself

Code Snippet

private void cboRecentNumbers_SelectedIndexChanged(object sender, EventArgs e)

{

if (this.cboDataSourceSet)

{

//Do something here

}

}

I am thinking of 2 methods to solve this:

1) is there a way to stop the event from firing only when you need it to. (maybe a way to track down where it is being fired)

2) Inherit from the combo box and add your own custom SelectionChanged event. (anyone point me in the right direction?).

I would perfer method 2 as I have never done that before, and good to learn some thing new.

Many thanks,

Steve

steve1_rm  Thursday, March 27, 2008 5:34 AM

I don't think you need to rebuild the logic of the event system in the combobox. All you need to do is to remove your selectedIndex changed event from the combobox's property sheet. After you've initialized everything and are ready for the event to begin firing, you wire it up:

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

When you're finished and you want to stop the event firing:

this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);

jbaird-pa  Saturday, March 29, 2008 4:08 PM

I don't think you need to rebuild the logic of the event system in the combobox. All you need to do is to remove your selectedIndex changed event from the combobox's property sheet. After you've initialized everything and are ready for the event to begin firing, you wire it up:

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

When you're finished and you want to stop the event firing:

this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);

jbaird-pa  Saturday, March 29, 2008 4:08 PM

Hello Steve,

I agree with John. You don’t need to re-implement a combo box only to avoid the firing of the SelectedIndexChanged event. You can do something like:

Code Snippet

List<int> numbers = new List<int>() { 0, 1, 2, 3};

/*

* Remove Eventhandler

*/

_cboNumbers.SelectedIndexChanged -= new System.EventHandler(_cboNumbers_SelectedIndexChanged);

_cboNumbers.DataSource = numbers;

/*

* Add Eventhandler

*/

_cboNumbers.SelectedIndexChanged += new System.EventHandler(_cboNumbers_SelectedIndexChanged);

In the above example the combobox shows '0' as the selected item, but since the Eventhandler was added after setting the datasource, no SelectedIndexChanged eventis fired.

dominik_h  Sunday, March 30, 2008 8:04 PM

You can use google to search for other answers

Custom Search

More Threads

• Excel like selection?
• Visual Basic Defaults
• Problem with ToolboxBitmapAttribute
• custom calendar control
• Get Controls collection and list it on xml or text file
• Inherited Control - How to Set Prop in Win Form which Will USe Control
• Exception of type ExceptionCollection thrown when a form is attempted open from VS2005's IDE
• Tell VS.NET to ignore certain code???
• WinForms 2005 - "Ctrl" drag on a form design.
• Reg: Custom Drawing the Menu in Dotnet 2.0