Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a combobox, where if 'option3' is selected, items 5 in the checkedlistbox should be disabled (not allow the user to select these items)

How would I go about doing this?

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (checkedListBox1.GetItemCheckState(5) == CheckState.Checked && (Define.Text != "Option 3"))
    {
            MessageBox.Show("This part does not require this process");

    }

}


Once the user gets to item 17, it calls the message however, it repeats the message for every item afterwards - and checks the item regardless

What I have tried:

There's not much information on google
Posted
Updated 26-Apr-18 3:57am
v2

Maybe the most user friendly option would be to update the Items from your checkedlistbox, including or excluding items 5, in response to checkedListBox1_ItemCheck.
 
Share this answer
 
Comments
Member 13765884 26-Apr-18 6:01am    
sorry, I dont understand what that means. Could you elaborate please
Not sure i understand you well, but if you want to change item checked state based on condition: Define.Text != "Option 3", you can simplify your code to:
C#
bool x = !(Define.Text == "Option 3");
checkedListBox1.SetItemChecked(5, x);


See: CheckedListBox.SetItemChecked Method (Int32, Boolean) (System.Windows.Forms)[^]

Seems, you're very beginner, so i'd strongly recommend to read this: 8 Most common mistakes C# developers make[^]
 
Share this answer
 
Your question is not clear and this answer will assume that you asked "How do I prevent a user changing the checkstate of a CheckedListBox item?".

It can be achieved via the ItemCheck event which occurs before the checkstate has changed. The ItemCheckEventArgs gives the index of the item which is about to change, the current checkstate in CurrentValue and the proposed new checkstate in NewValue. NewValue may be reassigned to change the behaviour of the control.

Changes to item 5 could be prevented like this
C#
private bool lockItem5;

private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
  CheckedListBox clbx = (CheckedListBox)sender;
  // The ItemCheck event is raised before the check is changed
  Debug.Print(@"""{0}"" Index:{1} Current:{2} New:{3}", clbx.Items[e.Index], e.Index, e.CurrentValue, e.NewValue);

  if (e.Index == 5 && lockItem5) {
    // Suppress the change by setting NewValue to CurrentValue
    e.NewValue = e.CurrentValue;
    Debug.Print("[{0}] change suppressed", e.Index);
  }
}


To give some visual indication that the item has been locked the checkstate could be set to CheckState.Indeterminate just before lockItem5 is set to true. That way item 5 will look different to the others and a user might realise that clicking repeatedly is not going to make it change!

Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900