help on winform listview scrollbar

csharp United States
  • 12 years ago
    The listview has vertical scrollbars and the user clicks the scroll bar to do a page down or page up, the grid lines are drawn through the items on the page down, and page up. What kind of event will fire if the scroll bar scrolls. Actually, I would like the load the data to the listview as a chunk. For example, I have 10000 items, and I only load 100 items one time and when the user scroll down the scroll nod, load the next block of 100 items, etc. Yeah, the data might stored in 'cache'. Do any one know what kind of event should fire for scrolling the scroll nod? Is here any one did the similar function before for the listview. I have to use listview, because I need to display image for each item.

    Thanks and any help is greatly appreciated.

    Tanyuk
  • 12 years ago

    Hi Tanyuk


    There maybe a better way to do this if there is 1 of the site guru's will probly come and correct but this is how i would do it.


    1) Add a VScrollBar to your form next to your ListView.
    2) Set the VScrollBar min and max values in proportion to your amount of items in ListView.
    3) Set The VScrollBar initial value to 1.
    4) Add an Event Handler for VScrollBar.ValueChanged.
    5) Within you event handler get the value of the VScrollBar by casting the sender object back to a VScrollBar.

    Code:
    int myValue = ((VScrollBar)sender).Value;

    6) Reload your values into your ListView according to this Value.


    Hope this makes sense and is helpfull. Any comments or advances welcome

  • 12 years ago

    Hi Lee,


    Thanks for the solution!
    However, I did tried the steps that you provided and still have some question on this.
    After I added the VScrollBar next to the listview, and set the scrollable of listview to "false", this way, there should not be 2 scroll bars appear. And I added a dummy messagebox for debug and show the value of the rows that scrolled the event raised for the VScrollBar value changed, code is like:

    Code:
    int myValue = ((VScrollBar)sender).Value;

    Code:
    MessageBox.Show((string)myValue.ToString());


    This way, the messagebox will show "95" as myValue when I scrolled the nod down first time. So. I guess it already scrolled down 95 rows. However, how to make the rows re-draw after the scrollbar move? What I mean is the items should be changed, since the user already scroll down to the next page. Can you please provide me some code for this?


    Thanks again for your help.


    Tanyuk

  • 12 years ago

    Hi Lee,


    If I set the scrollable of listview to "false", the event of VScrollBar value change will not bound to the listview. So I set it back to "true" and moved the VScrollBar inside the listview to cover the default scrollbar build in with the listview. Now, the rows got re-draw if the user scroll down or the up the nod. Thanks.


    Tanyuk

  • 12 years ago

    So have you sorted this out then ?


  • 12 years ago

    Hi Lee,


    However, when the VScrollBar_valueChanged event raised by the user, how the listview knows which rows should re-draw? If the data was loaded from the file or database, shall I load the data to "cache" first? How to look inside the code? Have you heard about virtual listview?


    Thanks
    Tanyuk

  • 12 years ago

    Hi Lee,


    I found a problem in my UI. I have 1000 items and I set the max value of VScrollBar to 1000, and min value to 1. However, it only display 991 items in listview control. Is there anything I might missing in terms of setting? Not sure why it only display 991 items? If I make the VScrollBar not visible, then the default scrollbar will show and display 1000 items in the listview.


    Thanks.


    Tanyuk

  • 12 years ago
    Hi Tanyuk

    You will probly need to send me some code for me to see, then i can have a look at the problem.

    But i do notice this, you say you set your maxvalue of the box to 100 because you have 100 items? are you only displaying 1 item at a time in your listview box?

    If you are displaying 10 items per clcik then maxvalue = 1000/10;

    i am fairly busy this morning but i will have a look and write some dummy code for you later this afternoon if you have not already sorted it.

  • 12 years ago

    Hi Tanyuk


    i had a look at this and also came across a similar problem, i beleive the problem to be, the maxvalue of your Vscrollbar is never  reached this may be because of the thickness of the scroller, and the value it taken from the top position. Here are the adjustments i made to make mine work


    Code:

    //Before 1000 items (10 displayed in listview per time)
    //Only shows 919 Items
    MinValue =0;
    MaxValue=100
    SmallChange=1
    Large Change=10


    //Adjusted maxValue to 108 (Now)
    //Now Shows all 999 Items (0 index)
    MinValue =0;
    MaxValue=108
    SmallChange=1
    Large Change=10



    So i beleive the problem is that the VScrollBar can never reach its actual MaxValue, so you have to adjust it up utill it reaches your required amount.

  • 12 years ago
    Hi Lee,

    You are right, the thickness of the vScrollBar caused the problem. I have to set MaxValue to 1008 if I want to display 1000 items in the listview. I understand that we set the Large Change to 10 for display 10 items per time, however, if I use 108 as MaxValue, it only can display 100 items. So I changed the Max Value to 1008, and it works fine now.

    By the way, have you ever tried "on-demanding data loading"? Use the scroll bar to determine which data need to be display. So, I might need to display certain number of rows first, and use the scroll event to fill other data when the user scroll down the nod. This is only use when I a have huge amount of data need to be displayed. Any idea on this?

    Thanks again.

    Tanyuk

  • 12 years ago

    replied to email, if it were you that sent the code


  • 12 years ago

    Hi Lee,


    I have sent you mail with the code which I want your help on it. However, the email was failure in delivery, so I re-send it again. Please check your inbox. Thanks and really appreciated your help.


    Tanyuk

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The difference between theory and practice is smaller in theory than in practice.”