Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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_For Loop_Textbox - Fatal编程技术网

C# 通过循环获取文本框值

C# 通过循环获取文本框值,c#,.net,for-loop,textbox,C#,.net,For Loop,Textbox,我有10个文本框txt_Address1、txt_Address2…txt_Address10和10列,用于在数据库中存储它们的值,即Address1、Address2…Address10。现在我想获取文本框的每个值,并将其存储到相应的列中。为此,我希望通过For循环来完成,而不是为每个文本框编写10行代码。有人能给我推荐合适的解决方案吗?只需在文本框数组中引用文本框 TextBox txt1 = new TextBox(); TextBox txt2 = new TextBox(); Text

我有10个文本框txt_Address1、txt_Address2…txt_Address10和10列,用于在数据库中存储它们的值,即Address1、Address2…Address10。现在我想获取文本框的每个值,并将其存储到相应的列中。为此,我希望通过For循环来完成,而不是为每个文本框编写10行代码。有人能给我推荐合适的解决方案吗?

只需在文本框数组中引用文本框

TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txtN = new TextBox();

TextBox[] allTextBoxes = new TextBox[] { txt1, txt2, txt3, txtN };

foreach(TextBox item in allTextBoxes)
{
StoreValue(item.Text);
}
或者你可以使用

List<TextBox> lst = new List<TextBox>();
lst.Add(txt1);
lst.Add(txt2);
lst.Add(txt3);

foreach(TextBox item in lst)
{
    StoreValue(item.Text);
}
List lst=new List();
第一次增补(txt1);
第一次增补(txt2);
第一次增补(txt3);
foreach(lst中的文本框项)
{
StoreValue(item.Text);
}

您可以将它们放在一个列表中,然后在该列表上迭代

编辑:似乎丢失了计算机比我早二十秒回答。。。
同样的。。。这两种方法都可以使用

当您创建文本框时,将它们存储在一个集合中

List<TextBox> textboxControls = new List<TextBox>();
textboxControls.Add(control);
然后您可以在它们上面循环并访问它们的值

foreach(var control in textboxControls )
    DoSomethingWithText(control.Text);

或者,您可以从表格中访问它们,而无需列表:

foreach(Control control in MyForm.Controls)
{
    if(control is TextBox)
    {
       //do what you want

    }
}
或者如果你把它们放在一个分组框中

foreach(Control control in myGroupBox.Controls)
{
     if(control is TextBox)
     {
         //do what you want
     }
}
希望这有帮助

或者使用FOR循环:

//Controls is the Controls collection of the form
for(int i=0;i<Controls.Count;i++)
        {
            if(Controls[i] is TextBox)
            {
                //do what you want
            }
        }
//控件是窗体的控件集合
对于(int i=0;i,在您花费(浪费)时间为这样设计的数据库编写代码之前,您应该重新设计您的数据库。在一个表中有10列包含10个地址不是一个好主意。您应该能够有0到无穷多个地址。查看如何创建关系数据库

基本上:

表格:客户

CustomerID
Name
Etc.
表格:客户地址

CustomerID
Address
City
State
Zip
步骤1: 您可以遍历所有的代码>窗体< /代码>控件,只考虑“代码>文本框< /代码>控件。

第2步:从所有表单
文本框
控件筛选包含
名称
的文本框,作为“
txt\u Address%
”在这里
%
可以是类似于1,2,3,4…等的任何内容

代码如下:

        List<String> txtValues=new List<string>();
        foreach (var control in this.Controls)
        {
         if((control is TextBox) && (((TextBox) control).Name.Contains("txt_Address")))
             txtValues.Add(((TextBox) control).Text.ToString());
        }
List txtValues=new List();
foreach(此.Controls中的var控件)
{
if((控件为文本框)和((文本框)控件).Name.Contains(“txt_地址”))
添加(((TextBox)控件).Text.ToString());
}
您可以像这样使用控件.Find():

        for (int i = 1; i <= 10; i++)
        {
            Control[] matches = this.Controls.Find("txt_Address" + i.ToString(), true);
            if (matches.Length > 0 && matches[0] is TextBox)
            {
                TextBox tb = (TextBox)matches[0];
                // ... do something with "tb" ...
            }
        }
for(int i=1;i 0&&matches[0]是文本框)
{
TextBox tb=(TextBox)匹配[0];
//…用“tb”做点什么。。。
}
}
var columns=new Dictionary();
for(int i=1;it是文本框);
columns.ToList().ForEach(c=>
{
var index=c.Key.Replace(“地址”,string.Empty);
var textBox=textboxs.FirstOrDefault(t=>index.Equals(t.Name.Replace(“txt_地址”,string.Empty));
如果(textBox!=null)列[c.Key]=textBox.Text;
});

您可以创建一个数组并将所有的txt\u Ad…添加到该数组中。TextBox[]textboxs=new TextBox[]{txt\u Addr1,txt\u Addr2,…}。然后循环。看我的答案,你可以将所有文本框添加到列表中或添加到数组中并循环,文本框是一种引用类型,这意味着声明一个文本框a=txt\u address10并不会复制它,而是创建一个指向txt\u address10的指针确保你真的想这样做。人们告诉你怎么做,但它看起来像是一个糟糕的数据库设计和随之而来的时间浪费。请看下面我的答案。@user1646737但这是我的客户希望我做的。有时,你必须服从命令,但我会向客户解释问题,并告诉他如何适度的更改可以使他的最终产品无限多功能。如果他拒绝,那么,你可以写所有的命令e对于设计糟糕的数据库来说,代码很糟糕。但是在这种情况下,我需要为每个文本框声明10个文本框。可以用for循环代替foreach循环吗?确保可以动态创建文本框,并将其以如下形式放置:List lst=new List();for(int i=0;在这种情况下,iYou还需要将
控件转换为
文本框
。也许最好先将
控件转换为文本框
,然后执行空检查。这样您只转换一次。是否需要为控件添加一些命名空间。Find()?如果是,请告诉我。因为我无法在intellicense中找到find方法。您没有指定…WinForms?、WebForms?、WPF?、其他内容?我说的是WebForms。对不起,我不是web开发人员。请参阅andleer的回答。
var columns = new Dictionary<string, string>();
for (int i = 1; i <= 10; i++) columns.Add("Address" + i, string.Empty);

var textBoxes = Controls.Find("txt_Address", true).Where(t => t is TextBox).ToList();

columns.ToList().ForEach(c =>
{
    var index = c.Key.Replace("Address", string.Empty);
    var textBox = textBoxes.FirstOrDefault(t => index.Equals(t.Name.Replace("txt_Address", string.Empty)));
    if (textBox != null) columns[c.Key] = textBox.Text;
});