+3 Chris Blum · October 22, 2014
My winform isn't recognizing when a selectedindex is changed.

I have a combobox that lists all the student names and is initialized in the load event.
When the user selects another student, I want to wipe the old data and do some code using the current data.
However, nothing happens when a user selects a new student in a combo box.

Can anyone help?

private void comboBoxStudentName_SelectedItemChanged(object sender, EventArgs e) {
Object studentNameObject = comboBoxStudentName.SelectedItem;
String studentNameString = studentNameObject.ToString();
textBoxStudentName.Text = studentNameString;
 HashSet<String> noDupesCourseHashSet = new HashSet<String>();
//Load only courses that student has taken lessons in
for (var i = 0; i < Variables.listLessonDate.Count; i++)
string nameCheck = Variables.listStudentName;
string courseCheck = Variables.listCourseName;
if (nameCheck == studentNameString)
}//End If
}//End For
List<String> noDupesCourseList = new List<String>(noDupesCourseHashSet);
comboBoxCourseName.DataSource = noDupesCourseList;

I've also tried
private void comboBoxStudentName_SelectedIndexChanged(object sender, EventArgs e){}


Post a Reply


Oldest  Newest  Rating
+1 Chris Blum · October 22, 2014
I didn't add the event in the properties panel in my winform and instead typed the code directly.
If you want to type the code directly you need to add a binder, such as this.

MyTextBox.KeyDown += new KeyEventHandler(MyKeyDownFunction);
0 Eugene Botma · October 27, 2014
Just to clarify, comboBoxStudentName.SelectedIndexChanged is an event, and 

private void comboBoxStudentName_SelectedIndexChanged(object sender, EventArgs e){}

is a type of method called the event handler. As you surely know, the event handler(s) get called whenever the event happens, in this case when the selected index of the combo box is changed.

You'll notice at the top of your windows forms class, it says public partial class, instead of just public class. This means that some of the code for this class is in another file. This is the <form name>.designer.cs file. Whenever you add an eventhandler to a control on the form in the properties/events window, code similar to 
MyTextBox.KeyDown += new KeyEventHandler(MyKeyDownFunction);

is generated and added to the designer file. So naturally, manually adding this code somewhere yourself would also work. This is not adding a binder, but rather adding an event handler. A single event can have more than one event handler, It's not too uncommon in large programs. Another way of adding an event handler is simply:

MyTextBox.KeyDown += MyKeyDownFunction;

as long as the MyKeyDownFunction matches the delegate for the event. There isn't really a need for the whole "new EventHandler(function name)" thing.
  • 1



C# is an object-oriented language designed for improving the development of web applications.