简体   繁体   中英

how to use selected index changed event in multiple listviews in single windows form?

I am working with a simple address book in which i have separate lists for companies and persons on a single window form. I have a problem when i selected an item from first listview it shows values for item from second list..i am beginner in programming languages. I also have a single list and a single xml file to save contacts for both listviews. Could some1 help me...? thanx in advance. i have this code in Selected index changed event

  try
  {
       textBox1.Text = contacts[listView1.SelectedItems[0].Index].FullName;
       textBox2.Text = contacts[listView1.SelectedItems[0].Index].Address1;
       textBox3.Text = contacts[listView1.SelectedItems[0].Index].Address2;                    
       textBox5.Text = contacts[listView1.SelectedItems[0].Index].Phone1;
       textBox6.Text = contacts[listView1.SelectedItems[0].Index].Phone2;
       textBox7.Text = contacts[listView1.SelectedItems[0].Index].Phone3;
       textBox8.Text = contacts[listView1.SelectedItems[0].Index].Email;
       textBox11.Text = contacts[listView1.SelectedItems[0].Index].Website;

   }
   catch { }

ad other list have

    textBox1.Text = contacts[listView2.SelectedItems[0].Index].FullName;                
     textBox3.Text = contacts[listView2.SelectedItems[0].Index].Address2;
     textBox4.Text = contacts[listView2.SelectedItems[0].Index].MailingAddress;
     textBox5.Text = contacts[listView2.SelectedItems[0].Index].Phone1;                
     textBox8.Text = contacts[listView2.SelectedItems[0].Index].Email;
     textBox11.Text = contacts[listView2.SelectedItems[0].Index].Website;
     textBox9.Text = contacts[listView2.SelectedItems[0].Index].ContactPerson;
     textBox10.Text = contacts[listView2.SelectedItems[0].Index].ContactPhone;

(please dont laugh i am just a beginner:))

well both peices of code are pulling from the same contacts array.. which you are accessing by INDEX.. and keyed to the INDEX of the selected listviewitem . That means that when you select the first item in listview1 you will get an index of ZERO .. and when you select the first item in listview2 you will get an index of ZERO .. effectively giving you the same contacts element.

I would recommend setting the . Tag property of each listviewitem as you populate it to the contact that it represents (eg. listviewitem item = listview1.items.add(contacts[index].FullName; item.Tag = contacts[index]; )

Then you can ask for the contact by reference in your selecteditemchanged event: contact contact = listview1.selectedItems[0].Tag;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM