【问题标题】:Accessing Form variable from outside of a method从方法外部访问 Form 变量
【发布时间】:2013-08-05 22:45:47
【问题描述】:
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,而谷歌也没有提供帮助。如何访问它?

【问题讨论】:

  • Console.WriteLine() 在 winform 中?
  • 有关变量范围msdn.microsoft.com/en-us/library/ms973875.aspx的更多信息,请参阅此 msdn 链接
  • @Precious1tj 实际上在故障排除方面很常见。
  • @Ralph 好的。我刚刚使用 winforms 大约 2 个月了,在我读过的所有书籍中。我从来没有真正见过这样的事情

标签: c# winforms textbox


【解决方案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 ();
    }
}

}

【讨论】:

    【解决方案2】:

    您必须在表单类的某处定义一些变量 txtTextBox,这实际上是设计人员在您将 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();
         }
      }
    }
    

    【讨论】:

    • 你能解释一下你为他做了什么,这样一目了然吗? :P
    • @MohammadAliBaydoun 添加了一些非常基本的解释,我认为不应该添加。 OP 应该首先阅读一些关于classwinforms 的非常基础的书。
    • @KingKing 可能是这样,但是没有解释或上下文说明您试图向 OP 展示的内容的代码块恕我直言会适得其反。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多