Is there a way to tell if the form load is complete so I can have run
when it's done. beacuase when I just put something at the end of the
form load it doesn't continue loading until that code is done. Which
some times can take a while.
OK My thought is that your probably trying to address sub items which you havent created for the record.
I have a little example here. When you click button1 it sets up the listview and adds data for 2 records. You are adding the subitems to a collection of the listviewitem. So if you have 3 subitems then you can refer to them.
So in the second record being added you will notice it works - now comment out those initial .SubItems.Add("") Statements and you will notice that the x.SubItems(0).Text = "New1" lines will now fail.
Why Because they are referring to items which do not exist in the subitems collection. You need to look at this as a collection and not an array.
I think you will need to look at what items you are actually populating in the listview.
The second button will display the contents of a specified item. In this case the 1st record which has all 4 columns populated it will work. Now with those commented out subitems commented so it is only populating the item and no subitems and change the line to MsgBox(ListView1.Items.Item(1).SubItems.Item(2).Text) and this will now fail as there are no subitems for this record now.
Hope this helps and I'm certain that this is what your issue is.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '//Setup the Listview Me.ListView1.View = View.Details Me.ListView1.Columns.Add("test1") Me.ListView1.Columns.Add("test2") Me.ListView1.Columns.Add("test3") Me.ListView1.Columns.Add("test4")
'//Add an Item Dim x As New ListViewItem With x .Text = "Test" .SubItems.Add("Test col2") .SubItems.Add("Test col3") .SubItems.Add("Test col4") End With ListView1.Items.Add(x)
'//Set using the blank template x = New ListViewItem With x .Text = "Second Record" .SubItems.Add("") .SubItems.Add("") .SubItems.Add("") End With ListView1.Items.Add(x)
'//Update those subitems With ListView1.Items(1) x.SubItems(0).Text = "New1" x.SubItems(1).Text = "New2" x.SubItems(2).Text = "New3" x.SubItems(3).Text = "New4" End With
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MsgBox(ListView1.Items.Item(0).SubItems.Item(2).Text) End Sub End Class
But you must have all the subitems populated to refer to them by index. You cant just say row 2 column 3. You must have populated at least to items and on that 2nd item you must have populated at least 2 subitems. If you had populated a 2nd item and only 1 subitem and tried to refer to 2nd row , column 3 then you will get the error.
This is not a grid - it is a means of displaying collections of items, subitems. You need to ensure the subitems are populated before you can refer to them.
Acually this is weird. Because I was using subItems as populating each
different column not subitems in one column. Thats the weird. Thing.
OOHHHH. I think I get it. All I want to do is change an already set
text under one column on a certain row. As you can see in my code above
thats what I was trying to do with the subitems. So how do I just
change text on a certain row under a certain column.
Hey I have another problem...not pertaining to this subject.... I'm
editing some text in my listview. I have 4 columns and I need to edit
the 4 one or vb reads it as 3. (1-4|0-3) But when I edit the '3' one,
and it gives me the an error saying that 3 is not a vaild index. I've
tryed editing the 2 and 1 and 0 one and it works fine. but the 3 one
always gives me an error. I've even tryed to add another column and
still got an error. Here is the line that gives me the error.
listview
ureyes84
OK My thought is that your probably trying to address sub items which you havent created for the record.
I have a little example here. When you click button1 it sets up the listview and adds data for 2 records. You are adding the subitems to a collection of the listviewitem. So if you have 3 subitems then you can refer to them.
So in the second record being added you will notice it works - now comment out those initial .SubItems.Add("") Statements and you will notice that the x.SubItems(0).Text = "New1" lines will now fail.
Why Because they are referring to items which do not exist in the subitems collection. You need to look at this as a collection and not an array.
I think you will need to look at what items you are actually populating in the listview.
The second button will display the contents of a specified item. In this case the 1st record which has all 4 columns populated it will work. Now with those commented out subitems commented so it is only populating the item and no subitems and change the line to MsgBox(ListView1.Items.Item(1).SubItems.Item(2).Text) and this will now fail as there are no subitems for this record now.
Hope this helps and I'm certain that this is what your issue is.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Setup the Listview
Me.ListView1.View = View.Details
Me.ListView1.Columns.Add("test1")
Me.ListView1.Columns.Add("test2")
Me.ListView1.Columns.Add("test3")
Me.ListView1.Columns.Add("test4")
'//Add an Item
Dim x As New ListViewItem
With x
.Text = "Test"
.SubItems.Add("Test col2")
.SubItems.Add("Test col3")
.SubItems.Add("Test col4")
End With
ListView1.Items.Add(x)
'//Set using the blank template
x = New ListViewItem
With x
.Text = "Second Record"
.SubItems.Add("")
.SubItems.Add("")
.SubItems.Add("")
End With
ListView1.Items.Add(x)
'//Update those subitems
With ListView1.Items(1)
x.SubItems(0).Text = "New1"
x.SubItems(1).Text = "New2"
x.SubItems(2).Text = "New3"
x.SubItems(3).Text = "New4"
End With
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox(ListView1.Items.Item(0).SubItems.Item(2).Text)
End Sub
End Class
apat
in my case
MsgBox(ListView1.Items.Item(0).SubItems.Item(2).Text)
is referring to 1st row - column 3
MsgBox(ListView1.Items.Item(1).SubItems.Item(2).Text)
is referring to 2st row - column 3
But you must have all the subitems populated to refer to them by index. You cant just say row 2 column 3. You must have populated at least to items and on that 2nd item you must have populated at least 2 subitems. If you had populated a 2nd item and only 1 subitem and tried to refer to 2nd row , column 3 then you will get the error.
This is not a grid - it is a means of displaying collections of items, subitems. You need to ensure the subitems are populated before you can refer to them.
Greg Johnson
KoKoNassar
The behaviour your describing sounds correct and by design that is the form load doesnt complete until all code within the form load event is run.
The following article describes the firing order of the form events and a little bit about there unpredictable firing order and ways around this.
http://www.knowdotnet.com/articles/eventfiringorder.html
Hopefully this will help.
john holland
Hey I have another problem...not pertaining to this subject.... I'm editing some text in my listview. I have 4 columns and I need to edit the 4 one or vb reads it as 3. (1-4|0-3) But when I edit the '3' one, and it gives me the an error saying that 3 is not a vaild index. I've tryed editing the 2 and 1 and 0 one and it works fine. but the 3 one always gives me an error. I've even tryed to add another column and still got an error. Here is the line that gives me the error.
ListView1.Items.Item(num2).SubItems.Item(3).Text = FormatSize(num)
This should sent the final format style (String) to the 4 column at 'num2' row. This line above it works just fine.
ListView1.Items.Item(num2).SubItems.Item(2).Text = ""
Can anyone figure out whats wrong Do I need to explain more
Steve Nye