且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#在面板内部创建动态控件

更新时间:2021-12-25 04:16:12

我要做的第一件事就是设计它应该做的看起来像所有信息都已填写完毕.执行此操作时,您可能希望将希望同时填写的控件分组在一起.

我不知道您使用的是WinForms,WPF,ASP.NET还是什么,但是我可以想到三种通用方法来获取所需的动态控件填充.

1.创建一个托管要一起显示的控件的控件.然后在运行时创建并将其插入到UI容器中.

2.创建一个控件,以承载要一起显示的控件或将它们承载在同一容器中,然后将该控件或容器放置在UI中.在设计器中将其收起(控件/容器),并在适当的时候使它在代码中再次可见.

3.使用具有可折叠行或列的表或网格,当折叠时,它们也将隐藏行或列的内容.如果这是WPF,我将使用Grid控件,并命名行/列定义或绑定行/列定义的大小.当需要显示控件时,使行/列对用户可见.

方法1将按照您的要求动态创建控件.方法2和3会让您看起来像是动态创建的控件,即使它们始终存在.
The first thing I would do is design what it should look like once all the information has been filled in. When doing this, you might want to group controls that are expected to be filled in at the same time together.

I don''t know if you are using WinForms, WPF, ASP.NET or what, but I can think of three general ways get the dynamic control fill-in that you want.

1. Create a control that hosts the controls you want to show up together. Then create and insert it into the UI container at run time.

2. Create a control that hosts the controls you want to show up together or host them in the same container, place the control or container in the UI. Collapse it (the control/container) it in the designer, and make it visible again when in code once the time is right.

3. Use a table or grid with collapsible rows or columns, that will also hide the contents of the rows or columns when collapsed. If this were WPF, I would use a Grid control, and I would either name the row/column definitions or bind the sizes of the row/column definitions. When it''s time to show the controls, make the row/column visible to the user.

Method 1 will create controls dynamically, like you asked. Methods 2 and 3 will just make it look like you created the controls dynamically even though they''re always there.