C# 如何使(Windows)窗体成为参数?
对大多数人来说,这可能是一个相当简单的问题,但我只是一个初学者,而且真的很好奇。我正试图将Form1(一个Windows窗体..)和另一个窗体(比如Form2)都变成函数的参数 我有一个类,其中包含所有表单中最常见的按钮代码 我当前尝试执行的代码是打开一个新表单并隐藏上一个表单:C# 如何使(Windows)窗体成为参数?,c#,winforms,C#,Winforms,对大多数人来说,这可能是一个相当简单的问题,但我只是一个初学者,而且真的很好奇。我正试图将Form1(一个Windows窗体..)和另一个窗体(比如Form2)都变成函数的参数 我有一个类,其中包含所有表单中最常见的按钮代码 我当前尝试执行的代码是打开一个新表单并隐藏上一个表单: public static void navigationButton(Form currentForm, Form nextForm) { currentForm.Hide(); nextForm f
public static void navigationButton(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm form = new nextForm();
form.Show();
}
我得到的错误出现在nextForm form=new nextForm()
上,它说“nextForm是一个变量,但像类型一样使用。”
我正在尝试这样做,这样我就不必不断地重复三行代码,我只需编写类似于navigationButton(Form1,Form2)
使Windows窗体成为参数的正确方法是什么?或者这个想法无效?您只需使用第二个表单参数,如下所示:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
然后你应该这样称呼它:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
您只需使用第二个form参数,如下所示:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
然后你应该这样称呼它:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
让我给出一个答案(我不能得到100%的正确性,但是…)
让我们看看你的代码
public static void navigationButton(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm form = new nextForm();
form.Show();
}
首先,在nextForm form=new nextForm()
中有一个错误,因为nextForm是类form的对象。其次,生成的表单对象将被标记为gc,并以不确定的时间/方式销毁(所以,如果我没有弄错的话),因为您不会返回任何关于它的引用。
第三,最好为代码创建“语法糖”。
那么,让我们用可能的方式重写它:
public static void NavigationButton(this Form currentForm, out Form nextForm)
{
currentForm.Hide();
nextForm = new Form();
nextForm.Show();
}
您可以用相同的方式调用代码:
Form testForm;
myForm.NavigationButton(out testForm);
myForm.NavigationButton(out Form testForm);
更新1:
在C#7.0中,您可以用相同的方式编写代码的最后一部分:
Form testForm;
myForm.NavigationButton(out testForm);
myForm.NavigationButton(out Form testForm);
更新2:
如果要创建“广泛”语法,请使用以下代码:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
publicstaticvoidnavigationbutton(此表单为currentForm,out表单为nextForm),其中T:Form
{
currentForm.Hide();
nextForm=(T)Activator.CreateInstance(typeof(T));
Show();
}
使用myForm.NavigationButton(OutForm2 testForm)代码>(适用于C#7.0)或
form2testform;
myForm.NavigationButton(OutTestForm);
一般来说。让我给出一个答案(我不能得到100%的正确性,但是…)
让我们看看你的代码
public static void navigationButton(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm form = new nextForm();
form.Show();
}
首先,在nextForm form=new nextForm()
中有一个错误,因为nextForm是类form的对象。其次,生成的表单对象将被标记为gc,并以不确定的时间/方式销毁(所以,如果我没有弄错的话),因为您不会返回任何关于它的引用。
第三,最好为代码创建“语法糖”。
那么,让我们用可能的方式重写它:
public static void NavigationButton(this Form currentForm, out Form nextForm)
{
currentForm.Hide();
nextForm = new Form();
nextForm.Show();
}
您可以用相同的方式调用代码:
Form testForm;
myForm.NavigationButton(out testForm);
myForm.NavigationButton(out Form testForm);
更新1:
在C#7.0中,您可以用相同的方式编写代码的最后一部分:
Form testForm;
myForm.NavigationButton(out testForm);
myForm.NavigationButton(out Form testForm);
更新2:
如果要创建“广泛”语法,请使用以下代码:
public static void Navigate(Form currentForm, Form nextForm)
{
currentForm.Hide();
nextForm.Show();
}
Navigate(activeForm, new Form2());
public static void NavigationButton<T>(this Form currentForm, out Form nextForm) where T: Form
{
currentForm.Hide();
nextForm = (T)Activator.CreateInstance(typeof(T));
nextForm.Show();
}
publicstaticvoidnavigationbutton(此表单为currentForm,out表单为nextForm),其中T:Form
{
currentForm.Hide();
nextForm=(T)Activator.CreateInstance(typeof(T));
Show();
}
使用myForm.NavigationButton(OutForm2 testForm)代码>(适用于C#7.0)或
form2testform;
myForm.NavigationButton(OutTestForm);
一般来说。假设您有两种形式;我们称之为formIndex和formNext 如果我没弄错的话,您在formIndex中有一个按钮,单击该按钮就会打开一个新窗口。我们叫按钮1
private void button1_Click(object sender, EventArgs e) {
// User clicks on button, close this form, open next
NavigateToNextForm(this, new FormNext(this));
// Call next method with instance of current object and new form
// Passing current instance to next form, so we can navigate back
}
internal void NavigateToNextForm(Form formIndex, Form formNext) {
// Assuming you want to return back to this form,
// we'll just hide the window
formIndex.Hide();
// Now we'll show the next one
formNext.Show();
}
当然,根据您想要实现的具体目标,您可能希望等待下一个表单关闭(并可能返回DialogResult)
在这种情况下,您可以使用异步等待模型和Task.Run(Action)。假设您有两种形式;我们称之为formIndex和formNext 如果我没弄错的话,您在formIndex中有一个按钮,单击该按钮就会打开一个新窗口。我们叫按钮1
private void button1_Click(object sender, EventArgs e) {
// User clicks on button, close this form, open next
NavigateToNextForm(this, new FormNext(this));
// Call next method with instance of current object and new form
// Passing current instance to next form, so we can navigate back
}
internal void NavigateToNextForm(Form formIndex, Form formNext) {
// Assuming you want to return back to this form,
// we'll just hide the window
formIndex.Hide();
// Now we'll show the next one
formNext.Show();
}
当然,根据您想要实现的具体目标,您可能希望等待下一个表单关闭(并可能返回DialogResult)
在这种情况下,您可以使用异步等待模型和Task.Run(Action)。form=newform();或形式=新形式();您尝试在代码中创建类“nextForm”的新对象。但是没有类“nextForm”。))您也需要以相同的方式通过引用传递nextForm。谢谢@dmitry的评论!但是我如何调用Form1中的函数?我试着做
ButtonControl.navigationButton(这个,表单2)
给了我一个错误,说“Form2”是一个类型,在给定的上下文中无效。”我回答了一个答案Yes,ypu将得到这个错误。如果我没弄错的话,你想创造“语法糖”,对吗?因此,您需要在静态方法定义(navigationButton(此表单currentForm,表单nextForm))中添加“this”。还有,Form2是什么?这是一个新的类还是对象?不能在函数中传递类,只能在对象中传递。另外,例如,您非常需要传入传出,但我不知道WinForms是否可以传入传出。它应该类似于导航按钮(此表单为currentForm,out表单为nextForm)。因此,您的代码应该是这样的:formtestform;myForm.navigationButton(OutTestForm);我写了答案,您可以尝试在代码中实现它。所以,它也适用于从类继承的所有类,但您需要使用反射来创建正确类型的表单继承类;或形式=新形式();您尝试在代码中创建类“nextForm”的新对象。但是没有类“nextForm”。))您也需要以相同的方式通过引用传递nextForm。谢谢@dmitry的评论!但是我如何调用Form1中的函数?我试着做ButtonControl.navigationButton(这个,表单2)代码>给我一个错误,说“'Form2'是