Removing (Disabling) the veritical scrollbar in a listbox

Hi, I have an Owner drawn listbox ive been working on and would like to keep it from showing a vertical scrollbar when the amount of items in it exceeds the client area. However, the listbox control doesnt seem to have a way to disable this scrollbar by default...Is there any way (perhaps API) to keep that scrollbar from coming up? If anyone knows, please reply! Thanks.
[372 byte] By [codefund.com] at [2007-12-16]
# 1
You can disable the scrollbars by overriding the CreateParams function in your listbox:

protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle ^= 0x00200000L; //WS_VSCROLL
return cp;
}
}

I've tested this for WS_HSCROLL, but not WS_VSCROLL, so if I'm wrong feel free to flame me for my lazyness.

Have a nice day,
Robert W. Grubbs

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2
When I attempt to use it first I had a conversion problem (cant convert long to int implicitly) so I added a cast (int) to 0x00200000L but then I get an Error creating window handle. Is there anyway to fix this?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
Hi,

First you should not call CreateParams.ExStyle but CreateParams.Style property because WS_VSCROLL is not an extended style. And using ^(XOR) is not the best approach to remove that style (if it isn't set initially then this operation will asign it).
Removing the L(which stands for "long") from the value will solve your problem:

CreateParams cp = base.CreateParams;
cp.Style &= ~0x00200000; //WS_VSCROLL
return cp;

That should do it.

Cheers,
Gogou

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4
For some reason, I cant seem to access the CreateParams for the listbox within the usercontrol. I have the usercontol's form (ie, CdListbox) and a listbox within the usercontrol. But I cant seem to get the listbox to access CreateParams...perhaps Im either dumb or I just am not paying attention?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5
Heh, 'tis what I get for not testing, but at least you got the general idea
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 6
Hi,

To override CreateParams property you need to inherit from ListBox because CreateParams is protected - visible only for inheritors:

public class MyListBox : ListBox
{
public MyListBox()
{
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~0x00200000; //WS_VSCROLL
//cp.ExStyle ^= 0x00200000;
return cp;
}
}
}

Then just construct an object of MyListBox and you will not have vertical scrollbar.

Cheers,
Gogou

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...