1
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从表单外部访问,谷歌也没有帮助。我如何访问它?

4

3 回答 3

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

}

于 2013-08-05T22:50:37.000 回答
1

您必须在表单类txt中的TextBox某个地方定义一些变量,这实际上是由设计人员在您将 a 拖放到表单上时自动为您完成TextBoxToolbox。此变量是 的一个实例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();
     }
  }
}
于 2013-08-05T22:53:13.103 回答
0

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

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

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

于 2013-08-05T22:56:01.327 回答