Reputation: 777
I am developing a windows forms application and load the list from this code:
private void showList()
{
TeamTableAdapter teamAdapter = new TeamTableAdapter();
lstTeamName.DataSource = teamAdapter.GetTeamsActive();
lstTeamName.DisplayMember = "TeamName";
lstTeamName.ValueMember = "TeamID";
}
I want to enable a button if the user selects one of the items. What event should I put the code into. I the following code but the event seems to fire before the user clicks on the list.
private void lstTeamName_Click(object sender, EventArgs e)
{
if (lstTeamName.SelectedIndex > -1)
btnImportXML.Enabled = true;
}
I moved my code to the SelectedIndexChange event but it still fires before the user selects an item and the selectedIndex is 0.
Upvotes: 1
Views: 277
Reputation: 188
I would agree that you don't want to bind to Click
as that will likely fire too early.
I recommend you look into the DropDownStyle
property. http://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle(v=vs.110).aspx. If you set that to DropDownList
then the SelectedItemChanged
will fire and SelectedIndex
could be > -1
If you leave it as the default DropDown
then you may want to use TextChanged
and check the Text
property.
Upvotes: 1
Reputation: 4151
You dont want to bind to the Click
event but to the SelectedIndexChanged
event. You should be able to accomplish this by simply double clicking on the Control in designer.
Upvotes: 2