【发布时间】:2012-11-28 05:43:18
【问题描述】:
我目前正在开发一个 C# Visual Studio Windows 应用程序。我在从一个表单(Form1)中获取文本框值并将其存储在一个变量中以供下一个表单(Form2)使用时遇到问题。我不想立即显示该值,因此我需要一种可以将值存储在后台并能够在以后使用它的方法。我该怎么做?
我试过这种方法,但它不起作用,谁能告诉我为什么? form2中的返回值为空。
Login.cs(登录页面)
public string uname;
private void LoginButton_Click(object sender, EventArgs e)
{
string userName = this.LoginUsernameTextBox.Text;
uname = userName;
}
Form2.cs
private void CheckLoginUsername_Click(object sender, EventArgs e)
{
Login login = new Login();
MessageBox.Show("The value of uname is:" + login.uname);
}
【问题讨论】:
-
你能用你试过的代码展示一些代码吗?
-
But when i tried it, the value seems to get lost along the way after not immediately passing it the the next form-- 那你做的不对。您可以轻松地将值存储在私有成员中,并随时在控件中显示它。 -
在第二个表单中创建一个属性,并在显示第二个表单之前从第一个表单写入值。
标签: c# winforms variables store