2012-12-23 175 views
0

我已經嘗試尋找解決方案來解決我的問題,但未能找到一個,因爲它似乎每個人都是我的問題前一步或兩步。選擇複選框列表選中的項目

我試圖從複選框列表中選中一個項目,而不是從中選擇一個項目。

我打算做的是從點擊一個按鈕並選中選中的選項後,知道將要觸發的結果事件,以顯示標記中的文本,而不是檢查項目中的文本。

基於裝飾者模式的程序將允許用戶從一組3/4個可選擇的選項中進行選擇,其中當按下按鈕時將顯示與在標籤末尾的那些項目相關的文本基本文本。目前,我所管理的全部內容都是一次只選擇一個項目,這與第一個例子類似。

例如,當所謂的監視器的選項被選中,它會顯示在標籤:

你得到一臺電腦和顯示器。

如果有多個檢查項目,如顯示器和鍵盤,然後它會說:

你得到一臺電腦和顯示器和鍵盤。

+0

這是的WinForms? WPF? – Blachshma

+0

只是想澄清,你想觸發基於checkbox tick事件的列表? – bonCodigo

+0

是的,Windows窗體應用程序。選項列表是在應用程序啓動時生成的,用戶按下其中描述的事件的按鈕,觸發按照選中選項出現在標籤中的文本。 – madman

回答

0

時,基於新的檢查項目值打響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, 

謝謝,
我希望對您有所幫助:)

+0

該代碼的第二行給了我5個錯誤: 1.'Decorator_Pattern.Form1.checkedListBox1'是一個'字段',但像'type'一樣使用。 \t 2.'Decorator_Pattern.Form1.checkedListBox1_ItemCheck(object,System.Windows.Forms.ItemCheckEventArgs)'是一個'方法',但像'type'一樣使用。\t 3.標識符預期(用於最後一個括號)。 4.類,結構體或接口成員聲明中的標記'+ ='無效。 \t 5.方法必須具有返回類型(對於'ItemCheckEventHandler') – madman

+0

@SergioJanuario您應該在'InitializeComponent();'之後的'public Form1()'下執行此操作。有一個愉快的一天:) –

+0

感謝您清理起來,現在工作。感謝您的幫助。 – madman