【发布时间】:2016-01-28 18:08:16
【问题描述】:
在我的 C# windows 窗体项目中,我的程序无法从文本框中获取文本,也无法设置文本框。我能够注册按钮点击,所以程序 GUI 线程似乎没问题。
private void sendButton_Click(object sender, EventArgs e)
{
Console.WriteLine("I am being pressed");
textBox2.Text = "Test";
this.Refresh();
}
从代码中可以看出,当按下按钮时,输出面板会显示“我被按下”。但是,文本框不会在视觉上更新并显示“测试”。
我无法设置或获取文本是否有原因。我试过附加文本,但这不是我的意图。
我的猜测是表单可能没有聚焦,因为我已经改变了显示的表单。下面的代码来自第一个表单类。 lobby 是我展示的新 Form。
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";
}
}
}
大厅.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;
}
}
这是用户看到的第一个表单。这里一切正常,应该如此。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;
namespace ClientMMO
{
public partial class Form1 : Form
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
User test;
public Form1()
{
InitializeComponent();
ConnectToServer();
}
private void ConnectToServer()
{
IPAddress ipAddress = IPAddress.Loopback;
IPEndPoint ep = new IPEndPoint(ipAddress, 1234);
client.Connect(ep);
Console.WriteLine("connnected");
}
private void button1_Click(object sender, EventArgs e)
{
test = new User();
test.UserName = textBox1.Text;
test.Password = textBox2.Text;
client.Send(ClassToByteArray(test));
byte[] data = new byte[1024];
client.Receive(data);
string returndata = System.Text.Encoding.ASCII.GetString(data);
label1.Text = returndata;
//Console.WriteLine(returndata);
string b = "Logged in";
int result = 0;
string str1 = "Logged in";
result = string.Compare(str1, returndata);
if (result == 0)
{
button1.Visible = false;
byte[] shit = new byte[1024];
client.Receive(shit);
Console.WriteLine(shit.ToString());
string sfs = System.Text.Encoding.ASCII.GetString(shit);
label1.Text = String.Empty;
label1.Text = sfs;
int port = Int32.Parse(sfs);
client.Close();
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddresse = IPAddress.Loopback;
IPEndPoint epe = new IPEndPoint(ipAddresse, port);
client.Connect(epe);
Console.WriteLine("connected to another server");
/*------------------------------------This is where I open up my new window --------------------*/
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.
}
else
{
label1.Text = "failed";
}
}
private byte[] ClassToByteArray(Object objClass)
{
try
{
MemoryStream ms = new MemoryStream();
XmlSerializer xmlS = new XmlSerializer(typeof(User));
XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8);
xmlS.Serialize(xmlTW, objClass);
ms = (MemoryStream)xmlTW.BaseStream;
return ms.ToArray();
}
catch (Exception)
{
throw;
}
}
private Object ByteArrayToClass(byte[] buffer)
{
try
{
XmlSerializer xmlS = new XmlSerializer(typeof(User));
MemoryStream ms = new MemoryStream(buffer);
XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8);
return xmlS.Deserialize(ms);
}
catch (Exception)
{
throw;
}
}
}
}
【问题讨论】:
-
您确定 textBox2 是您正在查看的那个吗?
-
@Chin 在 GUI Builder 中,属性选项卡的“名称”字段中的名称为“textBox2”。 Designer.cs 文件也是如此。即 this.textBox2.Name = "textBox2".
-
我不知道winforms,但是没有ID属性吗?
-
我建议在
textBox2.Text行上设置一个断点,然后逐步进行。检查textBox2.Text的值在行执行之前、之后和之后this.Refresh() -
@JonH ID 属性称为“名称”,此“指示代码中用于标识对象的名称” - Visual Studio