2011-05-10 158 views
0

我有兩個列表框。第一個列表框包含交通違規列表。當你點擊添加按鈕並執行代碼時,listbox2得到這個項目「ListBoxTest.Violation」,而不是從listBox1中顯示的項目...將列表框項轉移到另一個列表框(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 2011-05-10 05:08:48

+0

我修改了代碼來解決問題。 – 2011-05-10 05:20:31

0

確保listbox2具有相同的設置爲listbox1,例如listbox2.ValueMember,listbox2.DisplayMember ..

+0

是的,它的工作。 – yonan2236 2011-05-10 05:11:45

+0

可以接受你的答案爲一半,另一個爲Splendor>:) – yonan2236 2011-05-10 05:12:23

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

谷歌......... – yonan2236 2011-05-10 05:18:31

+0

它無所謂從哪裏8least我試了一下你 – Devjosh 2011-05-10 05:20:29

+0

學會欣賞你的同行的幫助,如果你想和這個論壇保持長久的關係 – Devjosh 2011-05-10 05:21:17

相關問題