C# 从方法外部访问表单变量
我正在尝试从表单外部访问C# 从方法外部访问表单变量,c#,winforms,textbox,C#,Winforms,Textbox,我正在尝试从表单外部访问txt.Text,谷歌也没有任何帮助。如何访问它?您的txt变量在Simple()构造函数的局部范围内声明。您将无法像在submit方法中那样访问此范围之外的任何位置 您可能想做的是在简单类中创建一个私有实例变量,然后可以从属于该类的任何声明方法访问该变量 例如: public class Simple : Form { public Simple() { Text = "Server Command Line"; Size
txt.Text
,谷歌也没有任何帮助。如何访问它?您的txt变量在Simple()构造函数的局部范围内声明。您将无法像在submit方法中那样访问此范围之外的任何位置
您可能想做的是在简单类中创建一个私有实例变量,然后可以从属于该类的任何声明方法访问该变量
例如:
public class Simple : Form
{
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.Parent = this;
txt.KeyDown += submit;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(sSubmit);
}
private void submit(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter ) {
Console.WriteLine ("txt.Text");//How do I grab this?
Submit();
}
}
}
}您必须在表单类的某个地方定义
TextBox
的某个变量txt
,事实上,当您将TextBox
从Toolbox
拖放到表单上时,设计器会自动为您完成此操作。此变量是TextBox
的一个实例。应该使用构造函数TextBox()
初始化它,并像在代码中那样使用一些属性。您可以在表单类Simple
的范围内使用此变量。它具有属性Text
(类型为string
),可以修改或获取以显示。要访问属性,只需使用以下模式:[instance Name].[property Name]
public class Simple : Form
{
//now this is field is accessible from any method of declared within this class
private TextBox _txt;
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
_txt = new TextBox ();
_txt.Location = new Point (20, Size.Height - 70);
_txt.Size = new Size (600, 30);
_txt.Parent = this;
_txt.KeyDown += submit;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(sSubmit);
}
private void submit(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter ) {
Console.WriteLine (_txt.Text);//How do I grab this?
Submit ();
}
}
默认情况下(出于正当理由),使用设计器在窗体上创建的控件是私有的。您可以将其更改为public,但更好的解决方案是在表单上创建一个公共属性来公开它
public class Simple : Form
{
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.Parent = this;
txt.KeyDown += submit;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(sSubmit);
}
TextBox txt;
private void submit(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter ) {
Console.WriteLine (txt.Text);
Submit();
}
}
}
当然,如果您想从外部进行更改,也可以在其中添加setter。但是,请记住,如果您试图访问其他线程上的控件,那么创建这些控件的线程将有一个单独的跨线程问题需要处理,但是已经有很多关于如何处理该问题的帖子了。您能解释一下您为他做了什么吗,这样就很清楚了?:P@MohammadAliBaydoun添加了一些非常基本的解释,我认为不应该添加这些。OP应该先阅读一些关于
类
和winforms
的基本书籍。@KingKing可能是这样,但是一段没有解释或没有上下文的代码对OP的演示是非常不利的。Console.WriteLine()
在winform中?有关变量作用域@Precious1tj的更多信息,请参阅此msdn链接,该变量作用域实际上非常常见,用于故障排除。@Ralph Ok。我刚刚使用winforms大约两个月了,在我读过的所有书籍中。我从没见过这样的事
public string MyTextField { get { return txt.Text; } }