Reputation: 225
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?
Upvotes: 0
Views: 153
Reputation: 17590
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;
}
}
Upvotes: 1
Reputation: 63317
You can add custom code to an ItemCheck
event handler:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
MessageBox.Show(e.NewValue.ToString());
}
Upvotes: 1