none
Can't make listview ensurevisible cause scroll to selected row/item RRS feed

  • Question

  • Simple routine where the user selects a drive to be used and that drive.name  (X:\) is saved in a setting for the next use.  On the next use I attempt so select the last used drive.name and cause it to be scrolled into view.  I have several other programs and doing similar things with listview and they all are working.  I have compared and the code and setup for listview is the same.  Looking for any hints. See the code and the setup for the listview below.   The proper line in the listview is selected.  

    With AvailableDrivesListView
                .Columns.Clear()
                .GridLines = True
                .View = View.Details
                .Columns.Add("Drive", -2, HorizontalAlignment.Left)
                .Columns.Add("Space Available", -2, HorizontalAlignment.Left)
                .FullRowSelect = True
                .Visible = True
                .Enabled = True
                .CheckBoxes = False
                .Scrollable = True
                .AutoArrange = True
                .HideSelection = False
                .MultiSelect = False
                .Sorting = SortOrder.None
                .Refresh()
            End With 
    
    

    Code doing the selection and ensurevisible. Reversing the order of the two key lines of code make no difference.  

    If My.Settings.AvailableDrivesListViewText <> "" Then
                    For i = 0 To AvailableDrivesListView.Items.Count - 1
                            If AvailableDrivesListView.Items.Item(i).Text = My.Settings.AvailableDrivesListViewText Then
                            AvailableDrivesListView.EnsureVisible(i)
                            AvailableDrivesListView.Items.Item(i).Selected = True
                            Exit For
                        End If
                    Next i
                End If



    Gene


    • Edited by Shady Brady Hat Thursday, September 3, 2020 7:46 PM left out a line in initial comment.
    Thursday, September 3, 2020 7:44 PM

Answers

  • The following I test in a C# project.

    When the form shows the code (which I converted to VB.NET) below finds a value, gets it's index and traverse to it and ensures it's visible.

    Dim item = ownerContactListView.FindItemWithText("Santé Gourmet")
    
    If item IsNot Nothing Then
    	Dim index = ownerContactListView.Items.IndexOf(item)
    	ownerContactListView.Items(index).Selected = True
    	ownerContactListView.EnsureVisible(index)
    End If
    
    ActiveControl = ownerContactListView


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 3, 2020 8:48 PM
    Moderator
  • Hi Karen, 

    I am marking your solution as the answer.  Thanks.  While it doesn't explain why the simple solution that  I have been using for years and is published in this forum many times no longer works, your solution is both simpler and it gets me moving again.  I will have to look harder at my code in the current project to fined what the difference is from other projects that work properly.  Thanks again. 


    Gene

    Hi Gene,

    I can't tell why what works on your other projects fail in this project. All I can say is the find method I recommended focuses solely on a ListView and is actually very flexible. When working with controls the first thing I look for are methods that do what I want in the control e.g. ComboBox FindString, FindStringExact.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, September 4, 2020 4:02 PM
    Moderator

All replies

  • Hi

    Untested but worth a try: *Also, maybe without the .Contains and change to =

    If AvailableDrivesListView.Items.Item(i).Text.ToLower.Contains(My.Settings.AvailableDrivesListViewText.tolower) Then
    
    End If
    the idea being that maybe if different cases no is match made.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, September 3, 2020 8:45 PM
    • Proposed as answer by KHURRAM RAHIM Sunday, November 22, 2020 3:55 PM
    Thursday, September 3, 2020 8:44 PM
  • The following I test in a C# project.

    When the form shows the code (which I converted to VB.NET) below finds a value, gets it's index and traverse to it and ensures it's visible.

    Dim item = ownerContactListView.FindItemWithText("Santé Gourmet")
    
    If item IsNot Nothing Then
    	Dim index = ownerContactListView.Items.IndexOf(item)
    	ownerContactListView.Items(index).Selected = True
    	ownerContactListView.EnsureVisible(index)
    End If
    
    ActiveControl = ownerContactListView


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 3, 2020 8:48 PM
    Moderator
  • Thanks Leshay.  It isn't a problem of it being there.  It is there because it comes up selected. It is just the scroll that isn't happening.   You can find examples all over this forum using below to cause the last item (just added for instance) is displayed. 
    AvailableDrivesListView.EnsureVisible(AvailableDrives(AvailableDrivesListview(count- 1) 


    Gene

    Thursday, September 3, 2020 11:01 PM
  • Thanks Karen,

    I will try that but the item is coming up selected in the code I provided and thus it had to have matched the search being done in the code shown just above that.  My problem is that this is code that was working and is still working in several other programs built under the previous release of Visual Basic.  I will have to take one of them and build it again to see if they then fail in the same manner.  I will let you know what happens.  Thanks. 


    Gene

    Thursday, September 3, 2020 11:05 PM
  • Hi Karen, 

    I am marking your solution as the answer.  Thanks.  While it doesn't explain why the simple solution that  I have been using for years and is published in this forum many times no longer works, your solution is both simpler and it gets me moving again.  I will have to look harder at my code in the current project to fined what the difference is from other projects that work properly.  Thanks again. 


    Gene

    Friday, September 4, 2020 3:31 PM
  • Hi Karen, 

    I am marking your solution as the answer.  Thanks.  While it doesn't explain why the simple solution that  I have been using for years and is published in this forum many times no longer works, your solution is both simpler and it gets me moving again.  I will have to look harder at my code in the current project to fined what the difference is from other projects that work properly.  Thanks again. 


    Gene

    Hi Gene,

    I can't tell why what works on your other projects fail in this project. All I can say is the find method I recommended focuses solely on a ListView and is actually very flexible. When working with controls the first thing I look for are methods that do what I want in the control e.g. ComboBox FindString, FindStringExact.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, September 4, 2020 4:02 PM
    Moderator
  • I don't know why this came back this AM.  Was marked as answered on September 4, 2020 and still has an answered status as far as I can tell.  In any case, will marked your last comment as the answer and see what happens then.  Thanks.  

    Gene

    Sunday, November 22, 2020 4:59 PM