簡體   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