2011-05-10 10 views
0

두 개의 목록 상자가 있습니다. 첫 번째 목록 상자에는 교통 위반 목록이 포함되어 있습니다. 추가 버튼을 클릭하고 코드를 실행하면 listbox1은 listBox1에서 표시되는 항목이 아닌 "ListBoxTest.Violation"항목을 가져옵니다. ...목록 상자 항목을 다른 목록 상자로 전송 (Winforms C#)

내 코드에 어떤 문제가 있습니까?

namespace ListBoxTest 
{ 
    /// <summary> 
    /// Description of MainForm. 
    /// </summary> 
    public partial class MainForm : Form 
    { 
     private List<Violation> violationList = new List<Violation>(); 
     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     void MainFormLoad(object sender, EventArgs e) 
     { 
      LoadViolations(); // Initialize and add violations to violationList. 
      listBox1.DataSource = violationList; // Set the DataSource property. 
      listBox1.ValueMember = "Code"; 
      listBox1.DisplayMember = "Description"; 

     } 

     void LoadViolations() 
     { 
      Violation violation; 

      violation = new Violation("001", "Beating the red light"); 
      violationList.Add(violation); 

      violation = new Violation("002", "Exceeding posted speed limit on the road"); 
      violationList.Add(violation); 

      violation = new Violation("003", "Driving a vehicle without license to drive"); 
      violationList.Add(violation); 

      violation = new Violation("004", "Driving a non registered vehicle");  
      violationList.Add(violation); 

      violation = new Violation("005", "Vehicle has no plate number"); 
      violationList.Add(violation); 
     } 

     void BtnAddClick(object sender, EventArgs e) 
     { 
      listBox2.Items.Add(listBox1.SelectedItem); // Add item from listBox1 to listBox2; 
     } 
    } 

    /// <summary> 
    /// Violation Class 
    /// Properties: Code, Description 
    /// </summary> 
    public class Violation 
    { 
     private string _code; 
     private string _description; 

     public Violation(string code, string description) 
     { 
      _code = code; 
      _description = description; 
     } 

     public String Code 
     { 
      get { return _code; } 
      set { _code = value; } 
     } 

     public String Description 
     { 
      get { return _description; } 
      set { _description = value; } 
     } 
    } 
} 

답변

0

유형 선택한 항목을 '위반'으로 전송합니다. 이렇게하면 문제가 해결됩니다. 편집 : 문제를 해결하기 위해 코드를 수정했습니다.

private void AddClick(object sender, EventArgs e) 
{ 
    // Set the DataSource property.   
    listBox2.ValueMember = "Code"; 
    listBox2.DisplayMember = "Description";  
    listBox2.Items.Add((Violation)listBox1.SelectedItem); 
}  
+0

같은 값이 listBox2에 추가됩니다. – yonan2236

+0

문제를 해결하기 위해 코드를 수정했습니다. –

0

listbox2는, 예를 들어 listbox1와 동일한 설정이 있는지 확인 listbox2.ValueMember, listbox2.DisplayMember ..

+0

네, 효과가있었습니다. – yonan2236

+0

은 답변을 반으로, 다른 하나는 Splendor> :) – yonan2236

0
To move items B/W listbox take help from following post i hope it'll help 



Protected void btnAdd_Click(object sender, EventArgs e) 
    { 
    if (lstEmployees.SelectedIndex > -1) 
     { 
     string _value = lstEmployees.SelectedItem.Value; 
              //Gets the value of items in list. 

     string _text = lstEmployees.SelectedItem.Text; 
             // Gets the Text of items in the list. 

     ListItem item = new ListItem(); //create a list item 

     item.Text = _text;    //Assign the values to list item 

     item.Value = _value; 
     lstSelectedEmployees.Items.Add(item); //Add the list item to the selected list of employees 

     lstEmployees.Items.Remove(item); //Remove the details from employee list 

     } 


    // Code to Remove selected item from the list 

    protected void btnRemove_Click(object sender, EventArgs e) 
     { 
      if (lstSelectedEmployees.SelectedIndex > -1) 
      { 
       string _value = lstSelectedEmployees.SelectedItem.Value; //Gets the value of items in list. 

       string _text = lstSelectedEmployees.SelectedItem.Text; // Gets the Text of items in the list. 

       ListItem item = new ListItem(); //create a list item 

       item.Text = _text;    //Assign the values to list item 

       item.Value = _value; 
       lstSelectedEmployees.Items.Remove(item); //Remove from the selected list 

       lstEmployees.Items.Add(item); //Add in the Employee list 


      } 

     } 

    Code to Remove All items from the list 

     protected void btnReset_Click(object sender, EventArgs e) 
     { 
      int _count=lstSelectedEmployees.Items.Count; 
      if (_count != 0) 
      { 
      for (int i = 0; i < _count; i++) 
       { 
        ListItem item = new ListItem(); 
        item.Text = lstSelectedEmployees.Items[i].Text; 
        item.Value = lstSelectedEmployees.Items[i].Value; 
        lstEmployees.Items.Add(item); 
       } 
      } 

      lstSelectedEmployees.Items.Clear();//clear the items 

     } 


    // Code to Add All items to the list 


     protected void btnAddAll_Click(object sender, EventArgs e) 
     { 
      int _count = lstEmployees.Items.Count; 
      if (_count != 0) 
      { 
       for (int i = 0; i < _count; i++) 
       { 
        ListItem item = new ListItem(); 
        item.Text = lstEmployees.Items[i].Text; 
        item.Value = lstEmployees.Items[i].Value; 
        //Add the item to selected employee list 

        lstSelectedEmployees.Items.Add(item); 
       } 

      } 

      //clear employee list 

      lstEmployees.Items.Clear(); 

     } 
+0

google ......... – yonan2236

+0

8 개를 사용해 보았습니다. – Devjosh

+0

당신의 동료로부터 도움을 주셔서 감사합니다. 너는이 포럼에 오래 머물고 싶어. – Devjosh

관련 문제