2017-02-10 36 views
1

我想我programme.i我能看到的價值使用其他功能checklistbox選擇的多個值通過message boxC#:我怎樣才能在checklistbox選擇的值存儲到一個數組

private void button8_Click(object sender, EventArgs e) 
{ 
    foreach (object item in checkedListBox1.CheckedItems) 
    { 
     MessageBox.Show(item.ToString()); 
    } 
} 
如下

什麼是各種方法,雖然我可以訪問這些選定的值並將它們存儲到一個數組,所以我可以在程序的其他部分使用它們? 編輯:我雖然通過搜索得知ListItem類它可以實現,但是當我嘗試我的visualbasic 2015並不顯示這樣的類甚至存在。

+0

相當混亂,很容易,如果你可以用描述的問題/問題 –

+0

我有我的checkedlistbox 4號我已經選擇了兩個例子說明現在我想將它們存儲在一個數組中,以便我可以將它們用作我的其他函數的輸入。@ HariPrasad –

回答

0

要在程序的其他部分使用它,通過將arrayList聲明爲類成員,使arrayList成爲全局的(因爲建議不幸)。

public partial class Form1 : Form 
{ 
    System.Collections.ArrayList arrayList = new System.Collections.ArrayList(); 

    // code if any... 

    private void button8_Click(object sender, EventArgs e) 
    { 
     foreach (object item in checkedListBox1.CheckedItems) 
     { 
      arrayList.Add(item.ToString()); 
     } 
    } 
} 
0

您可以將值存儲到ArrayList。下面是相同的代碼:

private void button8_Click(object sender, EventArgs e) 
{ 
    ArrayList list = new ArrayList(); 
    foreach (object item in checkedListBox1.CheckedItems) 
    { 
     list.Add(item.ToString()); 
    } 
} 

可以使用list只要你喜歡呢。

+1

希望你錯過了這一點:'所以我可以在程序的其他部分使用它們' –

+0

你可以通過這個列表來運行你想要使用的值嗎?> –

+0

爲什麼不是'list'是全局的? –

0

延伸到你寫

代碼
private void button8_Click(object sender, EventArgs e) 
{ 
    List<object> list= new List<object>(); 
    foreach (object item in checkedListBox1.CheckedItems) 
    { 
     list.Add(item.ToString()); 
    } 
    // Assign List to Element to display 
    //e.g Listbox 
    //Listbox1 lbOx1 = new ListBox(); 
    //lbOx1.Text= list 
} 
相關問題