473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deleting a list entry based on listview selection

Hey there!

I have a class called "student" which consists of a string "surname" and a string array "subjects". I also have a list of type "student" of all the students.

Expand|Select|Wrap|Line Numbers
  1. public class student
  2.         {
  3.             public static string surname;
  4.             public string[] subjects = new string[10];
  5.         };
  6.  
  7.         List<student> students = new List<student>();
  8.  
I have a button that adds a given student to the listview and creates a new object in the list. Let's say it works like this:
Expand|Select|Wrap|Line Numbers
  1.  private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             ListViewItem lvi = new ListViewItem(textBox1.Text);
  4.             lvi.SubItems.Add(subjects1);
  5. listView1.Items.Add(lvi);
  6.  
  7.  
  8.  students.Add(new student
  9.             {
  10.                 nazwisko = textBox1.Text,
  11.                 subjects1 = subjects
  12.             });
  13. }
  14.  
I'm now trying to add a code that, upon clicking on a contextmenustrip on the listview, would delete the student from BOTH the listview and the students LIST.

Expand|Select|Wrap|Line Numbers
  1. //upon clicking the remove button on a selected listview item
  2. if (listView1.SelectedItems.Count != 0)
  3.             {
  4.                 foreach (ListViewItem lvi in listView1.SelectedItems)
  5.                 {
  6.  
  7.                     lvi.Remove();
  8.                 }
  9.             }
However, I can't figure out how to remove THE SAME student from the students LIST. It's because I'd need to know the index of the student I'm selecting on listview, or I'd need to get the value of surname based on the selection. Any ideas how I can pull it out?
Feb 21 '12 #1

✓ answered by Maraj

Try this
Expand|Select|Wrap|Line Numbers
  1.  if (listView1.SelectedItems.Count != 0)
  2.  3.             {
  3.  4.                 foreach (ListViewItem lvi in listView1.SelectedItems)
  4.                   {
  5.    students.RemoveAt(listView1.Items.IndexOf(lvi));
  6.                       lvi.Remove();
  7.                   }
  8.               }
  9.  

2 12063
Maraj
24
Try this
Expand|Select|Wrap|Line Numbers
  1.  if (listView1.SelectedItems.Count != 0)
  2.  3.             {
  3.  4.                 foreach (ListViewItem lvi in listView1.SelectedItems)
  4.                   {
  5.    students.RemoveAt(listView1.Items.IndexOf(lvi));
  6.                       lvi.Remove();
  7.                   }
  8.               }
  9.  
Feb 21 '12 #2
Thanks a lot, it works like a charm now :)
Feb 23 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
1
by: thegame | last post by:
Filling One DataGrid Based on Selection from Another DataGrid - Both in Separate User Controls Hello, I have an interesting dilemma. I have an ASPX page with two user controls (two ASCXs). ...
0
by: Raphael | last post by:
Hi Everyone, I need some help in Editing and Deleting a record from a listview. Example- I have a list view that is populated from an Access Database, I will like users to click on any record in...
0
by: ROO | last post by:
Hi Everyone, I have a database table that have 4 field( C1, C2, M1, M2) on my form i have two combo box ComboC and ComboM C1 C2 M1 M2 1 ...
1
by: arv6004u | last post by:
Hi, I have created Map Data stacture. Each map Entry contains hostname and it's value. I have traversed the map using iterator then i want to delete specific hostname entry and to change the...
6
by: Brandon McCombs | last post by:
Hello, I have a form that contains a listview on the left side and a column of buttons on the right side. Only some of the buttons do I want enabled all the time. The other buttons should be...
5
by: Jure Bogataj | last post by:
Hi all! I have a problem (performance issue) with listview. I have implemented an ItemSelectionChange on my listview and put some code in it (I build some toolbar based on selection and update...
2
vikas251074
by: vikas251074 | last post by:
I am creating an application for official use. This application will be used by employees to take items for official use. I have a list, presently this list contains three items - 1) Cartridges, ...
1
by: amel86 | last post by:
hello.. i have a question about how to make dependent list menu based on mysql? actually, i already found some code that helped me a lot but there has a problem that is i want to change 'get'...
3
by: Lori Brynlund | last post by:
Hi, I'm trying to create a form (MS Access 2003) that currently has 2 items in it: a combo box and a list box. I'd like to be able to make a selection from the combo box and have the list box...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.