簡體   English   中英

C#Winform從CheckListBox獲取價值

[英]C# Winform Getting Value from CheckListBox

我在winform上有一個checkedListBox。 我在復選框中填寫了一些代碼,請參見下文。

一切正常。

現在,我需要獲取已檢查項目的值。 我有一些問題。

當我做foreach(var item in clbtest.CheckedItems)

我在執行即時窗口時得到此信息...? 項目

{ Text = "Depos", Value = "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos" }
Text: "Depos"
Value: "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos"

我不知道如何進入“價值”字段。 我嘗試了幾種方法,但似乎沒有任何效果。

private void FillIndexes()
{
    string stext = cblIndexToSearch.Text;
    cblIndexToSearch.Items.Clear();

    cblIndexToSearch.Text = "";
    cblIndexToSearch.DisplayMember = "Text";
    cblIndexToSearch.ValueMember = "Value";

    foreach (var item in indexlist)
    {
        cblIndexToSearch.Items.Insert(0, 
            new { Text = item.indexName, Value = @item.indexPath });
    }
}

希望這可以幫助。 只需創建一個名為“ checkedListBox1”的復選框,代碼就可以正常工作。 我詳細介紹了它的作用。

        checkedListBox1.Items.Clear();

        checkedListBox1.Text = "";
        checkedListBox1.DisplayMember = "Text";
        checkedListBox1.ValueMember = "Value";


        checkedListBox1.Items.Insert(0,
                new { Text = "Rawr", Value = "Whatever 2011"});

        string temp = checkedListBox1.Items[0].ToString(); //gets the string of the item. (switch 0 to the index you want)
        string string_Value = temp.Split(new string[] { "Value = " }, StringSplitOptions.None)[1]; //splits the string by the value part and returns the string value.
        string_Value = string_Value.Substring(0, string_Value.Length - 2); ; //removes the last 2 characters since they are not part of the value.
        //you now have the value in string form.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM