KyloRen
KyloRen

Reputation: 2741

How to test which combobox has been most recently changed? WPF

Is there a way to check to see which combobox which value has been most recently changed?

how to check if item is selected from a comboBox in C#

I know you can do this,

if (MyComboBox.SelectedIndex >= 0)
{
    //do stuff
}

The problem I am having is that I am combining Event Handlers in to one handler due to the amount of comboboxes and having one event for each combobox really is not practical if I can help it.

Is there a way to have a variable assigned giving you the name of the combobox which value has been most recently changed? Or will I have to use individual event handlers for each combobox?

Upvotes: 0

Views: 88

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29026

Hope that each comboBox having an unique name, then we can use those name to identify which one is the sender of event: Now consider the following code for this:

private void CboFirst_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox selctedComboBox = sender as ComboBox;
    string ComboName = selctedComboBox.Name;
    // Do something
}

Now tracking the last updated ComboBox, You can achieve this by keeping a blobal variable and update it in every trigger so Every time it holds the Latest value( the name of the combobox)

Upvotes: 1

Manish Parakhiya
Manish Parakhiya

Reputation: 3798

Actually It will be very easy to track most recent combobox change when you using single event handler for all combobox. You can do by following way.

string lastComboName=""; // define global variable  
//common event handler for all combobox 
 private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
    var cmb = (ComboBox)sender; 
    lastComboName = cmb.Name;
 }

Upvotes: 1

Related Questions