How to Transferring objects between windows forms in c#

后端 未结 4 2008
暖寄归人
暖寄归人 2021-01-29 06:37

i got a ListView in the windows form.When form loads ListView loading with personel objects. I want to do when some user double clicks on ListView , gets the personel object fro

相关标签:
4条回答
  • 2021-01-29 07:02

    In the new form that will be opened, add a new property in the form class;

    private PERSONNEL Personnel{get; set;}
    public ShowPersonnel(PERSONNEL _personnel){
       this.Personnel = _personnel;
       //do whatever you want here
    }
    

    In the main form;

    private void listView1_SelectedIndexChanged(object sender, EventArgs e){
            PERSONNEL personnel = listView1.SelectedItems[0].Tag as PERSONNEL;
            Form2 form2 = new Form2();
            form2.ShowPersonnel(personnel);
            form2.Show();
    
    }
    

    May include typos. Change PERSONNEL to PERSONEL.

    0 讨论(0)
  • 2021-01-29 07:08

    You could probably just add a public PERSONEL property to the form, which you would then set in your SelectedIndexChanged event handler. Then any code that has access to your selector form could access your custom selected PERSONEL property.

    0 讨论(0)
  • 2021-01-29 07:11
    • One way is to have a public propery as Factor Mystic has suggested.
    • Or you could have a parametrized ctor and pass Personnel object. Although, this might create some problem with the design view of the form in Visual Studio.
    0 讨论(0)
  • 2021-01-29 07:12

    You should be able to set the display member of the listview control. Berfore you enter the for loop do something like:

    list.DisplayMember = "Name"
    

    Then bind the object.

    list.DataSource = query.ToList()
    

    The selected item will give you the object you've binded...

    MessageBox.Show(((PERSONEL)list.SelectedItem).Name);
    

    This was how it worked in .net 2.0. But I am sure there is probably a way to do this in 3.0 and greater...

    0 讨论(0)
提交回复
热议问题