Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / August 2007

Tip: Looking for answers? Try searching our database.

How do I disable vertical scroll bar in a listbox?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Morris Neuman - 23 Aug 2007 23:22 GMT
Is there a way to prevent a vertical scroll bar to appear in a listbox if
there are more items then can show in the listbox?  I know you can set it to
alway be visible but I want the opposite, I want to never show them.
Signature

Thanks
Morris

Linda Liu [MSFT] - 24 Aug 2007 08:11 GMT
Hi Morris,

When there're more items in a ListBox that can show in the ListBox, a
vertical scroll bar will appear automatically. This is a default behavior.

In fact, we could use the Win32 API function SetWindowLong to remove the
vertical scroll bar, if present, within a ListBox.

However, even if we use the SetWindowLong function to ensure a ListBox
doesn't have a vertical scroll bar at present, the vertical scroll bar
would appear again if the user navigates the items by pressing the down
arrow key. So we need to remove the veritcal scroll bar in the ListBox's
SelectedIndexChanged event handler or in the override
OnSelectedIndexChanged method.

In addition, I bind a ComboBox and a ListBox to a same data source in my
test and I find that if I select the last item from the ComboBox so that
the selected item in the ListBox changes also, the vertical scroll bar
appears instantaneously in the ListBox.

I use Spy++ to monitor the Windows messages that the ListBox receives in
the above case and I find two messages: WM_PRINT and WM_PRINTCLIENT. If I
filter these two Windows messages, the vertical scroll bar never appears in
this case.

The following is a sample code for you.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyListBox:ListBox
   {
       int WM_PRINT = 0x0317;
       int WM_PRINTCLIENT = 0x0318;

       int WS_VSCROLL = 0x00200000;
       int GWL_STYLE = -16;
       int SWP_FRAMECHANGED = 0x0020;
       int SWP_NOMOVE = 0x0002;
       int SWP_NOSIZE = 0x0001;
       int SWP_NOZORDER = 0x0004;

       [DllImport("user32.dll")]
       static extern int GetWindowLong(IntPtr hWnd, int nIndex);
       [DllImport("user32.dll")]
       static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
       [DllImport("user32.dll")]
       static extern bool SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

       protected override void WndProc(ref Message m)
       {
           if (m.Msg == WM_PRINT || m.Msg == WM_PRINTCLIENT)
           { }
           else
           {
               base.WndProc(ref m);
           }          
      }

       private void SetNonVerScrollbar()
       {
           int style = GetWindowLong(this.Handle, GWL_STYLE);
           style = style & ~WS_VSCROLL;
           SetWindowLong(this.Handle, GWL_STYLE, style);
           SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE |
SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
       }
       
       protected override void OnSelectedIndexChanged(EventArgs e)
       {  
           SetNonVerScrollbar();
           base.OnSelectedIndexChanged(e);      
           
       }
   }

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Morris Neuman - 24 Aug 2007 18:26 GMT
Thank you so much for your setailed response I will try out your suggestions.
Signature

Thanks
Morris

> Hi Morris,
>
[quoted text clipped - 101 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Morris Neuman - 24 Aug 2007 20:26 GMT
I incorporated your code in my project and then ran it via the debugger What
I see if the vertical scroll bar first when the form comes up but and
manipulation of the listbox cause them to disappear.
So how do I prevent the vertical scroll bar from showing up initially?
I tried to add a constructor for my class that calls SetNonVerScrollbar
           public MyListBox()
           {
               SetNonVerScrollbar();
           }
but that did not help.
Signature

Thanks
Morris

> Hi Morris,
>
[quoted text clipped - 101 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeff Gaines - 25 Aug 2007 00:05 GMT
On 24/08/2007 in message

>I incorporated your code in my project and then ran it via the debugger
>What
[quoted text clipped - 7 lines]
>             }
>but that did not help.

Linda may give a more detailed reply but it may be because at that point
the form is not visible and perhaps the handle has not been created. You
could run it once on activation by setting a bool variable as a flag (e.g.
if (!blnActivated) SetNonVerScrollbar(); blnActivated = true;) or override
the on HandleCreated event and try it there.

Signature

Jeff Gaines

Morris Neuman - 25 Aug 2007 03:44 GMT
Jeff
Thanks alot.  I'm a newbee on this form coding.  What member function do
overide to run it at activation?  And do I have to call the base class
activation function first?
Signature

Thanks
Morris

> On 24/08/2007 in message
>
[quoted text clipped - 15 lines]
> if (!blnActivated) SetNonVerScrollbar(); blnActivated = true;) or override
> the on HandleCreated event and try it there.
Jeff Gaines - 25 Aug 2007 09:53 GMT
On 25/08/2007 in message

>Jeff
>Thanks alot.  I'm a newbee on this form coding.  What member function do
>overide to run it at activation?  And do I have to call the base class
>activation function first?

With the From visible and selected in the designer click the events button
(lightning strike icon) in the properties window. Scroll up to 'Activated'
and double click, the IDE will set up a function prototype for you.
There's no need to call the base class in this case.

The IDE will also help with the OnHandleCreated prototype, which looks
like this:

protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
        }

If you start typing 'protected override' then you'll get a popup that will
auto complete it for you. In thats case you do need to call the base
function but auto complete will put it in for you.

Let the IDE take the strain :-)

Signature

Jeff Gaines

Morris Neuman - 25 Aug 2007 16:32 GMT
Thanks so much for your patient reply.  I will try it out.
Signature

Thanks
Morris

> On 25/08/2007 in message
>
[quoted text clipped - 21 lines]
>
> Let the IDE take the strain :-)
Linda Liu [MSFT] - 29 Aug 2007 04:54 GMT
Hi Morris,

Sorry for my delayed reply!

I tried my sample code and did see the problem you described. To solve this
problem, we could call the SetNonVerScrollbar method of the derived ListBox
AFTER we add some items into the ListBox. For example,

private void Form1_Load(object sender, EventArgs e)
{
   this.myListBox1.Items.Add("item1");
   this.myListBox1.Items.Add("item2");
   this.myListBox1.Items.Add("item3");
   this.myListBox1.Items.Add("item4");
   this.myListBox1.Items.Add("item5");
   this.myListBox1.SetNonVerScrollbar();  // of course we need to make the
SetNonVerScrollbar method public first
}

However, the vertical scroll bar would appear again if we add or delete an
item to the ListBox later. So it is not a good solution to call the
SetNonVerScrollbar method every time we add or delete some items to the
ListBox.

In fact, when an item is added to a ListBox, the ListBox receives the
LB_ADDSTRING Windows message and when an item is removed, the ListBox
receives the LB_DELETESTRING Windows message. So we could catch these two
Windows messages in the override WndProc method of the derived ListBox and
call the SetNonVerScrollbar method to remove the vertical scroll bar.

The following is a sample.

class MyListBox:ListBox
{
      ....
      int LB_ADDSTRING = 0x0180;
      int LB_DELETESTRING = 0x0182;
      protected override void WndProc(ref Message m)
       {
           if (m.Msg == WM_PRINT || m.Msg == WM_PRINTCLIENT)
           { }
           else if (m.Msg == LB_ADDSTRING || m.Msg == LB_DELETESTRING)
           {
               base.WndProc(ref m);
               this.SetNonVerScrollbar();
           }
           else
           {
               base.WndProc(ref m);
           }          
      }
      ...
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Linda Liu [MSFT] - 31 Aug 2007 11:06 GMT
Hi Morris,

How about the problem now?

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.