C# 如何从按钮单击事件处理程序访问文本框中的文本

C# 如何从按钮单击事件处理程序访问文本框中的文本,c#,winforms,C#,Winforms,我正在尝试编写这个简单的winform菜单,我需要将NBox文本框的内容添加到字符串中,这样我就可以在按下按钮时显示它,但是我一直得到一个错误,即NBox在当前上下文中不存在。那么,我如何让文本框的内容在按下按钮时可用呢 using System; using System.Windows.Forms; using System.Drawing; using System.Diagnostics; //namespace game{ class MainM : Form{ public Mai

我正在尝试编写这个简单的winform菜单,我需要将NBox文本框的内容添加到字符串中,这样我就可以在按下按钮时显示它,但是我一直得到一个错误,即NBox在当前上下文中不存在。那么,我如何让文本框的内容在按下按钮时可用呢

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{

public MainM(){

    Text = "Adventures Main Menu";
    Size = new Size(400,400);

    //NameBox
    TextBox NBox = new TextBox();
    NBox.Location = new Point(145, 100);
    NBox.Size = new Size(200, 30);

    //Title Label

    Label title = new Label();
    title.Text = "ADVENTURE THE GAME";
    title.Location = new Point(145, 30);
    title.Size =  new Size(200,60);
    title.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);

    //The main menu Buttons and all that jazz
    Button credits = new Button();
    Button start = new Button();

    //Credits Button
    credits.Text = "Credits";
    credits.Size = new Size(75,20);
    credits.Location = new Point(145,275);
    credits.Click += new EventHandler(this.credits_button_click);

    //Start Button
    start.Text = "Start";
    start.Size = new Size(75,20);
    start.Location = new Point(145,200);
    start.Click += new EventHandler(this.start_button_click);


    //Control addition
    this.Controls.Add(title);
    this.Controls.Add(credits);
    this.Controls.Add(start);
    this.Controls.Add(NBox);
}

  public void test(){
            //The Main Window
  }

  private void credits_button_click(object sender, EventArgs e){
    MessageBox.Show("Created by: Me");
  }

 private void start_button_click(object sender, EventArgs e){
    this.Hide();
    string name = NBox.Text;
    MessageBox.Show(name);

    //Process.Start("TextGame.exe");
 }

  public static void Main(){
    Application.Run(new MainM());
  }

} 
//}

首先,您需要命名控件,该名称将是其在容器控件集合中的键:

//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
NBox.Name = "NBox"; //Naming the control
然后您将能够从容器中检索它:

private void start_button_click(object sender, EventArgs e){
    this.Hide();
    TextBox NBox= (TextBox)Controls.Find("NBox", true)[0];//Retrieve controls by name        
    string name = NBox.Text;
    MessageBox.Show(name);
    //Process.Start("TextGame.exe");
}

您在constructor中声明了NBox,它仅在构造函数中可见。您需要将其移到构造函数之外

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
    TextBox NBox;

    public MainM(){

    Text = "Adventures Main Menu";
    Size = new Size(400,400);

    //NameBox
    NBox = new TextBox();
    ...

我不认为在这里有一个可变级别的文本框有什么意义,很明显,他是在以友好的方式创建控件……虽然这很好而且很有效,但不要这样做!这不是正确的方法。@IvanP这取决于程序要求,有时不可能对每个控件都有一个变量引用。在这种情况下,这是一种方法。如果我需要一个变量引用,我宁愿在设计时添加控件,你不同意吗?我当然不同意。绝不应使用这种方法。