Dimo
Dimo

Reputation: 3280

Adding controls to Listbox

I know I can use listBox.Controls.Add(new Button()); but I need to add several controls as rows, preferably from a List and set it as datasource. I have tried the following with no success:

var list = new List<Control>();

list.Add(new Button());
list.Add(new Button());
list.Add(new Button());

listBox1.DataSource = list;

Upvotes: 3

Views: 5745

Answers (3)

Dimo
Dimo

Reputation: 3280

I have decided to use a flowLayoutPanel instead. This seems like the best option right now.

Upvotes: 4

Vitaliy
Vitaliy

Reputation: 485

I would advise you to use StackPanel instead of Listbox you can read about here also, you must add Button object in you list and in StackPanel like this:

Button b = new Button();
list.Add(b);
stackPanel.Children.Add(b);

so you can work with you buttons in stack panel via list

Upvotes: -2

Michael27
Michael27

Reputation: 159

A ListBox is not designed to be a container control. Its scrollbar cannot scroll the controls. It is in general something you want to avoid, putting a lot of controls in, say, a Panel whose AutoScroll property is True will make your UI unresponsive. Controls are expensive objects.

Upvotes: 1

Related Questions