繁体   English   中英

选择复选框列表选中的项目

[英]Selecting checkboxlist checked items

我试图四处寻找解决问题的方法,但是没有找到一个解决方法,因为似乎每个问题都比我的问题领先一步或两步。

我试图从它正在从CheckBoxList的检查,而不是选择从它的一个项目被选中选择一个项目。

我打算从知道的结果中做出的事情是,在单击按钮并选中选中的选项之后,将引发将触发的结果事件,以在选中项目的标签中显示文本。

该程序基于装饰器模式,将允许用户从3/4个可选项中进行选择,当按下按钮时,这些选项将在基础文本的末尾在标签中显示与那些项目有关的文本。 目前,我所要做的就是让它一次只在选定的项目上这样做,这与第一个示例类似。

例如,当选中一个名为Monitor的选项时,它将显示在标签中:

您将获得一台计算机和一台显示器。

如果有多个选中的项目(例如“显示器”和“键盘”),则会显示:

您将获得一台计算机,一个显示器和一个键盘。

根据新的已检查项目值触发CheckedListBoxItemCheck事件时,可以更改目标LabelLabel.Text属性。

假设您具有名称为label1Label ,名称为checkedListBox1CheckedListBox和名称为Form1Form ,则可能适用以下条件

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        label1.Text = "You are getting "; //Change the Text property of label1 to "You are getting "
        checkedListBox1.ItemCheck += new ItemCheckEventHandler(checkedListBox1_ItemCheck); //Link the ItemCheck event of checkedListBox1 to checkedListBox1_ItemCheck; not required as long as you link the event through the designer
    }
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Checked && e.CurrentValue == CheckState.Unchecked) //Continue if the new CheckState value of the item is changing to Checked
        {
            label1.Text += "a " + checkedListBox1.Items[e.Index].ToString() + ", "; //Append ("a " + the item's value + ", ") to the label1 Text property
        }
        else if (e.NewValue == CheckState.Unchecked && e.CurrentValue == CheckState.Checked) //Continue if the new CheckState value of the item is changing to Unchecked
        {
            label1.Text = label1.Text.Replace("a " + checkedListBox1.Items[e.Index].ToString() + ", ", ""); //Replace ("a " + the item's value + ", ") with an empty string and assign this value to the label1 Text property
        }
    }
}

样本输入

[x] Monitor
[x] Keyboard
[ ] Mouse
[x] Computer

样本输出

You are getting a Monitor, a Keyboard, a Computer, 

谢谢,
我希望这个对你有用 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM