Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问多个按钮、文本框或任何控件?_C#_.net_Winforms_Textbox - Fatal编程技术网

C# 如何访问多个按钮、文本框或任何控件?

C# 如何访问多个按钮、文本框或任何控件?,c#,.net,winforms,textbox,C#,.net,Winforms,Textbox,我想访问多个textboxname textbox1、textbox2、textbox3等。。按循环而不是按单个名称。出于这个原因,我创建了一个函数来创建这个变量名 public string[] nameCre(string cntrlName, int size) { string[] t = new string[size]; for (int i = 0; i < size; i++) { t[i] = cntrlName.ToString

我想访问多个
textbox
name textbox1、textbox2、textbox3等。。按循环而不是按单个名称。出于这个原因,我创建了一个函数来创建这个变量名

public string[] nameCre(string cntrlName, int size)
{
    string[] t = new string[size];

    for (int i = 0; i < size; i++)
    {
        t[i] = cntrlName.ToString() + (i + 1);
    }
    return t;
}
这给了我一个错误:

无法将类型“string”转换为“System.Windows.Forms.TextBox”

我怎样才能完成这项工作

string[] t= new string[50];
    t= nameCre("TextBox",5);

foreach (string s in t){
TextBox tb = (TextBox)this.Controls.FindControl(s);
tb.Text = "";
}
如果你有很多文本框

foreach (Control c in this.Controls)
{
  if (c.GetType().ToString() == "System.Windows.Form.Textbox")
  {
    c.Text = "";
  }
}

也许你需要这个-

        string[] t = new string[50];
        t = nameCre("TextBox", 5);

        foreach (string s in t)
        {
            if (!string.IsNullOrEmpty(s))
            {
                Control ctrl = this.Controls.Find(s, true).FirstOrDefault();
                if (ctrl != null && ctrl is TextBox)
                {
                    TextBox tb = ctrl as TextBox;
                    tb.Text = "";
                }
            }
        }

这篇文章很老了,不管怎样,我想我可以给你(或任何有类似问题的人)一个答案:

我认为使用文本框数组(或列表)将是最好的解决方案:

//使用数组:
TextBox[]TextBox=新的TextBox[5];
textBox[0]=newtextbox(){Location=new Point(),/*etc*/};
//或
textBox[0]=TextBox0;//如果您已经有一个名为TextBox0的文本框
//循环它:
for(int i=0;i


我希望这有帮助:)

不,你不能!无法将“字符串”转换为“文本框”。。。你到底想在这里做什么?你已经有文本框“TextBox1”、“TextBox2”等了吗。。您正在尝试获取它们并将它们的文本设置为“”?@PrateekSingh是的,这就是我要做的。在这种情况下,@GTSouza已经回答正确了……我使用的是Visual C#,而不是asp.net。我无法获取'FindControl'函数,只能获取Find()。我尝试了此代码。我遇到很多错误:1)我无法使用var。“找不到类型或命名空间名称‘var’(是否缺少using指令或程序集引用?)2)“System.Array”不包含“FirstOrDefault”的定义3)无法通过内置转换将类型“var”转换为“System.Windows.Forms.TextBox”。您使用的是什么.Net版本?我已更新了上面的代码以处理异常数1和3。对于2,在代码中使用System.Linq
添加System.Linq-
的引用。
var t = nameCre("TextBox",5);

foreach (var s in t)
{
    var textBox = new TextBox {Name = s, Text = ""};
}
        string[] t = new string[50];
        t = nameCre("TextBox", 5);

        foreach (string s in t)
        {
            if (!string.IsNullOrEmpty(s))
            {
                Control ctrl = this.Controls.Find(s, true).FirstOrDefault();
                if (ctrl != null && ctrl is TextBox)
                {
                    TextBox tb = ctrl as TextBox;
                    tb.Text = "";
                }
            }
        }
        // using an Array:
        TextBox[] textBox = new TextBox[5];
        textBox[0] = new TextBox() { Location = new Point(), /* etc */};
        // or
        textBox[0] = TextBox0; // if you already have a TextBox named TextBox0

        // loop it:
        for (int i = 0; i < textBox.Length; i++)
        {
            textBox[i].Text = "";
        }

        // using a List:  (you need to reference System.Collections.Generic)
        List<TextBox> textBox = new List<TextBox>();
        textBox.Add(new TextBox() { Name = "", /* etc */});
        // or
        textBox.Add(TextBox0); // if you already have a TextBox named TextBox0

        // loop it:
        for (int i = 0; i < textBox.Count; i++)
        {
            textBox[i].Text = "";
        }