473,809 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting a list entry based on listview selection

8 New Member
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 contextmenustri p 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
2 12072
Maraj
24 New Member
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
Scalp994
8 New Member
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
4901
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 a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
1
2127
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). Both ASCXs have DataGrids on them. The first user control's DataGrid is populated from our database on page load. The DataGrid also has a Select LinkButton.
0
1541
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 the listview and be able to delete or editing record in that list. Please help Ralph
0
2139
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 Sales 1 Monthly Terms 1 Sales 2 Annual Terms
1
2758
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 remaining map value for the existing hostnames. My sample code as follows. typedef struct _CAAMT_Credentials {
6
5559
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 enabled only if something is selected in the listview. That part specifically works but not very well. It seems that I can only get the buttons to disable if I click off the text of the items in the listview but still within about 10-20 pixels of...
5
12673
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 info in statusbar). When selecting one item (clicking on listview) it works fast, without noticing. However if selecting multiple items with SHIFT (approx. 500 items) or selecting with mouse, for each item selected through all listview an event...
2
2140
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, 2) Floppy, 3) CD Note : If a user selects Cartridges then list of printer should display, And if floppy or CD is selected, then 'purpose' text item should display. And many many more,
1
3066
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' method to 'post' method. can u all help me or guide me how to change this code. thank you <?php $state = $county = null; //declare vars
3
5363
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 display a filtered version of the data based on the selection from the combo box. For example, the combo box has a list of different projects, (a, b, c, and d) and the list box has a list of ALL of the reports associated with all of the projects. ...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.