创建并显示 WinForm
Create and Show a WinForm
我正在尝试使用通用方法在 VS 2013 中创建和显示表单,这是我的方法:
void showOrUpdateForm<T>(ref Form form) where T : Form
{
if (form == null)
{
form = Activator.CreateInstance(typeof(T)) as T;
form.Show();
}
else
form.WindowState = FormWindowState.Normal;
form.Focus();
}
这段代码是我从这里得到的。因此,当我尝试像他们所说的那样调用此方法时:
myForm1 form;
showOrUpdateForm<myForm1>(ref form);
它给我错误:
错误 1 'GestionEAS.GestionEAS.showOrUpdateForm(ref System.Windows.Forms.Form)' 的最佳重载方法匹配有一些无效参数
错误 2 参数 1:无法从 'ref package.Views.form' 转换为 'ref System.Windows.Forms.Form'
我该如何解决这个问题?
您不能将 myForm1
作为 ref Form
传递。
您应该将其更改为 ref T
,以便参数类型与变量匹配。
此外,您应该将 , new()
添加到您的通用约束中,并将 Activator.CreateInstance(typeof(T)) as T
替换为 new T()
看来,你想要这样的东西:
// I'd rather return potentially created form directly (not via "ref")
// do you need "this" in the method? That's why "static"
static T showOrUpdateForm<T>(T form = null)
where T: Form, new() {
if (null == form)
form = new T(); // <- no Activator since "new()" constraint is declared
form.WindowState = FormWindowState.Normal;
form.BringToFront(); // <- if the form is not a foreground one
if (form.CanFocus) // <- better check than have an exception
form.Focus();
return form;
}
...
// more compact and readable
myForm1 form = showOrUpdateForm<myForm1>();
这个怎么样,可以很好地满足创建具有不同参数的表单的需求
void showOrUpdateForm<T>(ref T form, Func<T> formCreator) where T : Form
{
if (form == null)
{
form = formCreator();
form.Show();
}
else
form.WindowState = FormWindowState.Normal;
if(form.CanFocus)
form.Focus();
}
将其用作 . . .
MyForm myForm = null;
form1 = showOrUpdateForm(myForm, () => new MyForm());
我正在尝试使用通用方法在 VS 2013 中创建和显示表单,这是我的方法:
void showOrUpdateForm<T>(ref Form form) where T : Form
{
if (form == null)
{
form = Activator.CreateInstance(typeof(T)) as T;
form.Show();
}
else
form.WindowState = FormWindowState.Normal;
form.Focus();
}
这段代码是我从这里得到的。因此,当我尝试像他们所说的那样调用此方法时:
myForm1 form;
showOrUpdateForm<myForm1>(ref form);
它给我错误:
错误 1 'GestionEAS.GestionEAS.showOrUpdateForm(ref System.Windows.Forms.Form)' 的最佳重载方法匹配有一些无效参数
错误 2 参数 1:无法从 'ref package.Views.form' 转换为 'ref System.Windows.Forms.Form'
我该如何解决这个问题?
您不能将 myForm1
作为 ref Form
传递。
您应该将其更改为 ref T
,以便参数类型与变量匹配。
此外,您应该将 , new()
添加到您的通用约束中,并将 Activator.CreateInstance(typeof(T)) as T
替换为 new T()
看来,你想要这样的东西:
// I'd rather return potentially created form directly (not via "ref")
// do you need "this" in the method? That's why "static"
static T showOrUpdateForm<T>(T form = null)
where T: Form, new() {
if (null == form)
form = new T(); // <- no Activator since "new()" constraint is declared
form.WindowState = FormWindowState.Normal;
form.BringToFront(); // <- if the form is not a foreground one
if (form.CanFocus) // <- better check than have an exception
form.Focus();
return form;
}
...
// more compact and readable
myForm1 form = showOrUpdateForm<myForm1>();
这个怎么样,可以很好地满足创建具有不同参数的表单的需求
void showOrUpdateForm<T>(ref T form, Func<T> formCreator) where T : Form
{
if (form == null)
{
form = formCreator();
form.Show();
}
else
form.WindowState = FormWindowState.Normal;
if(form.CanFocus)
form.Focus();
}
将其用作 . . .
MyForm myForm = null;
form1 = showOrUpdateForm(myForm, () => new MyForm());