1

I am adding user control to listbox. Everything works fine but i have a problem that when i add 10 user controls then i am expecting that listbox should show scroll so that i can select items at the end of listbox but this never happens even after setting show scroll property to true.

here is my code

UserControl1 button = new UserControl1();

button.Location = new Point(10, 100 * i + 10);
button.Size = new System.Drawing.Size(560, 59);
button.MessageUsername = "Wao this is great";
listBox1.Controls.Add(button);

I am open to ideas

here is the picture you can see that there is no scroll enter image description here

ColinE
  • 68,894
  • 15
  • 164
  • 232
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

2 Answers2

2

Where do people get the idea that a ListBox is a container control? That's not what a ListBox is for, it's not designed to hold child controls added with the Controls.Add method. You're supposed to add (and otherwise manage) the items it displays using its aptly-named Items property.

There's no scroll bars visible because you've just overlaid each of the child controls on top of one another. The ListBox itself doesn't know about them, so it doesn't know that it needs to scroll them.

You should be using some type of panel control, either the Panel control itself, or better yet one that will manage layout for you automatically. Investigate the FlowLayoutPanel or TableLayoutPanel. All of those controls have an AutoScroll property you can set to "True" that will automatically show the scrollbars whenever their contents exceed the visible client area.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

ListBox is not a container! For customize listbox items you should use ownerdraw draw mode. But in you case it wouldn't be right. As variant I can suggest you to use simple panel and isolated scroll box which would move (scroll) each control on panel

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69