views:

32

answers:

1

Hello...I am building a WinForms Application in C# .NET

The WinForms Application has a ComboBox where the DropDownStyle is set to DropDownList. When the App is launched, I read an XML file to populate the values of the ComboBox. And, at this time, nothing is selected in the ComboBox by default. As a result, buttons Change and Delete are disabled.

Now, when the user selects a value, I want the buttons Change and Delete to be enabled. So far I have accomplished (although, I am not sure that I have done it in the right way).

I have written the code in the SelectionChangeCommitted Event.

private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
    if (cbList.SelectedItem != null)
    {
        this.btnModify.Enabled = true;
        this.btnRemove.Enabled = true;
    }
    else
    {
        this.btnModify.Enabled = false;
        this.btnRemove.Enabled = false;
    }
}

Now, when I chose a value...the buttons get enabled (as expected). The user then clicks on Delete button and we remove the selected value. Now, there is nothing Selected in the cbList but the buttons are still enabled?

What is the function/event where I check if a value is selected or not and then enable/disable the buttons.

+1  A: 

At the moment, dont have Visual Studio, so I dont remember which events we have. But you can make this,

 private void CheckButtons()
    {

        if (cbList.SelectedItem != null)
        {
            this.btnModify.Enabled = true;
            this.btnRemove.Enabled = true;
        }
        else
        {
            this.btnModify.Enabled = false;
            this.btnRemove.Enabled = false;
        }
    }

and use your func in event

private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
CheckButtons();
}

as you said, after deleting, buttons are still visible, so you can put CheckButtons() function after your delete function like

DeleteX();
CheckButtons();
Serkan Hekimoglu