Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 9-Dec-12 18:11pm
Comments
__TR__ at 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 at 10-Dec-12 0:24am
   
Sure. I answered in more detail, please see.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
// 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
  Permalink  
v3
Comments
__TR__ at 10-Dec-12 0:33am
   
+5.
Sergey Alexandrovich Kryukov at 10-Dec-12 0:37am
   
Thank you.
--SA
CHAITANYA KIRAN KASANI at 10-Dec-12 3:11am
   
ThanQ Sergey For Your Information
Sergey Alexandrovich Kryukov at 10-Dec-12 9:04am
   
Sure. Then, are you going to accept the answer formally (green button)? — thanks.
--SA
CHAITANYA KIRAN KASANI at 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
Sergey Alexandrovich Kryukov at 10-Dec-12 23:27pm
   
OK, but I gave you more general answer exactly to the point. Why wouldn't you formally accept it (green button)? -- thanks.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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,
 
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);
}
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 364
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Sergey Alexandrovich Kryukov 166
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100