Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView display problems

Status
Not open for further replies.

rickyoswald

Programmer
Jun 12, 2004
59
0
0
GB
I have a ListView control that is being populated via a loop from a database. The loop is quite straightforward:

.MoveFirst
i = 1
Do While i < .RecordCount
frmMain.lstMainContacts.ListItems(i).SubItems(4) = !_[Phone]
.MoveNext
i = i + 1
Loop

This works okay except the first record shows up blank in the list! I thought this may be because arrays tend to start on 0 and my variable is starting on 1, however when I change it to 0 I get the error "Index out of bounds". I have looked on google and here for answers but cannot find anything specific to this problem.
 
It is actually the last one that is not being displayed (the last record). I also pasted the wrong loop, the main one is:

Do While i < .RecordCount '+ 1
frmMain.lstMainContacts.ListItems.Add i
frmMain.lstMainContacts.ListItems(i).SubItems(1) = ![Surname]
frmMain.lstMainContacts.ListItems(i).SubItems(2) = ![Title]
frmMain.lstMainContacts.ListItems(i).SubItems(3) = ![FirstName]
.MoveNext
i = i + 1
Loop
 
Does VB code actually run sequentially? It appears to process a block of code (inside a loop or with etc) then return values to the begining of the loop, execute them then move to the next block. This means my loop at the END of my with statment is effecting the begining of the block of code!
 
Change your code to

Code:
Do While Not .EOF
   frmMain.lstMainContacts.ListItems.Add 
   frmMain.lstMainContacts.ListItems(i).SubItems(1) = ![Surname]
   frmMain.lstMainContacts.ListItems(i).SubItems(2) = ![Title]
   frmMain.lstMainContacts.ListItems(i).SubItems(3) = ![FirstName]
   .MoveNext
Loop
 
yes,, but I forgot to change something else... I apologize!

Code:
Dim lItem as ListItem

Do While Not .EOF
   Set lItem = frmMain.lstMainContacts.ListItems.Add 
   lItem.SubItems(1) = ![Surname]
   lItem.SubItems(2) = ![Title]
   lItem.SubItems(3) = ![FirstName]
   .MoveNext
Loop
 
The other thing to note is that with the code above the first column in the list will also be blank. Set set the text of the first column of each row do a

lItem.Text = "First Column Text
 
Coincidently I already have a first column which is blank and hidden away. This is beacuase the first column cannot have text aligned in the center!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top