[英]Function that takes name of textBox as argument
我想为Windows窗体创建一个函数,例如:
private void do(textBox_name, string text)
{
textBox_name.Text=text.toString();
}
该函数应将textBox
名称作为参数,然后将文本写入正确的textBox。
但是我不知道如何正确发送应该使用哪个文本框。
目前,我已经在“ if语句”中完成了此操作,但是添加新元素是一次又一次地编写脚本,因为我必须编写
textBox1.Text=text.toString();
然后textBox2.Text=text.toString();
对于每个需要花费大量时间的文本框。 另外,我知道我可以使用开关来做到这一点,就像这样:
switch(ID)
case 1:
textBox1.Text=text.toString();
break;
case 2:
textBox2.Text=text.toString();
但是我想有一种方法可以为每个textBox做一个功能。
我有一个问题-为什么要在string
上调用ToString()
方法? 完全没有必要。
您的函数应如下所示:
private void do(TextBox tb, string text)
{
tb.Text = text;
}
如果使用double
s,则将略有不同:
private void do(TextBox tb, double number) //use meaningful names, "text" for double is poor choice
{
tb.Text = number.ToString();
}
再次欢迎。 问题已经解决,很容易,所以我真的很尴尬,我一直在寻找解决问题的时间。 我不知道我可以发送到TextBox的函数参数。 现在看起来像
private void write(TextBox tbox, string text) { tbox.Text=text; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.