C# 获取在运行时创建的文本框的文本
在我的应用程序中,我在运行时创建了一个文本框,下面是代码C# 获取在运行时创建的文本框的文本,c#,wpf,C#,Wpf,在我的应用程序中,我在运行时创建了一个文本框,下面是代码 TextBox bt = new TextBox(); bt.Name = "population_textbox"; bt.Height = 20; bt.SetValue(Grid.ColumnProperty, 1); bt.SetValue(Grid.RowProperty, 0); temp_grid.Children.Add(bt); 那么,如何在用户键入内容并输入后获取此文本框的文本。我不知道怎么做,我试过了 var tb
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
那么,如何在用户键入内容并输入后获取此文本框的文本。我不知道怎么做,我试过了
var tb = (FrameworkElement)this.FindName("population_textbox") as TextBox;
Console.Write(tb.Text);
这是错误警报:
Exception has been thrown by the target of an invocation.
我给你写一个简单的例子:
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Text = "some";
bt.Height = 20;
main_Grid.Children.Add(bt);
foreach (TextBox txt in main_Grid.Children)
{
if (txt is TextBox)
{
if ((txt as TextBox).Name == "population_textbox")
{
MessageBox.Show((txt as TextBox).Text);
}
}
}
我给你写一个简单的例子:
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Text = "some";
bt.Height = 20;
main_Grid.Children.Add(bt);
foreach (TextBox txt in main_Grid.Children)
{
if (txt is TextBox)
{
if ((txt as TextBox).Name == "population_textbox")
{
MessageBox.Show((txt as TextBox).Text);
}
}
}
使用以下代码:
this.RegisterName("bt", textBox);
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
Console.Write(bt.Text);
还可以尝试:
var tb = (FrameworkElement)this.FindName("population_textbox");
或直接写入:
this.RegisterName("bt", textBox);
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
Console.Write(bt.Text);
[未将其纳入tb var中]
这用于从运行时分配值的文本框中获取文本。使用以下代码:
this.RegisterName("bt", textBox);
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
Console.Write(bt.Text);
还可以尝试:
var tb = (FrameworkElement)this.FindName("population_textbox");
或直接写入:
this.RegisterName("bt", textBox);
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
Console.Write(bt.Text);
[未将其纳入tb var中]
这用于从文本框中获取文本,文本框的值在运行时分配。您应该声明控件,然后调用RegisterName方法使控件可访问,然后您可以从窗口范围内的任何位置引用控件名称:
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
this.RegisterName(bt.Name, bt);
var tb = this.FindName("population_textbox") as TextBox;
Console.Write(tb.Text);
您应该声明控件,然后调用使控件可访问的RegisterName方法,然后可以从窗口范围内的任何位置引用控件名称:
TextBox bt = new TextBox();
bt.Name = "population_textbox";
bt.Height = 20;
bt.SetValue(Grid.ColumnProperty, 1);
bt.SetValue(Grid.RowProperty, 0);
temp_grid.Children.Add(bt);
this.RegisterName(bt.Name, bt);
var tb = this.FindName("population_textbox") as TextBox;
Console.Write(tb.Text);
Console.Write(((FrameworkElement)this.FindName(“population_textbox”)为textbox.Text.ToString());
temp\u grid
是否有名为FindName(..)
的方法?如果是这样,请尝试调用它而不是this.FindName(..)
@zey:同样的结果,异常已经被抛出了在您的上下文中this
(Type)是什么?@Jens Kloster:不,temp\u网格只是局部变量,所以外部范围无法看到它!Console.Write(((FrameworkElement)this.FindName(“population_textbox”)为textbox.Text.ToString());temp\u grid
是否有名为FindName(..)
的方法?如果是这样,请尝试调用它而不是this.FindName(..)
@zey:同样的结果,异常已经被抛出了在您的上下文中this
(Type)是什么?@Jens Kloster:不,temp\u网格只是局部变量,所以外部范围无法看到它!你能给我举个完整的小例子吗?我是C#的新手,我收到一条异常消息:对象引用未设置为对象的实例。你的意思是我注册一个文本框名称“bt”,然后直接调用bt.Text吗?当然没用了!它确实有效,但不是我需要的答案,因为textbox declare在上次使用时在另一个作用域中,bt是一个局部变量,因此,在另一个函数中不能访问它。你可以为此声明全局变量,然后直接赋值,这样整个代码都可以访问它。你能给我一个完整的小例子吗?我是C#的新手,我收到一条异常消息:对象引用未设置为对象的实例。你的意思是我注册一个文本框名称“bt”,然后直接调用bt.Text吗?当然没用了!它确实有效,但不是我需要的答案,因为textbox declare在上次使用时在另一个作用域中,bt是一个局部变量,因此,在另一个函数中不能访问它。您可以为此声明全局变量,然后直接赋值,这样整个代码都可以访问它。