开发者

WinForms ComboBox - How to Check Values

开发者 https://www.devze.com 2023-01-21 23:07 出处:网络
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 va

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 t开发者_运维问答he 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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号