use*_*984 0 .net c# forms variables toolstrip
我通过Toolstrip打开另一个表单,输入Mainform中需要的用户名(并在Mainform中声明为String)
主体代码:
private void toolStripButton6_Click(object sender, EventArgs e)
{
using (Form frm = new Form3())
{
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
frm.StartPosition = FormStartPosition.CenterParent;
if (frm.ShowDialog() == DialogResult.OK)
{
Username = frm.ReturnValue1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form3代码:
public string ReturnValue1 {
get
{
return textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
Run Code Online (Sandbox Code Playgroud)
C#告诉我没有frm.ReturnValue1 :(
您已将表单声明为Form
不是Form3
:
using (Form frm = new Form3())
Run Code Online (Sandbox Code Playgroud)
并且由于该类Form
没有属性,ReturnValue1
您将收到错误.这个编译是因为它Form3
是一个子类,Form
所以你可以将它分配给一个类型的变量,Form
而不需要任何转换.如果你有它,那么编译器就会告诉你需要一个演员.
你的代码应该是:
using (Form3 frm = new Form3())
Run Code Online (Sandbox Code Playgroud)
或者甚至(我的偏好):
using (var frm = new Form3())
Run Code Online (Sandbox Code Playgroud)
然后它将始终是正确的类型,如果您决定将来使用不同的表单,则不必记住在两个地方更改类名.