Windows窗体在运行时动态添加文本框 [英] windows form adding textbox at runtime dynamically

查看:90
本文介绍了Windows窗体在运行时动态添加文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我可以使用以下代码在运行时动态添加页面上的文本框:-

hi all ,

I am able to add textbox on page at run time dynamically using code like:-

Textbox objtxtBox = new Textbox();
objtxtBox.name = "txtFName";



我可以在单击按钮时显示新的文本框,但是我想在单击另一个按钮时获得该文本框的值,但是无法使用相同的文本框名称属性获取该值.




I am able to display new textbox on button click but i want to get the value of that textbox on the click o another button, but unable to get the value using same textbox name property.


how to do this.

推荐答案

创建控件后,是否将控件添加到Form.Controls集合中?
Did you add the control to the Form.Controls collection after you created it?


您可以通过以下代码获取文本框值:

You can get your text box value by the following code:

Control cc = this.Controls.Find(txtFName, true).First();
string value = cc.Text;



希望对您有帮助
Theingi Win



Hope be helpful
Theingi Win


可能会有所帮助,

Might be helpful,

private void btnCreate_Click(object sender, EventArgs e)
{
     TextBox txtBox = new TextBox();
     txtBox.Name = "txtFName";
     txtBox.Text = "Hello world";
     Controls.Add(txtBox);
}
private void btnFind_Click(object sender, EventArgs e)
{
     TextBox rc = (TextBox)FindDynamicControlByName("txtFName");
}
private Control FindDynamicControlByName(string controlName)
{
      return Controls.Find(controlName, true).FirstOrDefault();
}



对于参考:
Control.Controlcollection.Find [ ^ ]

:)



For Ref:
Control.Controlcollection.Find[^]

:)


这篇关于Windows窗体在运行时动态添加文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆