[英]dynamically adding listbox at runtime
I need to add listboxes to a windows forms application (C#) at application runtime. 我需要在应用程序运行时将列表框添加到Windows窗体应用程序(C#)。 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...
我目前正在通过创建7个列表框并根据Dir的数量使它们可见或不可见来进行攻击...但是有可能超过7个...
any help is much appreciated! 任何帮助深表感谢!
thanks 谢谢
Just to expand on Habib's answer, you'd create a dynamic ListBox, popuplate it, then add it to some container: 只是为了扩展Habib的答案,您将创建一个动态ListBox,将其弹出,然后将其添加到某个容器中:
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. 但是,您需要为每个ListBox显式设置一个Location(),除非您将它们添加到FlowLayoutPanel之类的东西之后,它将为您自动安排它们。 If you need better control of how they are arranged take a look at using the TableLayoutPanel.
如果需要更好地控制它们的排列方式,请使用TableLayoutPanel。 You could dynamically change the number of rows/columns in it based on the number of directories you find.
您可以根据找到的目录数动态更改其中的行数/列数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.