Results 1 to 11 of 11

Thread: Navigating items on ListView with Horizontal Scroll Bar Control

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Navigating items on ListView with Horizontal Scroll Bar Control

    Hello again everyone.
    I'm having a rather simple problem, that's proving hard for me to understand a way to solve it...

    I have a listview with a number of items and I'd like to select them one by one with the aid of a horizontal scroll bar control.

    I've set the maximum and mininum values of the scroll bar like so:

    vb.net Code:
    1. hsbPlayListNavigator.Maximum = lvPlayList.Items.Count
    2.             hsbPlayListNavigator.Minimum = 0

    Note: The lvPlayList is the ListView control I'm using to store the items.

    Then I've got the following code for the Horizontal Scroll Bar scroll Event:

    vb.net Code:
    1. Private Sub hsbPlayListNavigator_Scroll(sender As Object, e As ScrollEventArgs) Handles hsbPlayListNavigator.Scroll
    2.         For i As Integer = 0 To lvPlayList.Items.Count - 1
    3.             If e.Type = ScrollEventType.SmallDecrement Then
    4.  
    5.             ElseIf e.Type = ScrollEventType.SmallIncrement Then
    6.  
    7.             ElseIf e.Type = ScrollEventType.First Then
    8.  
    9.             ElseIf e.Type = ScrollEventType.Last Then
    10.  
    11.             End If
    12.         Next
    13.     End Sub

    And this is where I'm stuck, because I don't have a clue on how to check which item is selected and how to go to the next item on the ListView control.
    There's no selecteditem property like on the ComboBox, so I'm not sure of what to do.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    which View do you use in your listview?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    do you have MultiSelect set to False?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    The property View is set to Details and yes I did set the MultiSelect property to false. Sorry, forgot to mention those x.x

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    there is a selecteditems property and a selectedindices property.
    if multiselect = false then listview.selectedindices(0) is the selectedindex

    edit: but it'll cause an error if there is no selecteditem

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    Hi,

    I see that .paul. is going the same way I was thinking so assuming that the MultiSelect property of the ListView is false and so long as you set the Scroll bar values correctly then you don’t need to do any of that. The first thing to note is that the Maximum values of the scroll bar should be set to:-

    vb.net Code:
    1. hsbPlayListNavigator.Maximum = lvPlayList.Items.Count-1

    Once done, you can simply say the following in the Scroll event of the Scroll Bar:-

    vb.net Code:
    1. With ListView1.Items(e.NewValue)
    2.   .Selected = True
    3.   .EnsureVisible()
    4. End With

    Hope that helps.

    Cheers,

    Ian

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    Thanks everyone for the replies. I just got home from work and I'm dead tired... I'll sweep through your suggestions tomorrow and will come back with some feedback.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    I'm back again with some vb.net goodness lol.

    IanRyder's code worked fine, I think.
    I'm not able to select any of the items on the ListView, but the first one. I went on debugging the scroll event and it seems that the NewValue property is always set to 0. I noticed that the scroll bar box is filling the whole control, which means that the max property is implicetly set to 0 (at least I think that's where the problem is).

    On debugging, I checked the max property value and it is set to 2, which is the current value of lvPlayList.Items.Count-1, so I'm not sure what the hell I'm missing.

    Here's a screenshot to better describe what's happening:

    Name:  1.jpg
Views: 395
Size:  22.8 KB

    Edit* Sorry about the image attachment. I don't know why the image is that small, on my computer it was saved at a larger size...
    Last edited by Simbiose; Jul 23rd, 2014 at 03:43 AM.

  9. #9
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    Hi,

    IanRyder's code worked fine, I think.
    Here are a couple of extra pointers for you:-

    When using the ListView, if you select something in code and the control does not have the focus then the selection is hidden. This can be overcome by setting the property HideSelection to False and even then the highlight can be difficult to see so it is often best to set the property FullRowSelect to True and then move the focus back to the ListView control to show the highlight in full. i.e:-

    vb.net Code:
    1. With ListView1.Items(e.NewValue)
    2.   .Selected = True
    3.   .EnsureVisible()
    4. End With
    5. ListView1.Select()

    I noticed that the scroll bar box is filling the whole control,
    As to the Scroll bar filling the control here is something I did not know. As I have done, the first thing you should always do if you see unexpected results is to read the documentation for the properties and methods you are using. In this case have a read here:-

    ScrollBar.Maximum Property

    Can you see the issue? Once found, you will then need to compensate for the ListView Item Index when scrolling.

    Hope that helps.

    Cheers,

    Ian

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    Quote Originally Posted by IanRyder View Post
    Hi,



    Here are a couple of extra pointers for you:-

    When using the ListView, if you select something in code and the control does not have the focus then the selection is hidden. This can be overcome by setting the property HideSelection to False and even then the highlight can be difficult to see so it is often best to set the property FullRowSelect to True and then move the focus back to the ListView control to show the highlight in full. i.e:-

    vb.net Code:
    1. With ListView1.Items(e.NewValue)
    2.   .Selected = True
    3.   .EnsureVisible()
    4. End With
    5. ListView1.Select()



    As to the Scroll bar filling the control here is something I did not know. As I have done, the first thing you should always do if you see unexpected results is to read the documentation for the properties and methods you are using. In this case have a read here:-

    ScrollBar.Maximum Property

    Can you see the issue? Once found, you will then need to compensate for the ListView Item Index when scrolling.

    Hope that helps.

    Cheers,

    Ian
    Aaaaah, now I'm getting places
    Thank you very much for the help, I am now able to select up 2 items, I'm still missing something, but I'm still trying to work it around.

    I have the following code so far:

    Initializers

    vb.net Code:
    1. lvPlayList.MultiSelect = False
    2.  
    3. lvTotalPages = Math.Floor((lvPlayList.Items.Count - 1) / 13) + 1
    4.  
    5. hsbPlayListNavigator.Maximum = lvPlayList.Items.Count - 1
    6. hsbPlayListNavigator.Minimum = 0
    7. hsbPlayListNavigator.SmallChange = 1
    8. hsbPlayListNavigator.LargeChange = (lvPlayList.Items.Count - 1) / lvTotalPages

    Scroll Event

    vb.net Code:
    1. If e.Type = ScrollEventType.SmallDecrement Then
    2.             With lvPlayList.Items(e.NewValue)
    3.                 .Selected = True
    4.                 .EnsureVisible()
    5.             End With
    6.             e.NewValue -= 1
    7.         ElseIf e.Type = ScrollEventType.SmallIncrement Then
    8.             With lvPlayList.Items(e.NewValue)
    9.                 .Selected = True
    10.                 .EnsureVisible()
    11.             End With
    12.             e.NewValue += 1
    13.         ElseIf e.Type = ScrollEventType.First Then
    14.             'Still need to think what I need to do here
    15.         ElseIf e.Type = ScrollEventType.Last Then
    16.             'Still need to think what I need to do here
    17.         End If

    Edit* Ok I got the scrollbar to scroll throughout every item on listview with the following code:

    Initializers

    vb.net Code:
    1. lvPlayList.MultiSelect = False
    2.  
    3. lvPlayList.MultiSelect = False
    4.  
    5.             lvTotalPages = Math.Floor((lvPlayList.Items.Count - 1) / 13) + 1
    6.  
    7.             hsbPlayListNavigator.Maximum = lvPlayList.Items.Count - 1
    8.             hsbPlayListNavigator.Minimum = 0
    9.             hsbPlayListNavigator.SmallChange = 1
    10.             hsbPlayListNavigator.LargeChange = (hsbPlayListNavigator.Maximum - 1) / lvTotalPages

    Scroll Event

    vb.net Code:
    1. If e.Type = ScrollEventType.SmallDecrement Then
    2.             With lvPlayList.Items(e.NewValue)
    3.                 .Selected = True
    4.                 .EnsureVisible()
    5.             End With
    6.         ElseIf e.Type = ScrollEventType.SmallIncrement Then
    7.             With lvPlayList.Items(e.NewValue)
    8.                 .Selected = True
    9.                 .EnsureVisible()
    10.             End With
    11.         ElseIf e.Type = ScrollEventType.First Then
    12.             'Still need to think what I need to do here
    13.         ElseIf e.Type = ScrollEventType.Last Then
    14.             'Still need to think what I need to do here
    15.         End If

    However, there's something that doesn't feel right... by checking the following line of code:

    vb.net Code:
    1. hsbPlayListNavigator.LargeChange = (hsbPlayListNavigator.Maximum - 1) / lvTotalPages

    I noticed that it's the same as having:

    vb.net Code:
    1. hsbPlayListNavigator.LargeChange = 1

    So... if I set SmallChange and LargeChange properties to 1, the scrollbar box adjusts to the same size as the number of items on the ListView control and I'm trying to make some sense of it and the only conclusion I can reach is that when I set the Maximum property as the total number of items on the ListView and since my logic is to slide the scrollbar according to each item on the ListView control, it's safe to say that each click on the Scrollbar arrows is equivalent to 1 unit of width measurement of the scrollbar box?

    I have a feeling that what I'm saying ins't easy to understand and I'm probably complicating things, but may anyone confirm if I'm right?
    Last edited by Simbiose; Jul 23rd, 2014 at 06:58 AM.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: Navigating items on ListView with Horizontal Scroll Bar Control

    Hello, just pointing out that there's still a question in the air here :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width