2013-05-05 67 views
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創建了一個理論上無限的文本框,但我希望將這些動態生成的文本框中的文本添加到列表中。如何才能做到這一點?
[編輯] 我怎樣才能將它們全部顯示在一個消息框中,每一行都在不同的行上?無法將動態生成的文本框的文本添加到列表(C#)

回答

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); 
+0

於是說, 「這是一個類型,而是像一個變量使用。」 (TextBox) – TheUnrealMegashark 2013-05-05 03:01:09

+0

錯誤'System.Collections.Generic.List '是一個'類型',但用於'變量'\t < - 有確切的錯誤。 – TheUnrealMegashark 2013-05-05 03:03:20

+0

@ user2351094,我的壞,只是複製你的代碼,忘記了新的關鍵字。它應該是'新列表()' – Habib 2013-05-05 03:05:13

0

我不知道你爲什麼要使用一個單獨的按鈕2點擊事件添加文本。爲什麼不使用全局變量tilelocation,並將文本添加到button1_click事件中的此列表中?喜歡的東西:

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

如果你想在消息框中顯示它們,然後添加代碼:

string str=""; 
foreach(text in tilelocation) 
{ 
    str+=text; 
} 
MessageBox.Show(str); 
+0

我希望它添加文本並將它們全部顯示在消息框中。 – TheUnrealMegashark 2013-05-05 03:15:37

+0

我更新了我的答案,您需要更新您的問題。 – 2013-05-05 03:21:23

+0

好的,謝謝。 – TheUnrealMegashark 2013-05-05 03:24:16

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

}