从方法外部访问Form变量

时间:2013-08-05 22:45:47

标签: c# winforms textbox

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,谷歌也没有帮助。我该如何访问它?

3 个答案:

答案 0 :(得分:1)

您的txt变量在Simple()构造函数的本地范围内声明。您将无法像在提交方法中那样访问此范围之外的任何位置。

您可能想要做的是在Simple类中创建一个私有实例变量,然后您可以从声明属于此类的任何方法访问该变量。

示例:

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

}

答案 1 :(得分:1)

您必须在表单类的某处定义txt的某个变量TextBox,这实际上是设计人员在您拖放TextBox时自动完成的Toolbox在您的表单上。此变量是TextBox的实例。它应该使用构造函数TextBox()进行初始化,并使用您在代码中执行的一些属性。您可以在表单类Simple的范围内使用此变量。它具有属性Text(类型为string),可以修改或提取以显示。要访问媒体资源,只需使用此模式:[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();
     }
  }
}

答案 2 :(得分:0)

默认情况下(并且有充分理由)使用设计器在表单上创建的控件是私有的。您可以将其更改为public,但更好的解决方案是在Form上创建一个公共属性以公开它。

public string MyTextField { get { return txt.Text; } }

当然,如果你想从外面改变它,你也可以在那里添加一个setter。但是,请记住,如果您尝试访问其他线程上的控件,那么他们在您创建的线程上将会有一个单独的跨线程问题需要处理,但是有很多关于如何处理该问题的帖子在SO上已经。