C# 程序不获取文本或设置文本

C# 程序不获取文本或设置文本,c#,winforms,C#,Winforms,在我的C#windows窗体项目中,我的程序无法从文本框中获取文本,也无法设置文本框。我能够注册按钮点击,所以程序GUI线程似乎还可以 private void sendButton_Click(object sender, EventArgs e) { Console.WriteLine("I am being pressed"); textBox2.Text = "Test"; this.Refresh(); } 从按下按钮时的代码中可以看出,输出面板显示“我正在被

在我的C#windows窗体项目中,我的程序无法从文本框中获取文本,也无法设置文本框。我能够注册按钮点击,所以程序GUI线程似乎还可以

private void sendButton_Click(object sender, EventArgs e)
{
    Console.WriteLine("I am being pressed");
    textBox2.Text = "Test";
    this.Refresh();
}
从按下按钮时的代码中可以看出,输出面板显示“我正在被按下”。但是,文本框不会在视觉上更新并显示“Test”

是否有我无法设置或获取文本的原因。我已经试着附加文本,但这不是我的意图

我的猜测是,形式可能没有集中,因为我已经改变了形式显示的内容。下面的代码来自第一个表单类<代码>大厅是我展示的一个新的
表单

if (correct)
{
    lobby lob = new lobby(client, this);
    this.Hide(); // this is the first form the user sees, I have hidden it. If I close this form then the application exits.
    lob.Show(); // this is the form that I show after the user logs in with right credentials. Textbox.text = "test" does not work on this for some reason.                
}
在第一个窗体窗口中,我可以使用
Text
方法检索/设置值

但我不能在第二种形式中做同样的事情

--------编辑------- 这是第二个表单的GUI生成器。 您可以看到名称字段名为“TextBox2”

lobby.cs文件

using System.Net.Sockets;
using System.Windows.Forms;

namespace ClientMMO
{
    public partial class lobby : Form
    {
        static int counter;
        private Socket client;
        private Form1 form1;

        public lobby()
        {
            InitializeComponent();
        }

        public lobby(Socket client)
        {
            InitializeComponent();
            this.client = client;   

        }

        public lobby(Socket client, Form1 form1) : this(client)
        {
            InitializeComponent();
            this.form1 = form1;

            textBox2.Text = "Test";

        }

    }
}
lobble.Designer.cs

namespace ClientMMO
{
    partial class lobby
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(107, 90);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(251, 20);
            this.textBox2.TabIndex = 0;
            // 
            // lobby
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(511, 413);
            this.Controls.Add(this.textBox2);
            this.Name = "lobby";
            this.Text = "lobby";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox2;
    }
}

假设你的主窗体启动正确,你所做的应该是有效的。您可以在设置text属性后尝试放置Application.DoEvents()。确保你没有在等信号灯什么的


是否将lob存储为类变量而不是局部变量

对于这个问题没有答案,我可以从“修复”bug(如果有)的角度给出答案


但解决方案是创建一个新的Windows窗体文件并重新开始。

我认为这不是一个bug。我认为您正在尝试从另一个窗体访问私有对象。 在lobb.cs中编写一个新方法,将textBox1的文本设置为:

public void SetText(string text){
 textBox2.Text = text;
}
然后,当您隐藏主窗体时:

lobby lob = new lobby(client, this);
this.Hide();
lob.SetText("Test");  
lob.Show(); 

您确定您正在查看的是textBox2吗?@Chin在GUI生成器中,“属性”选项卡的“名称”字段中有名称“textBox2”。Designer.cs文件也是如此。i、 e this.textBox2.Name=“textBox2”。我不知道winforms,但是没有ID属性吗?我建议在
textBox2.Text
行上放置一个断点,然后逐步执行。在执行该行之前、之后以及之后检查
this.Refresh()
@JonH的
textBox2.Text的值。ID属性称为“Name”,这“指示代码中用于标识对象的名称”-Visual StudioMy主窗体启动任何其他windows窗体的启动方式。即默认情况下,锅炉板代码未更改任何代码。我尝试过使用Application.DoEvents(),但它仍然没有更新文本框。但是,我可以通过代码更改文本框的值,但文本框不会更新。我可以在文本框中键入,但无法通过代码检索值。”lob'是局部变量,而不是全局变量。
lobby lob = new lobby(client, this);
this.Hide();
lob.SetText("Test");  
lob.Show();