2015-05-23 12 views
0

我真的很困惑,在考慮使用哪一種控件來達到我的目的。我應該使用什麼控件在winforms C#中多選項目?

我有項目列表說item1到item10。用戶可以按任意順序選擇4個或5個項目。

現在用戶選擇的項目必須以相同的順序分開。

例如,如果用戶按以下順序選擇項目,item4,item8,item3和item2。

我希望它在相同的順序。 ITEM4,item8,項目3,ITEM2。

如何在winforms控件中實現此目的?

+0

你試過了ListBox控件嗎? – Steve

+7

如果選擇順序非常重要,那麼用戶可以確定他選擇的順序非常重要。並可以修復錯誤。標準用戶界面是*兩個*列表框。一個顯示可用項目,另一個顯示選定的項目和順序。兩個按鈕可在列表框之間來回移動項目。你可能已經見過[this before](http://www.installsite.org/pages/en/msi/articles/MultiListBox/MultiListBox.gif)。 –

+0

雅我嘗試列表框爲'hans passant'說。但問題是我在窗體頁面中有很多其他控件。如果我使用2列表框方法,則表單頁面會佔用更多的部分。是否有任何其他選項可能比2 listbox方法 –

回答

0

這不是一個很好的解決方案,但我按照你的要求寫了它。 根據需要將您的列表框的SelectionMode設置爲MultiExtendedMultiSimple
然後在SelectedIndexChanged事件的列表框的寫代碼:

List<string> orderedSelection = new List<string>(); 
    bool flag = true; 
    private void listBox3_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (flag) 
     { 
      flag = false; 
      var list1 = listBox3.SelectedItems.Cast<string>().ToList(); 
      if (listBox3.SelectedItems.Count > orderedSelection.Count) 
      { 
       orderedSelection.Add(list1.Except(orderedSelection).First()); 
      } 
      else if (listBox3.SelectedItems.Count < orderedSelection.Count) 
      { 
       orderedSelection.Remove(orderedSelection.Except(list1).First()); 
      } 

      var list2 = listBox3.Items.Cast<string>().Except(list1).ToList(); 
      listBox3.Items.Clear(); 
      for (int i = 0; i < list1.Count; i++) 
      { 
       listBox3.Items.Add(list1[i]); 
       listBox3.SelectedIndex = i; 
      } 
      foreach (string s in list2) 
      { 
       listBox3.Items.Add(s); 
      } 
      flag = true; 
     } 
    } 

當用戶選擇一個項目,它涉及到第一列表和項目的其餘來自未來。

此外,還有一種替代方法。您可以使用帶有兩個額外按鈕的CheckedListBox來上下移動選定的項目。因此用戶可以更改所選項目的順序。

0

此解決方案使用CheckListBox的ItemCheck事件以及私人列表來跟蹤點擊項目的順序。

protected List<string> clickOrderList = new List<string>(); 

private void Form1_Load(object sender, EventArgs e) 
{ 
    // Populate the checked ListBox 
    this.checkedListBox1.Items.Add("Row1"); 
    this.checkedListBox1.Items.Add("Row2"); 
    this.checkedListBox1.Items.Add("Row3"); 
    this.checkedListBox1.Items.Add("Row4"); 
} 

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) 
{ 
    if (sender != null && e != null) 
    { 
     // Get the checkListBox selected time and it's CheckState 
     CheckedListBox checkListBox = (CheckedListBox)sender; 
     string selectedItem = checkListBox.SelectedItem.ToString(); 

     // If curent value was checked, then remove from list 
     if (e.CurrentValue == CheckState.Checked && 
      clickOrderList.Contains(selectedItem)) 
     { 
      clickOrderList.Remove(selectedItem); 
     } 
     // else if new value is checked, then add to list 
     else if (e.NewValue == CheckState.Checked && 
      !clickOrderList.Contains(selectedItem)) 
     { 
      clickOrderList.Insert(0, selectedItem); 
     } 
    } 
} 

private void ShowClickOrderButton_Click(object sender, EventArgs e) 
{ 
    StringBuilder sb = new StringBuilder(); 
    foreach (string s in clickOrderList) 
    { 
     sb.AppendLine(s); 
    } 

    MessageBox.Show(sb.ToString()); 
} 
相關問題