Enable / disable the button until the item is selected in the list?

How to disable a button until an item is selected in the list?

+3
source share
2 answers

First you turn off your button:

button1.Enabled = false;

Then you subscribe to the SelectedIndexChanged event in the list. Bellow is a handler:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex >= 0)
    {
        button1.Enabled = true;
    }
    else
    {
        button1.Enabled = false;
    }
}

You subscribe to an event from within the Visual Studio IDE or programmatically:

listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged);
+4
source

Since you mention winforms, one way is to set the IsEnabled = false property in the property explorer. Then add an event for the OnSelectionChanged list. VS automatically inserts the code for you, then you can put it in an event handler:

 button1.IsEnabled = listbox1.SelectedIndex > 0;

WPF, , Databinding.

0

Source: https://habr.com/ru/post/1773643/


All Articles