获取动态创建的文本框的值
Get value of dynamically created textbox
我现在有点困惑,我创建了一些代码来创建 4 个文本框并在 运行 时间将它们添加到 table 布局(代码下面),但我正在努力从它获取文本,我尝试像 string s = TxtBox1.Text.ToString();
一样从它获取值,但它只是得到一个空引用,然后我尝试 txt.Text.ToString();
这只是获取文本来自最后创建的文本框。
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
我找遍了这个问题的答案,到目前为止还没有找到任何答案,如果有人有任何想法,我将不胜感激。
谢谢
此代码从 tableLayoutPanel1 中选取 textbox1,将其从 Control 转换为 TextBox 并获取 Text 属性:
string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;
如果您需要它们,请遍历文本框:
string[] t = new string[4];
for(int i=0; i<4; i++)
t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
你可以试试
var asTexts = tableLayoutPanel1.Controls
.OfType<TextBox>()
.Where(control => control.Name.StartsWith("TxtBox"))
.Select(control => control.Text);
这将枚举 tableLayoutPanel1 的所有子控件的 Text 值,其中它们的类型是 TextBox 并且它们的名称以 "TxtBox" 开头。
您可以选择放宽过滤器,删除 OfType 行(排除任何非 TextBox 控件)或 Where 行(仅允许名称与您的示例匹配的控件)。
确保有
Using System.Linq;
在文件的开头。
问候,
丹尼尔.
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
txt.KeyPress += txt_KeyPress;
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//the sender is now the textbox, so that you can access it
System.Windows.Forms.TextBox textbox = sender as System.Windows.Forms.TextBox;
var textOfTextBox = textbox.Text;
doSomethingWithTextFromTextBox(textOfTextBox);
}
我现在有点困惑,我创建了一些代码来创建 4 个文本框并在 运行 时间将它们添加到 table 布局(代码下面),但我正在努力从它获取文本,我尝试像 string s = TxtBox1.Text.ToString();
一样从它获取值,但它只是得到一个空引用,然后我尝试 txt.Text.ToString();
这只是获取文本来自最后创建的文本框。
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
我找遍了这个问题的答案,到目前为止还没有找到任何答案,如果有人有任何想法,我将不胜感激。
谢谢
此代码从 tableLayoutPanel1 中选取 textbox1,将其从 Control 转换为 TextBox 并获取 Text 属性:
string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;
如果您需要它们,请遍历文本框:
string[] t = new string[4];
for(int i=0; i<4; i++)
t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
你可以试试
var asTexts = tableLayoutPanel1.Controls
.OfType<TextBox>()
.Where(control => control.Name.StartsWith("TxtBox"))
.Select(control => control.Text);
这将枚举 tableLayoutPanel1 的所有子控件的 Text 值,其中它们的类型是 TextBox 并且它们的名称以 "TxtBox" 开头。 您可以选择放宽过滤器,删除 OfType 行(排除任何非 TextBox 控件)或 Where 行(仅允许名称与您的示例匹配的控件)。
确保有
Using System.Linq;
在文件的开头。 问候, 丹尼尔.
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
txt.KeyPress += txt_KeyPress;
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//the sender is now the textbox, so that you can access it
System.Windows.Forms.TextBox textbox = sender as System.Windows.Forms.TextBox;
var textOfTextBox = textbox.Text;
doSomethingWithTextFromTextBox(textOfTextBox);
}