如何以编程方式在flowlayoutpanel中插入控件 [英] How to programatically insert a control in a flowlayoutpanel
问题描述
我想根据条件声明添加它
我尝试过:
flowLayoutPanel1.Controls.Add(控制名称);
I want to add it based on a conditional statement
What I have tried:
flowLayoutPanel1.Controls.Add(control name);
推荐答案
你必须首先以编程方式创建控件。
示例:
Hi,
you must first create the control programmatically.
Example:
var txt = new TextBox();
txt.name = "myNewTextBox";
然后你可以将它添加到你的flowLayoutPanel:
Then you can add it to your flowLayoutPanel:
flowLayoutPanel1.Controls.Add(txt);
注意:您不能使用在代码中引用该TextBox this.myNewTextBox
。 您需要保存对它的引用(全局变量或全局字典吗?)或从flowLayoutPanel1.Controls获取它的引用。您可能还需要处理文本框的名称:如果添加两个文本框对于你的flowLayoutPanel具有相同的名称,WinForm将为最后添加的一个名称分配一个不同的名称(myNewTextBox1?)。
示例:
Note: you cannot then refer to that TextBox in your code with this.myNewTextBox
. You need to either save a reference to it (global variable or global dictionary?) or get its reference from flowLayoutPanel1.Controls. You may also need to handle the names of the textboxes: if you add two textboxes to your flowLayoutPanel with the same name, WinForm will assign the last added one a different name ("myNewTextBox1" ?).
Example:
flowLayoutPanel1.Controls["myNewTextBox"].Text = "Hi!";
处理添加到FlowLayoutPanel的元素
如前所述,您需要管理文本框的名称(例如,通过使用计数器),然后在代码中检索TextBoxes FlowLayoutPanel.Controls
数组中的名称或索引。
示例:
Handle Elements added to FlowLayoutPanel
As previously stated, you need to manage the name of textboxes (for instance, by using a counter) and then retrieve the TextBoxes in code knowing either the name or their index in the FlowLayoutPanel.Controls
array.
Examples:
//I have 10 textboxes in my flowlayoutpanel and want to access the third one:
var txt3 = myFlowLayoutPanel.Controls[2];
//I want to refer to the textbox named "mySpecialTextBox"
var txtSpecial = myFlowLayoutPanel.Controls["mySpecialTextBox"]
附注:如有疑问,请咨询MSDN。在这种特殊情况下,请此处。
希望这会有所帮助。
LG
Side note: when in doubt, consult MSDN. In this particular case go here.
Hope this helps.
LG
非常感谢你。这对我帮助很大。我现在唯一的问题是如何设置新添加的控件的属性
Thank you so much. It helped me alot. The only problem i have it now is how to set the properties of the newly added control
如何识别添加到flowControlPanel的每个控件
How do i identify each of the controls added to the flowControlPanel
这篇关于如何以编程方式在flowlayoutpanel中插入控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!