List View Selected Index

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1961 Views - Last Post: 16 May 2014 - 12:08 PM Rate Topic: -----

#1 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

List View Selected Index

Posted 14 May 2014 - 11:59 AM

I have a lisview that I have created that loads information into the program from selected files. I am now trying to add in a search button to the information typed in so that it highlights (selects) the line of code that information is found in (Entire line for both columns) Multiselect is turned on.

The part I am stuck with is if it was a normal listbox I would use selectedindex but with Listview this method does not seem to work and only has selectedIndexCollection or selected items.

If I try something like these they dont seem to work and want more information added

List.SelectedIndexCollection = Lines.text
List.SelectedItems = Lines.text


Is This A Good Question/Topic? 0
  • +

Replies To: List View Selected Index

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: List View Selected Index

Posted 14 May 2014 - 12:17 PM

Are you talking about programatically selecting the line in the listview?
Was This Post Helpful? 0
  • +
  • -

#3 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 14 May 2014 - 12:35 PM

View PostCharlieMay, on 14 May 2014 - 12:17 PM, said:

Are you talking about programatically selecting the line in the listview?


Yes, I am trying to find a way to write code to make it so when I type something into the textbox hit search it finds any wording relative to the search and highlights that specific line of code as a typical "Search" feature does; but highlight the entire row for both column not just a sub column
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: List View Selected Index

Posted 14 May 2014 - 12:37 PM

You could use a for loop and use the iterator variable as the index for the items property
lvw.items(<index>).Selected = True


This should select the item at the specified index.

EDIT:
I'm pretty sure you need to set the focus to the listview after you've ran through the loop.

Or if you don't want to give the listview focus, at the very least set HideSelection to False

This post has been edited by CharlieMay: 14 May 2014 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 14 May 2014 - 01:01 PM

View PostCharlieMay, on 14 May 2014 - 12:37 PM, said:

You could use a for loop and use the iterator variable as the index for the items property
lvw.items(<index>).Selected = True


This should select the item at the specified index.

EDIT:
I'm pretty sure you need to set the focus to the listview after you've ran through the loop.

Or if you don't want to give the listview focus, at the very least set HideSelection to False



This code confuses me in the sense that as I try to find a way to implement this to select the coding any lines of text left below this is instantly greyed out any nothing else works any long I am not really sure what to even do about that as I have never used this "Index" coding before so I am assuming that there is a way to turn it off but I just dont even know where to begin on that
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: List View Selected Index

Posted 14 May 2014 - 01:12 PM

Probably time to show the code you've attempted to implement this with.

The <index> is a placeholder to show that this in a number that corresponds to the index in the collection (an integer)

Since a For..Next loop can use an Integer as a iterator variable, they are a typical way of passing a "Changing" index number to a method.

It's a pretty straightforward process

Example:
ListView1.Items(0).Selected = True would hilight the 0th item in the list. And since indexes in lists start at 0 this is the same thing as saying Select the first item in the listview

Keep in mind that Selecting is not necessarily the same thing as hilighting in the listview as I said earlier. To show this you either need to set the HideSelection or set the focus(). Without this, you won't see it but your selecteditems collection would still contain the items.
Was This Post Helpful? 0
  • +
  • -

#7 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 14 May 2014 - 01:37 PM

View PostCharlieMay, on 14 May 2014 - 01:12 PM, said:

Probably time to show the code you've attempted to implement this with.

The <index> is a placeholder to show that this in a number that corresponds to the index in the collection (an integer)

Since a For..Next loop can use an Integer as a iterator variable, they are a typical way of passing a "Changing" index number to a method.

It's a pretty straightforward process

Example:
ListView1.Items(0).Selected = True would hilight the 0th item in the list. And since indexes in lists start at 0 this is the same thing as saying Select the first item in the listview

Keep in mind that Selecting is not necessarily the same thing as hilighting in the listview as I said earlier. To show this you either need to set the HideSelection or set the focus(). Without this, you won't see it but your selecteditems collection would still contain the items.



Genius! with this you actually assisted me with fixing my other issue I was trying to figure out. However still not quite sure how to get this to focus quite right.

    Private Sub Search_Click(sender As System.Object, e As System.EventArgs) Handles Search.Click
        Dim T As String
        T = Lines.Text
        Do Until List.FocusedItem = T
            List.FocusedItem = List.Items(0)
        Loop
    End Sub

I feel like I am on the right track for this to solving this mystery but now I get error code
Overload resolution failed because no accessible '=' can be called with these arguments:
Was This Post Helpful? 0
  • +
  • -

#8 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 14 May 2014 - 02:06 PM

I edited this up some more to try to get closer and it wont let me edit my last post but I recoded this and I feel that this should work however it doesnt like my +1 part

    Private Sub Search_Click(sender As System.Object, e As System.EventArgs) Handles Search.Click

        Do Until List.FocusedItem.Text = Lines.Text
            List.FocusedItem = List.Items(0) + 1
        Loop
    End Sub

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: List View Selected Index

Posted 14 May 2014 - 02:08 PM

OK, I was under the assumption that you had the search feature working and just wanted to select the found items. It appears that you haven't.

With this method you would probably need to loop through each row and then inside that loop, loop through each column. Once you find a .Text that matches your search then you would call the .Selected using the iterators for the proper index.

There is a FindItemWithText method of the listview that searches subitems but it appears it only works off of finding text that starts with your search criteria.

[Pseudocode]
For r as Integer = 0 to row count -1
   For c as Integer = 0 to column count -1
     If listview.items(r).SubItems(c).text = <your search string> then
        'use r for the items index to set selected = true
     end if
   next
next

Was This Post Helpful? 0
  • +
  • -

#10 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 15 May 2014 - 12:37 PM

I am creating a loop to search for a matching text on my listview subitems equal to what is typed into my textbox when I hit the search button Below is what I have but I get syntax error with the 0 and cant convert string



        Dim T As String
        T = Lines.Text
        For r As Integer = 0
            For C As Integer = 0
                If List.Items(r).SubItems(C).Text = Lines.Text Then
                    List.FocusedItem = T
                End If
            Next
        Next

    End Sub

Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13389
  • View blog
  • Posts: 53,429
  • Joined: 12-June 08

Re: List View Selected Index

Posted 15 May 2014 - 12:39 PM

03	    For r As Integer = 0
04	        For C As Integer = 0


FYI - typically you need a 'to' unless you are using a 'for each'.

http://msdn.microsof...y/5z06z1kb.aspx
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13389
  • View blog
  • Posts: 53,429
  • Joined: 12-June 08

Re: List View Selected Index

Posted 15 May 2014 - 12:48 PM

Aaaaaand let's not create duplicate topics. Merging.
Was This Post Helpful? 0
  • +
  • -

#13 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 15 May 2014 - 12:52 PM

View Postmodi123_1, on 15 May 2014 - 12:48 PM, said:

Aaaaaand let's not create duplicate topics. Merging.


Ahh tahts where it went.. wasnt sure how to find my old topic after closing it last night to go to bed...

Anyway so I can understand that simple error as to why that would not have worked not really sure what number I should put here honestly since the length depends on the size of the file uploaded so any length really so I just wrote it like this see what happens even though I have a feeling I should probably right +1 instead if that even works.
But still leaves my confusion as to how to set focus when it doesnt like the covnert to string

        Dim T As String
        T = Lines.Text
        For r As Integer = 0 To -1
            For C As Integer = 0 To -1
                If List.Items(r).SubItems(C).Text = Lines.Text Then
                    List.FocusedItem = T
                End If
            Next
        Next

    End Sub

Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: List View Selected Index

Posted 15 May 2014 - 12:59 PM

In the main VBNet forum listing, on the black bar that reads VB.Net there is a link to Topics you personally started and a link to Topics you replied to. You could also click on your username and there are buttons in your profile to find your topics.

OK the listview has Rows (items) and Columns this is what you are looping through.

Both of these have a count property. Since indexes start at 0 you need to subtract 1 from the total count

ListView1.Items.Count will tell you the number of rows your listview contains
ListView1.Columns.Count will tell you the number of columns your listview contains

Using that number-1 as the max number of your for loop will ensure that your index reaches all rows and all columns respectively (assuming your start at 0)

This post has been edited by CharlieMay: 15 May 2014 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#15 Nevaeha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 09-November 13

Re: List View Selected Index

Posted 15 May 2014 - 01:50 PM

I certainly understand a little more of this as to how it would cycle does this look about appropriate? I at least no longer get an error, or a failure when hitting the search button, still doesnt work with the focus but I feel Ia m at least ALMOST there I dont understand why I am having such a ridiculous hard time with a Listview versus as Listbox that was super easy to figure out...



    Private Sub Search_Click(sender As System.Object, e As System.EventArgs) Handles Search.Click

        For r As Integer = List.Items.Count To List.Items.Count - 1
            For C As Integer = List.Columns.Count To List.Columns.Count - 1
                If List.Items(r).SubItems(C).Text = Lines.Text Then
                    List.FocusedItem.Selected = Lines.Text
                End If
            Next
        Next

    End Sub

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2