2012-11-08 4 views
0

enter image description here 6 개의 콤보 상자가 모두 같은 열의 데이터베이스에서 채워지는 C# 형식으로되어 있습니다. 학생 결과 콤보 박스가 제목 이름을 채우기위한 i-e. 이제는 이전 콤보 상자에서 주제가 이미 선택되어있는 경우 제 2 콤보 상자에서 제목 이름을 삭제하는 방법을 원합니다. 콤보 상자 속성을 사용하여 데이터베이스의 각 콤보 상자를 채 웁니다.C에서 콤보 상자 값의 유효성을 검사하는 방법

+0

WPF 또는 WinForms? – Joe

+0

승 폼 디스플레이 – Asim

+0

위 지금 나는 사용자가 첫 번째 콤보 상자의 첫 번째 제목으로 다음에 두 번째 콤보 상자 제목 영어 제거 – Asim

답변

1

Firsty 당신은 List<ComboBox>를 작성하고 각 ComboBox이 방법으로 항목과 SelectedValueChanged 이벤트 처리기를 추가 할 수 있습니다 : 다음

List<ComboBox> comboBoxes = new List<ComboBox>(); 
//here add your comboboxes to the list using the Add() method 

foreach(ComboBox cb in comboboxes){ 
    cb.SelectedValueChanged += new EventHandler(comboBox_SelectedValueChanged); 
    //add items to the ComboBox 
} 

SelectedValueChanged 이벤트를 사용하여 다른 선택 상자에서 선택한 항목을 삭제 해고 SelectedItem 재산 및 Items.Remove() 방법 :

private void comboBox_SelectedValueChanged(object sender, EventArgs e){ 
    //Fill the comboboxes 
    ComboBox comboBox = (ComboBox)sender; 
    if(comboBox.SelectedItem != null){ 
     foreach(ComboBox cb in comboboxes) 
     cb.Items.Remove(comboBox.SelectedItem); 
    } 
} 

PS :이 하시다하지 않았다 사용자가 ComboBox에서 요소를 두 번 이상 선택하면 다른 ComboBox에서 해당 요소를 삭제하므로 rk도 마찬가지입니다. 그래서 이것은 그것을 개발하는 방법에 대한 조언을 해주는 예제 일뿐입니다. 요약하면 개선해야합니다.


편집 :이 문제를 해결하기 위해 몇 가지 문제가있을 수 있기 때문에 , 나는 당신을 위해 그것을 개선 :

private void comboBox_SelectedValueChanged(object sender, EventArgs e){ 
    foreach(ComboBox cb in comboBoxes){ 
     //Here listItems is a list of elements you added firstly to the comboboxes 
     if(cb.Items.Count < listItems.Count) 
     foreach(object item in listItems){ 
      if(!cb.Items.Contains(item)) 
       cb.Items.Add(item); 
     } 
     } 
    } 

    //Remove the selected items in all the comboboxes 
    ComboBox comboBox = (ComboBox)sender; 
    if(comboBox.SelectedItem != null){ 
     for(int i = 0; i < comboBoxes.Count; i++){ 
     for(int j = 0; j < comboBoxes.Count; j++){ 
      if(i != j && comboBoxes[j].SelectedItem != null && comboBoxes[j].Contains(comboBoxes[j].SelectedItem)) 
       comboBoxes[i].Items.Remove(comboBoxes[j].SelectedItem); 
     } 
     } 
    } 
} 

이 난 당신이 너무 많은 선택 상자가있는 경우 매우 느린, 그래서 수 가능한 한 빨리하려고했습니다. 하지만 6 ComboBox 컨트롤이있는이 경우에는 잘 작동해야합니다.

0

선택한 항목의 목록이 있어야하며 selectedValueChanged() 이벤트 이후에 이전 콤보 상자 값을 제거하고 각 콤보 상자를 다시 채워야합니다.

List<object> coll = new List<Object>(); 
    List<Object> dbItems = new List<Object>(); 
    private void FillComboBox(object currSelected, ComboBox control) 
    { 
     control.Items.Clear(); 
     foreach (object c in dbItems) 
     { 
      if ((coll.Contains(c) == false) || (c == currSelected)) 
      { 
       control.Items.Add(c); 
      } 
     } 
     control.SelectedItem = currSelected; 
    } 
    private void SelectedItemChanged() 
    { 
     foreach (object c in coll) { 
      if ((comboBox1.SelectedItem != null && c.Equals(comboBox1.SelectedItem)) || 
       (comboBox2.SelectedItem != null && c.Equals(comboBox2.SelectedItem)) || 
       (comboBox3.SelectedItem != null && c.Equals(comboBox3.SelectedItem))) 
      { 
      }else{ 
       coll.Remove(c); 
      } 
     } 
    } 

    private void comboBox1_SelectedValueChanged(object sender, EventArgs e) 
    { 
     // That code must be in every combobox_selectedValueChanged 
     SelectedItemChanged(); 
     FillComboBox(coll, comboBox1); 
     FillComboBox(coll, comboBox2); 
     FillComboBox(coll, comboBox3); 
    } 

PS : 죄송합니다,하지만 난 콤보 상자에서 이전에 선택한 항목을 가져 오기 때문에 내가 어떤 콤보 선택한 항목이 변경되었습니다 추가 방법을 작성하는 방법을 기억 돈`t.

관련 문제