简体   繁体   English

在运行时动态添加列表框

[英]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.

相关问题 在运行时动态添加动态属性 - Dynamically adding dynamic properties at runtime 在运行时将网格添加为ListBox的项最终会导致未知错误/崩溃 - Adding a grid as an item of a ListBox at runtime ends up with an unknown error/crash 在动态运行时将代码添加到方法的开头/结尾 - Adding code to the beginning / end of methods in runtime dynamically 在运行时动态地将按钮添加到Tab控件上下文 - dynamically Adding buttons to a Tab Control context at runtime 在运行时期间动态地向TableLayoutPanel添加控件 - Adding controls to TableLayoutPanel dynamically during runtime 将XML项目动态添加到列表框时遇到问题,C#? - Problem dynamically adding XML items to listbox, C#? WPF / C#-向在列表框内动态创建的按钮添加功能 - WPF / C# - Adding functionality to a button dynamically created inside a listbox 在列表框中动态添加值,然后尝试在“提交”按钮上获取值 - Adding the values dynamically in the Listbox and then trying to get the value on the submit button 如何在运行时通过在WPF中使用拖放功能将列表框动态添加到“包装”面板 - How to add Listbox Dynamically to Wrap panel at runtime by using Drag and Drop in WPF 如何在运行时刷新ListBox? - How refresh a ListBox at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM