0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mod
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int c = 0;

    private void button1_Click(object sender, EventArgs e)
        {
        TextBox txtRun = new TextBox();
            txtRun.Name = "txtDynamic" + c++;
            txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
            txtRun.Size = new System.Drawing.Size(200,15);
            this.Controls.Add(txtRun);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            List<string>tilelocation = List<string>();
            tilelocation.Add();  //What goes in this method's arguments?
        }
    }
}

这是我的代码。Button1 创建了理论上无限的文本框,但我希望将这些动态生成的文本框中的文本添加到列表中。如何才能做到这一点?
[编辑] 我怎样才能将它们全部显示在一个消息框中,每个都在单独的行上?

4

3 回答 3

1

您需要保留对控件的引用。

另一个秘密是您必须将其保存在 ViewState 中,以便在回发之间可用。

public partial class Form1 : Form {
  public Form1() {
    InitializeComponent();
  }

  int c = 0;
  private List<TextBox _lstTextBoxList;
  public List<TextBox> lstTextBoxList {
    get { 
      if(_lstTextBoxList == null) {
        _lstTextBoxList = ViewState["lstTextBoxList"] as List<TextBox>;
      }
      return _lstTextBoxList; 
    }
    set { ViewState["lstTextBoxList"] = _lstTextBoxList = value; }
  }


  private void button1_Click(object sender, EventArgs e) {
    TextBox txtRun = new TextBox();
    txtRun.Name = "txtDynamic" + c++;
    txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
    txtRun.Size = new System.Drawing.Size(200,15);
    this.Controls.Add(txtRun);
    lstTextBoxList.Add(txtRun);
  }
  private void button2_Click(object sender, EventArgs e) {
    // Not sure of your goal here:
    List<string> tilelocation = List<string>();
    tilelocation.Add(lstTextBoxList[lstTextBoxList.Count - 1]);
    // I would assume you wanted this:
    List<string> strValues = lstTextBoxList.Select<TextBox,string>(t =>   t.Text).ToList();
  }

}

于 2013-05-05T03:29:01.260 回答
0

但我希望将这些动态生成的文本框中的文本添加到列表中。如何才能做到这一点?

你应该new List<string>像这样使用:

private void button2_Click(object sender, EventArgs e)
{
    List<string> tilelocation = new List<string>();
    foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(r=> r.Name.StartsWith("txtDynamic"))
          titlelocation.Add(tb.Text);

 //if you want a string out of it. 

 string str = string.Join(",", titlelocation);
 MessageBox.Show(str);
}

编辑:对于新行使用的每个文本:

string str = string.Join(Environment.NewLine, titlelocation);
MessageBox.Show(str);
于 2013-05-05T02:57:26.300 回答
0

我不知道您为什么要使用单独的 button2 单击事件来添加文本。为什么不使用全局变量 tilelocation,并在 button1_click 事件中将文本添加到此列表中?比如:</p>

txtRun.Text=Your Text;
tilelocation.add(Your Text);

如果要在消息框中显示,则添加代码:</p>

string str="";
foreach(text in tilelocation)
{
   str+=text;
}
MessageBox.Show(str);
于 2013-05-05T03:09:03.297 回答