473,513 Members | 3,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Do I Add Items and SubItems to a Selected ListView Row?

6 New Member
Hello,

In my application I have a form with a ListView control. I have three columns and I add text to each of those columns from three text boxes using a single Click Event.

This works fine as long as I want to append the new items and subitems to the last line of my listView control. However, what I need sometimes is to edit the items and subitems at a specific row in the list.

For example, I would like to select a certain row, add the items and subitems from the text boxes and have it add those items to that line, not the last line.

My guess is that it could be done by identifying the index of a particular row and sending the items and subitems to that row based on the index. I expect that if there is already some item or subitem at that location, adding a new one will overwrite the existing one.

I know how to select a row and determine the index but I cannot figure out how to add the items to a row of that particular index.

Can anyone help me?


This is my code:


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItems.Click
  4.  
  5.         Dim strItem As String
  6.         strItem = TextBox.Text
  7.  
  8.         Dim strItem1 As String
  9.         strItem1 = TextBox1.Text
  10.  
  11.         Dim strItem2 As String
  12.         strItem2 = TextBox2.Text
  13.  
  14.  
  15.      Dim item1 As New ListViewItem(strItem)
  16.      ListView1.Items.AddRange(New ListViewItem() {item1})
  17.      item1.SubItems.Add(strItem1)
  18.      item1.SubItems.Add(strItem2)
  19.  
  20.  
  21.         TextBox.Clear()
  22.         TextBox1.Clear()
  23.         TextBox2.Clear()
  24.  
  25.     End Sub
  26.  
  27.  
  28.  
  29.  
  30.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.         ListView1.GridLines = True
  32.         ListView1.View = View.Details
  33.  
  34.  
  35.         ListView1.Columns.Add("Item Column", 150)
  36.         ListView1.Columns.Add("Column 2", 100)
  37.         ListView1.Columns.Add("Column 3", 100)
  38.         ListView1.Font = New Font("times", 14)
  39.         ListView1.FullRowSelect = True
  40.  
  41.     End Sub
  42.     Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
  43.         'Inserts selected ListView Item to the designated text box when the listview item is selected
  44.         If ListView1.SelectedItems.Count > 0 Then
  45.  
  46.             Dim selectedItem As String
  47.             selectedItem = ListView1.SelectedItems(0).Text
  48.             TextBox.Text = selectedItem
  49.         End If
  50.  
  51.         Dim subitem1 As String
  52.         subitem1 = ListView1.SelectedItems(0).SubItems(1).Text()
  53.         TextBox1.Text = subitem1
  54.  
  55.         Dim subitem2 As String
  56.         subitem2 = ListView1.SelectedItems(0).SubItems(2).Text()
  57.         TextBox2.Text = subitem2
  58.  
  59.         End Sub
  60.  
  61.  
  62.  
  63.  
  64.  
  65.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  66.  
  67.         Dim index As Integer = ListView1.SelectedIndices(0)
  68.         txtIndex.text = index
  69.  
  70.     End Sub
  71. End Class
  72.  
Dec 24 '12 #1
4 11954
PsychoCoder
465 Recognized Expert Moderator Contributor
This is VB.NET so I'll go a head and move this to the VB.NET forum for you so it can be seen by the proper experts :)
Dec 25 '12 #2
CyberSoftHari
487 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. Dim tmpListItem As ListItem ' Declaration should be out of add list function
  2. Set tmpListItem  = UIlstItem.ListItems.Add(intIndex,strKey ,strDisplayText)
  3. tmpListItem.SubItems(fieldIndex) = strDisplayString2 
  4.  
Above code format is an vb6 code and you can try this for VB.Net
Dec 25 '12 #3
Gary108
6 New Member
@CyberSoftHari
Hello. Thank you for replying. I do not fully follow your code. Please explain the meaning of intIndex, strKey, and strDisplayText.

Also, could you explain "fieldIndex and strDisplayString2? Thank you.
Dec 27 '12 #4
CyberSoftHari
487 Recognized Expert Contributor
UiListitems-> user interface listitems
strDisplayText-> text to display in list items.
intInex->row index(number)
intFieldIndex->column index(number)
strKEY->key value for row
Dec 28 '12 #5

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

Similar topics

1
1637
by: Angelina | last post by:
Hi, I wanted to add a few items to my listview control. The values of these will be obtained from other controls on my interface. When the user clicks the select button, they will need to be added to my listview. This is what i need help doing. I have added the columns to the listview but i now want to add the values that correspond to...
9
6094
by: Eva | last post by:
Hi, I wanted to know how i can enter values into a specific column of a listview. I have tried the following code but this seems to enter all my values into the first column!!! Can anyone please help me out on this?? hers my code so far.....
0
2544
by: Samuel R. Neff | last post by:
I'm having a index problem with ListView SubItems. If I add multiple columns to the listview and then add items with associated subitems, the ListView displays fine. Then if I delete a column via ListView.Columns.RemoveAt() then the ListView still displays fine. However, if I grab a subitem value via listView.Items.SubItems.Text Then I...
7
34765
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards Martin Arvidsson
3
15686
by: Bry | last post by:
I know you can select a single item in a list view using the following code listView.Items.Selected = true; Is it possible to do this for all items in the listView control without using a loop? Thanks.
0
1210
by: V&G | last post by:
Is there some way to specify different HorizontalAlignment values for caption and subitems in listview?
7
1459
by: CJ Taylor | last post by:
I have the following code. Dim lItem As ListViewItem For Each lItem In Me.lv_Search.Items lItem.Selected = True Next
3
2599
by: Holmes | last post by:
Hello Ran into a bit of a problem here and have now exhausted my resources to getting this working What I am trying to do is load and show a simple vb form with a listbox in it Dim frm_nc_code As New frm_nc_sen frm_nc_code.Show( Well what I want to have happen is it loads the form then shows all of the controls on the form (especially...
3
1419
by: Julian Milano | last post by:
I'm using the following code to act on only the selected items in a listview. The items are selected via checkboxes: Dim indexes As ListView.SelectedIndexCollection = .SelectedIndices For Each i In indexes ActiveXFileName = .Items(i).ToString Call RegisterActiveXFile(ActiveXFileName) Next For some reason it does not work. Can someone...
4
6073
by: Mark Rae | last post by:
Hi, I have a bog-standard ListView control in a v1.1 C# WinForms project, with three colums, Details View. There is "Select All" button under the ListView control with the following code: foreach(ListViewItem lvi in lvBackupFiles.Items) {
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
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...
0
7543
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...
0
5703
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...
0
4757
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...
0
3252
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
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
0
470
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...

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.