문제

I have a checklistbox that has values such as Value 1, Value 2, Value 3. If a user checks "Value 1" then the Label1 will change it's name to "Value 1" and TextBox1 will be enabled. However, I do not know how to check if the selected value has been deselected. If user deselects a value then Label1 will change from "Value 1" to "Label1" and TextBox1 will be disabled. How can I achieve this?

도움이 되었습니까?

해결책

Subscribe to event ItemCheck it is raised when item is checked/unchecked:

private void CheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)
{
    var value = checkedListBox1.Items[e.Index].ToString();

    if (value == "Value 1" && e.NewValue == CheckState.Checked)
    {
        Label1.Text = "Value 1";
        Textbox1.Enabled = true;
    }
    else
    {
        //disable
        Label1.Text = "Label 1";
        Textbox1.Enabled = false;
    }
}

다른 팁

You can add custom code to an ItemCheck event handler:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
  MessageBox.Show(e.NewValue.ToString());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top