Accessing Form variable from outside of a method

泄露秘密 提交于 2021-02-02 09:45:46

问题


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

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


回答1:


Your txt variable is declared within the local scope of the Simple() constructor. You will not be able to access it anywhere outside of this scope like you are doing in your submit method.

What you may want to do is create a private instance variable within your Simple class that you will then be able to access from any method declared that belongs to this class.

Example:

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

}




回答2:


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



回答3:


By default (and for good reason) controls created on forms using the designer are private. You could change it to public, but a better solution would be to just create a public property on the Form to expose it.

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

Of course you could also add a setter in there too if you want to change it from the outside. However, keep in mind that if you are trying to access controls on a thread other then the one they were created on you will have a separate cross-threading concern to deal with, but there are plenty of posts on how to deal with that issue on SO already.



来源:https://stackoverflow.com/questions/18069033/accessing-form-variable-from-outside-of-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!