禁用chekcedlistbox中的一个项目 [英] Disable one item in chekcedlistbox

查看:77
本文介绍了禁用chekcedlistbox中的一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,如果选择'option3',则应禁用checkedlistbox中的项目5(不允许用户选择这些项目)



我该怎么做呢?



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");

    }

}





一旦用户到达项目17,它调用消息然后,它重复每个项目的消息 - 并检查项目无论



我尝试过:



谷歌上没有太多信息



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

推荐答案

也许用户最友好的选择就是更新您检查列表框中的项目,包括或排除项目5,以响应checkedListBox1_ItemCheck。
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.


不确定我理解您,但是如果您想根据条件更改项目检查状态: Define.Text!=选项3,您可以将代码简化为:

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:
bool x = !(Define.Text == "Option 3");
checkedListBox1.SetItemChecked(5, x);





请参阅: CheckedListBox.SetItemChecked Method(Int32,布尔))(System.Windows.Forms) [ ^ ]



好​​像,你是初学者,所以我是强烈建议阅读: 8 C#开发人员犯的最常见错误 [ ^ ]


您的问题不明确,此答案将假设您询问如何阻止用户更改CheckedListBox项目的检查状态? 。



可以通过在检查状态发生变化之前发生的ItemCheck事件来实现。 ItemCheckEventArgs给出了即将更改的项目的索引,CurrentValue中的当前检查状态以及NewValue中的建议的新检查状态。可以重新分配NewValue以更改控件的行为。



可以像这样阻止对第5项的更改

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
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);
  }
}





要给出一些视觉指示,表明项目已被锁定,检查状态可以在lockItem5设置为true之前将其设置为CheckState.Indeterminate。这样第5项看起来与其他项目不同,用户可能会意识到反复点击不会让它发生变化!



Alan。



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.


这篇关于禁用chekcedlistbox中的一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆