从方法外部访问 Form 变量

Accessing Form variable from outside of a method(从方法外部访问 Form 变量)
本文介绍了从方法外部访问 Form 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试从表单外部访问 txt.Text,而谷歌也没有提供帮助.如何访问它?

I'm trying to access txt.Text from outside the Form, and google hasn't been helpful either. How do I access it?

推荐答案

你必须在你的表单类的某处定义TextBox的一些变量txt,这其实是当您将 TextBoxToolbox 拖放到表单上时,设计器会自动为您完成.此变量是 TextBox 的一个实例.它应该使用构造函数 TextBox() 进行初始化,并使用您在代码中所做的一些属性.您可以在表单类Simple 的范围内使用此变量.它具有属性 Text(类型为 string),可以修改或获取以显示.要访问属性,只需使用以下模式:[instance Name].[Property name]

You have to define some variable txt of TextBox somewhere in your form class, this is in fact done automatically by designer for you when you drag-n-drop a TextBox from Toolbox onto your form. This variable is an instance of TextBox. It should be initialized using the constructor TextBox() and with some properties as you did in your code. You can use this variable in the scope of the form class Simple. It has property Text (of type string) which can be modified or fetched to display. To access a property, just use this pattern: [instance Name].[Property name]

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

这篇关于从方法外部访问 Form 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)