locked
custom Listview Scrollbar RRS feed

  • Question

  • Hi!

    I've created an vertical scrollbar, overrided the Paint event and 'n stuff, it all works very well. But here is the crux. How can I use this somewhat modified scrollbar in an ListView? Is there any way to use my new ScrollBar in my ListView?

     

     

    //Thanks, luntarn

    • Moved by CoolDadTx Wednesday, June 29, 2011 1:32 PM Winforms related (From:Visual C# General)
    Tuesday, June 21, 2011 6:44 PM

All replies

  • So I tried the "SetScrollInfo" method I found. The problem is that it does move the original Scrollbar, but does NOT update the ListView, second of all the fact that I still have to show the original scrollbars is... well I don't like that =)

     

        [DllImport("user32.dll")]
        static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
    
        [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
        static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
    
        public void ScrollTo(int Position)
        {
          SetScrollPos((IntPtr)this.listView.Handle, 0x1, Position, true);
          PostMessage((IntPtr)this.listView.Handle, 0x115, 4 + 0x10000 * Position, 0);
        }
    

    Anyone?

    Tuesday, June 21, 2011 8:44 PM
  • Hi LuntarN,

    Based on your descirption, you can use the following code to add your customer scrollbar into the ListView.

    this.listView1.Scrollable = false;
    
          CustomerVScrollBar vs = new CustomerVScrollBar();
          vs.Dock = DockStyle.Right;
          this.listView1.Controls.Add(vs);

    If any misunderstood, please feel free let me know. Thanks.

     


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 23, 2011 5:53 AM
  • Hi Vin Jin and thanks for your reply! -Well, no not exactly. I wonder how to make my listView scroll whenever I scroll mu CustomVScrollbar, in short "change Windows-default scrollbar in listview to my custom one".

     

    I made some experiment on SendMessage to "user32.dll" like;

          if (oldScrollValue < scrollBarOverride1.Value)
          {
            SendMessage(listViewObject.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
            oldScrollValue = scrollBarOverride1.Value;
          }
          else
          {
            SendMessage(listViewObject.Handle, WM_VSCROLL, SB_LINEUP, 0);
            oldScrollValue = scrollBarOverride1.Value;
          }
    


    But this didn't really work. So I tried to call for the "EnsureVisible", but this has to enable "Scrollable" in ListView, and all the ugly Windows-default scrollbars are back! :(

    Maybe I can hide the Vertical and Horizontal scrollbars? A workaround? -It feels as there has to be some way of changing the scrollbar in an ListView!

     

     

    //Thanks!

    Thursday, June 23, 2011 3:43 PM
  • *bump* Anyone?
    Sunday, June 26, 2011 5:37 PM
  • Post the relative code of your overriden ListView that applies to your ScrollBar.
    Sunday, June 26, 2011 8:35 PM
  • Hi LuntarN,

    >"change Windows-default scrollbar in listview to my custom one".

    Sorry for late response. But I cannot fully understand what do you mean of that. As you have created your customer scrollbar and you want to use the scrollbar instead of the ListView's default scrollbar, then why do you want to get the default scrollbar's value and set it to customer scrollbar?

    As you can use the code I posted to bind the customer scrollbar into the listview and set the LiseView's Scrollable to false as to remove the default scrollbar, so it can behaviour like as the default scrollbar.

    Also, you can handle the cutomer scrollbar's Scroll event to get the value.

    vs.Scroll +=new ScrollEventHandler(vs_Scroll);
    
        void vs_Scroll(object sender, ScrollEventArgs e)
        {
          int value1 = e.OldValue;
          int value2 = e.NewValue;
        }
    So if any minunderstanding, plese feel free to let me know and specific it in detail. Thanks.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 6:31 AM
  • Thanks Vin Jin for your answer! Sorry if I've been unclear in my explanation. Let my try again.

    This is what I've done so long:
    I have an ListView, lets call it ListView1 why don't we :). The ListView1 has some type of default AutoScroll, this is all very well, but what I wish to do is replace Windows default scrollbar with something new. So I created a new component class (lets call it CustomScroll) that inherited from Windows VScrollBar.

    I was not sure how to override the VScrollBar object so I guessed a bit and overrided the OnPaint method. I added some graphics, a background and a scroll-thumb. Time to get the scroll-thumb moving. I overrided some OnMouse events to to get the calls whenever someone tried to move the mouse and holding right-muse-button. Next step I overrided the get/set method of VScrollBar Value. And took the mouse.Y value as CustomScroll's new Value.

    In the OnPaint method I added calls to the events of ValueChanged and Scroll as follows:

          if (ValueChanged != null)
            ValueChanged(this, new EventArgs());
          if (Scroll != null)
            Scroll(this, new EventArgs());
    

    All this works very well. Now the only thing to do is to get things scroll'n using my new CustomScroll component.

    I don't really know if I done this properly, but I tried to look at some examples found on CodeProject, but as there examples only intended to generate new Values to a Panel components AutoScrollPosition, this couldn't possibly work for my ListView component as there is no AutoScrollPosition function. So I tried with everything I could think of, including sending calls to User32.dll (as seen above). This didn't work, so I tried a simpler method:

    ListView1.TopItem = ListView1.Items[CustomScroll.Value];
    
    In "CustomScroll_Scroll". This did work somewhat okay I guess, but still not perfect. It feels as thou I making it harder than necessary(?!).

     

    There you have it, I hope I described it well (I'm not good at this stuff, explaining =)

    Any clue if I'm doing it wrong or if there are some light in the dark. Thanks for reading!

    Monday, June 27, 2011 4:47 PM
  • Sounds like a lot of work for a simple task.  Why not add a standard VScrollBar to the ListView and disable the ListView's scrolling?  Track the Value with the ListView's TopItem.
    Monday, June 27, 2011 5:29 PM
  • Sounds like a lot of work for a simple task.  Why not add a standard VScrollBar to the ListView and disable the ListView's scrolling?  Track the Value with the ListView's TopItem.
    Yes, but the whole point is to make the ScrollBar somewhat more fancy. In my OnPaint override I have made some modifications as to the appearance of the scrollbar, it no longer looks like Windows Default - the crux is how to make ListView listen to it's scroll event.
    Monday, June 27, 2011 5:34 PM
  • I simply would have attacked the problem differently.  I would have gotten the scrolling working first and then made it pretty.
    Monday, June 27, 2011 6:18 PM
  • I simply would have attacked the problem differently.  I would have gotten the scrolling working first and then made it pretty.

    Thats all very well, but I don't really see how that tip helps... anyhow, my CustomScrollbar works, it gives me a new scroll Value - that has never been the problem so we can move the focus from specific details on how I overrided the Scrollbar, to the question of how I can use it to scroll a ListView. To make it all simpler, think of it in this scenario:

    You add a ListView to your form, the ListView contains enough items to make it call for the Scrollbars to appear. Now, think that you add a separate VScrollBar component. How would you do to make this separate VScrollBar act as an scrollbar to your ListView? Thats really the question as I then can apply it to my real situation.

     

    Thanks

    Monday, June 27, 2011 9:25 PM
  • See my prior post.
    Monday, June 27, 2011 9:36 PM
  • See my prior post.

    Okay, that is (also described above) how I do stuff now - new TopItem indications that is. Curious question, is there any other way how to create a scroll function to a ListView?

    Monday, June 27, 2011 9:47 PM
  • You should re-think about your request. Just as JohnWein said, you have made it more complex as based on your description it is a simple question.

     


    Please Mark it as answer, if it helps solve your problem.
    Wednesday, June 29, 2011 8:47 AM