C# 导航或跳过表单
我早些时候学会了如何在两种形式之间来回移动。但如果有更多形式怎么办? 这是我的form1代码:
Form2 form2 = new Form2();
private void aboutoldtrafford_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
form2.ShowDialog();
this.Show();
}
我可以转到form2,那里有两个按钮:后退和下一个
private void backbutton_MouseClick(object sender, MouseEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
Form3 form3 = new Form3();
private void nextbutton_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
form3.ShowDialog();
this.Show();
}
后退按钮将返回到form1,下一个按钮将转到form3。下面是我的 form3 代码。 在form3中,有两个按钮:后退和完成后退
private void back_MouseClick(object sender, MouseEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void finish_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
// i want to go back to form1
}
按钮将返回到form2,完成按钮将返回到form1。 显然,我不能做“this.DialogResult = DialogResult.OK;”在完成按钮中。 我怎样才能回到form1而不去form2?请帮忙...
i learned earlier how to move between two forms back and forth. but what if there's more forms?
this is my code for form1:
Form2 form2 = new Form2();
private void aboutoldtrafford_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
form2.ShowDialog();
this.Show();
}
i can go to form2 and there's two button there: back and next
private void backbutton_MouseClick(object sender, MouseEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
Form3 form3 = new Form3();
private void nextbutton_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
form3.ShowDialog();
this.Show();
}
back button will return to form1 and the next button will go to form3. below is my code for form3.
in form3, there are two buttons: back and finish
private void back_MouseClick(object sender, MouseEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void finish_MouseClick(object sender, MouseEventArgs e)
{
this.Hide();
// i want to go back to form1
}
back button will return to form2 and the finish button will go back to form1.
obviously, i cant do "this.DialogResult = DialogResult.OK;" in the finish button.
how can i go back to form1 without going to form2? please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个选项 - 您可以使用 UserControls 而不是 Forms,只需在要激活的控件上调用 BringToFront() 即可。
另一种选择 - 将应用程序状态管理移至某个对象。创建状态地图
我在这里使用表单实例,但您可以通过工厂创建实例。
下一步 - 注册应用程序状态(当然,您应该为应用程序状态提供更有意义的名称):
设置当前状态并运行应用程序:
最后一步 - 更改应用程序状态。您可以将 stateManager 实例传递给表单构造函数,或使用静态单例:
此外,您可以动态创建表单,使用配置文件进行状态定义等。
祝您好运!
First option - you can use UserControls instead of Forms and just call BringToFront() on control that you want to make active.
Another option - move application state management to some object. Create states map
I used here forms instances, but you can create instances via factory.
Next step - register states of application (of course, you should give more meaningful names for application states):
Set current state and run application:
And last step - change applications states. You can pass stateManager instance to forms constructors, or use static Singleton:
Further you can create forms dynamically, use configuration file for states definition etc.
Good luck!