User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 391,709 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,403 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser:
Views: 469 | Replies: 7 | Solved
Reply
Join Date: Apr 2008
Posts: 32
Reputation: ninjaimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ninjaimp ninjaimp is offline Offline
Light Poster

selecting in listview

  #1  
Jun 25th, 2008
Hi

i have a listview which is populated with a number of 'Appointments' and when selected this fills a number of lables with information.

now on the first click it works fine and everything gets populated but when a different row is selected i get an error.

the code im using is
Dim selection As ListViewItem = lstDays.GetItemAt(e.X, e.Y)


        If (selection IsNot Nothing) Then
            lblCustID.Text = selection.SubItems(2).Text
            FillCustData(selection.SubItems(2).Text)
        Else
            MsgBox("There is no current appointment set for " & selection.SubItems(2).Text & " Would you like to make one?")
            Return
        End If

the error is appearing on the row 'lblCustID.Text = selection.SubItems(2).Text '
with the error message
' InvalidArgument=Value of '2' is not valid for 'index'. Parameter name: index '

i wondered if anyone could help
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 366
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 61
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Whiz

Re: selecting in listview

  #2  
Jun 25th, 2008
To select ListItems you can use ListView1.SelectedItems. This is a collection of Selected List Items. You can refer first selected item as ListView1.SelectedItems(0)

if ListView1.SelectedItems.Count > 0 then
Dim selection As ListViewItem = ListView1.SelectedItems(0)
  ' Your Selected code
End If
Last edited by selvaganapathy : Jun 25th, 2008 at 12:12 pm.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: ninjaimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ninjaimp ninjaimp is offline Offline
Light Poster

Re: selecting in listview

  #3  
Jun 25th, 2008
many thanks for your response

i have now amended my code to:

If lstDays.SelectedItems.Count > 0 Then

Dim selection As ListViewItem = lstDays.SelectedItems(0)

If lstDays.SelectedItems(0).SubItems(1).Text = "" Then
MsgBox("no appointment set")
Else
lbltest.Text = lstDays.SelectedItems(0).SubItems(1).Text
End If

End If

but im still getting an error when i click a row that has now value in column 2.

Because column one will always contain a value (Time) = i am checking that if column 2 is empty, then display message, else fill lable.
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 366
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 61
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Whiz

Re: selecting in listview

  #4  
Jun 26th, 2008
Hi
This may help u.
Draw a ListView Control (ListView1)

Try the Following code
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Dim list As ListViewItem
  3. list = ListView1.Items.Add("One")
  4. list.SubItems.Add(" Sub Item 1")
  5. list = ListView1.Items.Add("Two")
  6. list.SubItems.Add(" Sub Item 2")
  7. ListView1.Columns.Add("Name")
  8. ListView1.Columns.Add("Address", 150)
  9. ListView1.View = Windows.Forms.View.Details
  10. ListView1.FullRowSelect = True
  11. End Sub
  12.  
  13. Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
  14. If ListView1.SelectedItems.Count > 0 Then
  15. Dim list As ListViewItem = ListView1.SelectedItems(0)
  16. MsgBox(list.Text & " " & list.SubItems(1).Text)
  17. End If
  18. End Sub
Last edited by selvaganapathy : Jun 26th, 2008 at 10:29 am.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: ninjaimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ninjaimp ninjaimp is offline Offline
Light Poster

Re: selecting in listview

  #5  
Jun 26th, 2008
hi

thanks for this.

my problem lies in the fact that the 'list.SubItems(1)' may be empty and i need to be able to detect this.

how do i find out if a subitem in a listview is empty?

regards
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 366
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 61
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Whiz

Re: selecting in listview

  #6  
Jun 26th, 2008
I understand the problem, Try this
  1. Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
  2. If ListView1.SelectedItems.Count > 0 Then
  3. Dim list As ListViewItem = ListView1.SelectedItems(0)
  4. 'Check whether subitems exists
  5. If list.SubItems.Count > 1 Then
  6. 'Here also check whether they are empty
  7. If list.SubItems(1).Text <> "" Then
  8. MsgBox(list.Text & " " & list.SubItems(1).Text)
  9. Else
  10. MsgBox("Incomplete")
  11. End If
  12. Else
  13. MsgBox("Incomplete")
  14. End If
  15. End If
  16. End Sub
Last edited by selvaganapathy : Jun 26th, 2008 at 10:42 pm.
KSG
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: ninjaimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ninjaimp ninjaimp is offline Offline
Light Poster

Re: selecting in listview

  #7  
Jun 27th, 2008
this looks like it should do it

i give it a go and let u know how i get on

many thanks for your help
Reply With Quote  
Join Date: Apr 2008
Posts: 32
Reputation: ninjaimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
ninjaimp ninjaimp is offline Offline
Light Poster

Re: selecting in listview

  #8  
Jun 27th, 2008
works like an absolute charm

many thanks for your help
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb VB.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 3:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC