R – listbox scrolling

listboxwinforms

I want to disable scrolling ( not hide the scroll bar but disallow scrolling altogether) when the user selects certain items in my listbox
if the user moves to a differnet item ( the criterai are not important ) then I want to re-enable scrolling
How can I do this in .NET 2.0

Best Answer

Override CreateParams property of ListBox class.

public class My : ListBox
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style &= ~0x00200000; // VScroll
            return cp;
        }
    }
}

Add following code to test,

private void Form1_Load(object sender, EventArgs e)
        {
            My c = new My();
            for (int i = 1; i <= 100; i++){
               c.Items.Add(i.ToString());
            }
            this.Controls.Add(c);
        }
Related Topic