[Resolved]Trouble with Listview Subitems
I am sure I have totally screwed this up, but here is my code. I am trying to loop through a listview control, storing the listitems and all of the subitems.
Code:
Dim SurgProcedures(frmaddforms.lvSurgProcedures.items.count) as String
Dim frm As frmAddForms
With frm
For i As Integer = 0 To frm.lvSurgProcedures.Items.Count - 1
SurgProcedures(i) = frm.lvSurgProcedures.Items.Item(i)
SurgDate = frm.lvSurgProcedures.Items.Item(i).SubItems(0)
SurgName = frm.lvSurgProcedures.Items.Item(i).SubItems(1)
Surgphone= frm.lvSurgProcedures.Items.Item(i).SubItems(2)
SurgFacility = frm.lvSurgProcedures.Items.Item(i).SubItems(3)
SurgPostOp = frm.lvSurgProcedures.Items.Item(i).SubItems(4)
Next
End With
I have SurgDate, SurgName, etc. declared as strings. Thanks in advance for any direction.
Re: Trouble with Listview Subitems
As a starter, why do you declare frm locally and what type is a frmAddForms?
Re: Trouble with Listview Subitems
Hello,
In addition to the above question, what issue are you actually having? Is there an error? If so, what is the error message? Also, how are you populating the listview in the first place?
Gary
Re: Trouble with Listview Subitems
Code:
Dim frm as frmAddforms
That code is basically just saying to use "frm" as "frmAddforms". frmAddforms is the name of the form, but it's a lot quicker to type "frm".
I am populatiing the listview from textboxes on the same form. Here is the code I am using to populate the listview control:
Code:
Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String, i As Integer, AlreadyPresent As Boolean = False
'remove leading and trailing spaces from the entries and store as a variable
strSurgProcedure = Trim(txtSurgProcedure.Text)
strSurgPerformed = Trim(dtpSurgPerformed.Text)
strSurgName = Trim(txtSurgName.Text)
strSurgPhone = Trim(mtxtSurgPhone.Text)
strSurgFacility = Trim(txtSurgFacility.Text)
strSurgPostOp = Trim(txtSurgPostOp.Text)
'set lvitem to the value of the SurgProcedure string
lvItem.Text = StrConv(strSurgProcedure, VbStrConv.ProperCase)
'update the string
strSurgProcedure = lvItem.Text
'Set the value of the lvitem subitems
With lvItem
.SubItems.Add(strSurgPerformed)
.SubItems.Add(strSurgName)
.SubItems.Add(strSurgPhone)
.SubItems.Add(strSurgFacility)
.SubItems.Add(strSurgPostOp)
End With
For i = 0 To lvSurgProcedures.Items.Count - 1
If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
AlreadyPresent = True
MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
'clear all textboxes
txtSurgProcedure.Text = ""
txtSurgName.Text = ""
txtSurgFacility.Text = ""
txtSurgPostOp.Text = ""
'set the focus back on the first text box
txtSurgProcedure.Focus()
Exit For
End If
Next
If Not AlreadyPresent Then
'add the item to list
lvSurgProcedures.Items.Add(lvItem)
'clear all textboxes
txtSurgProcedure.Text = ""
txtSurgName.Text = ""
txtSurgFacility.Text = ""
txtSurgPostOp.Text = ""
'set the focus back on the first text box
txtSurgProcedure.Focus()
Re: Trouble with Listview Subitems
Hey,
Using the intellisense window should negate the need to do the aliasing that you are doing. Simply type frm and then ctrl + space (if the intellisense window doesn't appear) and you should be able to use the full name directly from there by simply hitting tab, or period, to start accessing the properties and methods of the form.
Also, you still haven't mentioned exactly what problems you are having.
Gary
Re: Trouble with Listview Subitems
I am having problems storing the subitems of the listview items.
Re: [Resolved]Trouble with Listview Subitems
Hey,
What exactly seems to be the problem?
I have taken your code and pasted it into a new project, and aside from a few small tweaks, I have been able to use it and display the contents of the listview sub items.
One of the tweaks that I had to do though was this:
Code:
Dim SurgDate As String = Me.lvSurgProcedures.Items.Item(i).SubItems(0).Text
Since it is actually the subitem text that you are interested in, not information about the subitem itself.
Also, on another note, rather than declaring frm As FormName, to get access to the controls within the form, just use Me.lvSurgProcedures.
Hope that helps!!
Gary