0

I need to add listboxes to a windows forms application (C#) at application runtime. the issue I am having is I do not know how many boxes will be added... The application is going to check a specific path for dir's, and each dir currently listed needs to be a separate listbox... is there a way to do this? I currently am attacking it by creating 7 listboxes and making them visible or non visbile based on number of Dir's... but theres a chance of more than 7...

any help is much appreciated!

thanks

2
  • 1
    Something like this.Controls.Add(new ListBox()); Commented Nov 20, 2013 at 20:08
  • Why don't you just make it work the way Windows Explorer does this? Any user will understand having a TreeView of directories on the left, list on the right. Commented Nov 20, 2013 at 21:55

1 Answer 1

1

Just to expand on Habib's answer, you'd create a dynamic ListBox, popuplate it, then add it to some container:

ListBox lb = new ListBox();
// populate "lb" somehow
this.Controls.Add(lb);

The above code can be inside a loop.

You'd need to explicitly set a Location() for each ListBox, though, unless you added them to something like a FlowLayoutPanel which would arrange them automatically for you. If you need better control of how they are arranged take a look at using the TableLayoutPanel. You could dynamically change the number of rows/columns in it based on the number of directories you find.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.