【问题标题】:Getting text box values through loop通过循环获取文本框值
【发布时间】:2013-11-11 06:29:36
【问题描述】:

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

【问题讨论】:

  • 您可以创建一个数组并将所有 txt_Ad... 添加到其中。 TextBox[] textBoxes = new TextBox[] { txt_Addr1, txt_Addr2,...}。然后循环。
  • 查看我的回答,您可以将所有文本框添加到列表中或将其添加到数组中并循环它,TextBox 是引用类型,这意味着声明 TextBox a = txt_address10 不会产生它的副本,而是创建一个指向 txt_address10 的指针
  • 确保你真的想要这样做。人们告诉你怎么做,但它看起来像一个糟糕的数据库设计和随之而来的时间浪费。请参阅下面的答案。
  • @user1646737 但这是我的客户希望我做的。
  • 有时,您必须听从命令,但我会向客户解释问题,并告诉他如何进行适度的更改才能使他的最终产品无限多才多艺。如果他拒绝了,那么你可以为设计不佳的数据库编写所有糟糕的代码。

标签: c# .net for-loop textbox


【解决方案1】:

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

List<TextBox> textboxControls = new List<TextBox>();

然后当你创建它们时,将它们添加到集合中

textboxControls.Add(control);

然后你可以遍历它们并访问它们的值

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

【讨论】:

    【解决方案2】:

    只引用TextBox数组中的文本框;

    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);
    }
    

    【讨论】:

    • 但在这种情况下,我还需要为每个文本框声明 10 个文本框。可以用 FOR 循环代替 foreach 循环吗?
    • 确保您可以动态创建文本框并将其放置在如下形式:
    • List lst = new List(); for (int i=0; i
    【解决方案3】:

    您可以将它们放在一个列表中,然后遍历该列表...

    编辑:似乎 lostincomputer 比我早二十秒回答... 一样一样...两者都可以工作

    【讨论】:

      【解决方案4】:

      在您花费(浪费)时间为这样设计的数据库编写代码之前,您应该重新设计您的数据库。在一个表中为 10 个地址设置 10 列并不是一个好主意。您应该能够拥有从 0 到无穷大的地址。查看如何制作关系数据库。

      基本上:

      表格:客户

      CustomerID
      Name
      Etc.
      

      表:客户地址

      CustomerID
      Address
      City
      State
      Zip
      

      【讨论】:

        【解决方案5】:

        您可以像这样使用 Controls.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" ...
                    }
                }
        

        【讨论】:

        • 我需要为 Controls.Find() 添加一些命名空间吗?如果是,请告诉我。因为我无法在 intellicense 中找到 Find 方法。
        • 您没有指定...WinForms?、WebForms?、WPF?,还有什么?
        • 我说的是 WebForms。
        • 对不起,我不是网络开发人员。请参阅 here 上的 andleer 的回答。
        【解决方案6】:

        或者您可以从没有列表的表单中访问它们:

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

        或者,如果您将它们放在 groupBox 中

        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
                    }
                }
        

        【讨论】:

        • 在这种情况下,您还需要将control 转换为TextBox。也许最好先转换为control as Textbox 并进行空检查。这样你只能施放一次。
        【解决方案7】:
        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;
        });
        

        【讨论】:

          【解决方案8】:

          第一步: 您可以浏览所有Form 控件,只考虑TextBox 控件。

          第 2 步: 从所有表单 TextBox 控件中过滤包含 Name 的 TextBox 为“txt_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());
                  }
          

          【讨论】:

            猜你喜欢
            • 2011-11-02
            • 2016-03-23
            • 2011-11-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多