Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi Friends ...
Can We Perform Same Event For Different ComboBoxes...For Example I Have 5 ComboBoxes...All The 5 ComboBoxes Are Performing Same Code Of Event...Now Can We Write Single Event For All The ComboBoxes...in C#,Winforms...

Pls help Me
Posted
Comments
__TR__ 10-Dec-12 0:16am    
You can create a method and call that method in the event of all the 5 comboBoxes.
Sergey Alexandrovich Kryukov 10-Dec-12 0:24am    
Sure. I answered in more detail, please see.
--SA

You are talking not about the same event, but about the same event handle. You always can do it. Naturally, in your event handler, you might need to get information specific to each instance of the control in which you have added an event handler to the invocation list of some event instance. You can always do it through the arguments passed to the handler.

Here is how it might look:

C#
// suppose you already have some combo boxes, say, an array of them:
ComboBox[] comboBoxes = // ...

//...

foreach (ComboBox comboBox in comboBoxes)
   comboBox.SelectedIndexChanged += (sender, eventArgs) => { // let's take this event; it is often used
       ComboBox instance = (ComboBox)sender; // you can bravely do this case and be sure it's a ComboBox
       int selectedIndex = instance.SelectedIndex;
       // in other cases, you can also use eventArgs, for example, mouse coordinates for mouse events
       SomeActionOnComboBoxSelectedIndexChanged(instance, selectedIndex);  
   };

//...

// note that the parameters are strongly typed:
void SomeActionOnComboBoxSelectedIndexChanged(ComboBox sender, int newIndex) {
    // and not, this code can be used for all combo boxes
}


Something like this.

Good luck,
—SA
 
Share this answer
 
v3
Comments
__TR__ 10-Dec-12 0:33am    
+5.
Sergey Alexandrovich Kryukov 10-Dec-12 0:37am    
Thank you.
--SA
CHAITANYA KIRAN KASANI 10-Dec-12 3:11am    
ThanQ Sergey For Your Information
Sergey Alexandrovich Kryukov 10-Dec-12 9:04am    
Sure. Then, are you going to accept the answer formally (green button)? — thanks.
--SA
CHAITANYA KIRAN KASANI 10-Dec-12 23:08pm    
I have a Simple Code Sergey
see The Below Code:

private void ComboBox_Leave(object sender, System.EventArgs e)

{

ComboBox cmb = (ComboBox) sender;

//My Code Is Here

}

// Now It Is Running Successfully
Yes... Suppose, we have written event for one ComboBox.. Now all you need to do is call this event for other comboboxes.. Like in your VS IDE go to comboBox's event tab and select your written event for other ComboBoxes...
or else by code,

C#
private void ComboBox1_SelectedIndexChange(object sender, EventArgs e)
{
     // code...
}

private void ComboBox2_SelectedIndexChange(object sender, EventArgs e)
{
     ComboBox1_SelectedIndexChange(sender, e);
}

private void ComboBox3_SelectedIndexChange(object sender, EventArgs e)
{
     ComboBox1_SelectedIndexChange(sender, e);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900