C# 用两种形式传递值
下面的代码是Form1,当我在Form2中使用它时,它会收到一个空字符串。 有没有办法在两个表单之间传递数据C# 用两种形式传递值,c#,forms,C#,Forms,下面的代码是Form1,当我在Form2中使用它时,它会收到一个空字符串。 有没有办法在两个表单之间传递数据 public string returnAccount() { return currentAccount; } public string returnPassword() { return currentPassword; } public string returnIP() {
public string returnAccount()
{
return currentAccount;
}
public string returnPassword()
{
return currentPassword;
}
public string returnIP()
{
return currentIP;
}
public string returnPort()
{
return currentPort;
}
如果它不起作用,那么您可能使用了错误的表单引用(如果您在多个地方编写
newform1()
,那么这可能是一个签名)
像这样的方法应该会奏效:
Form1 frm = new Form1();
if(frm.ShowDialog() == DialogResult.OK)
{
string account = frm.returnAccount();
}
在
Form2
可以访问变量之前,需要为当前变量赋值。您是否在Form1
类的构造函数方法中分配这些变量值?您是否有希望从中收集数据的表单实例,您是否没有创建新实例?检索是什么样子的?如何从表单2
调用这些方法?