user6477547
user6477547

Reputation:

C# WPF ComboBox SelectedIndex Change

I have a ComboBox where the SelectedIndex is set equal to zero. How do you programatically return the SelectedIndex of a ComboBox when the user selects a new professor and the SelectedIndex changes? Below is my code so far.

System.Windows.Controls.ComboBox comboBox1 = new System.Windows.Controls.ComboBox();
returnedTable = sqlFunctions.getTable("professor");
comboBox1.Items.Insert(0, "Professor");

for (int i = 0; i < returnedTable.Tables[0].Rows.Count; i++)
{
    comboBox1.Items.Insert(i + 1, returnedTable.Tables[0].Rows[i]["first_name"].ToString() + " " + returnedTable.Tables[0].Rows[i]["last_name"].ToString());
}

comboBox1.SelectedIndex = 0;

// Code to capture newly changed selected index??

Upvotes: 0

Views: 5778

Answers (1)

ViVi
ViVi

Reputation: 4464

SelectionChanged is the event you're looking for. You can get the comboBox1.SelectedIndex value inside this event.

In constructor :

public Window1()
{
    InitializeComponent();

    comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1SelectionChanged);
}

Now handle the selectionchanged event as below :

void comboBox1SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var currentSelectedIndex = comboBox1.SelectedIndex;
}

Upvotes: 1

Related Questions