C# 导航或跳过表单

发布于 2024-11-14 08:45:05 字数 1140 浏览 0 评论 0原文

我早些时候学会了如何在两种形式之间来回移动。但如果有更多形式怎么办? 这是我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-11-21 08:45:05

第一个选项 - 您可以使用 UserControls 而不是 Forms,只需在要激活的控件上调用 BringToFront() 即可。

另一种选择 - 将应用程序状态管理移至某个对象。创建状态地图

public class StateManager
{
   private Dictionary<ApplicationState, Form> _stateMap = new Dictionary<ApplicationState, Form>();
   private ApplicationState _currentState;

    public void RegisterState(ApplicationState state, Form form)
    {
        if (_stateMap.ContainsKey(state))
            // throw an exception, or rewrite mapping

       _stateMap.Add(state, form);
    }

    public ApplicationState CurrentState
    {
        get { return _currentState; }
        set
        {
            if (!_stateMap.ContainsKey(value))
                // do nothing or throw exception

            if (_currentState == value)
                return;                

            CurrentForm.Hide();                
            _currentState = value;
            CurrentForm.Show();
        }
    }

    public Form CurrentForm
    {
        get { return _stateMap[_currentState]; }
    }
}

我在这里使用表单实例,但您可以通过工厂创建实例。

下一步 - 注册应用程序状态(当然,您应该为应用程序状态提供更有意义的名称):

StateManager stateManager = StateManager.Instance;
stateManager.RegisterState(ApplicationState.Form1, new Form1());
stateManager.RegisterState(ApplicationState.Form2, new Form2());
stateManager.RegisterState(ApplicationState.Form3, new Form3());

设置当前状态并运行应用程序:

stateManager.CurrentState = ApplicationState.Form1;
Application.Run(stateManager.CurrentForm);

最后一步 - 更改应用程序状态。您可以将 stateManager 实例传递给表单构造函数,或使用静态单例:

private void previousButton_Click(object sender, EventArgs e)
{
   StateManager.Instance.CurrentState = ApplicationState.Form1;
}

此外,您可以动态创建表单,使用配置文件进行状态定义等。

祝您好运!

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

public class StateManager
{
   private Dictionary<ApplicationState, Form> _stateMap = new Dictionary<ApplicationState, Form>();
   private ApplicationState _currentState;

    public void RegisterState(ApplicationState state, Form form)
    {
        if (_stateMap.ContainsKey(state))
            // throw an exception, or rewrite mapping

       _stateMap.Add(state, form);
    }

    public ApplicationState CurrentState
    {
        get { return _currentState; }
        set
        {
            if (!_stateMap.ContainsKey(value))
                // do nothing or throw exception

            if (_currentState == value)
                return;                

            CurrentForm.Hide();                
            _currentState = value;
            CurrentForm.Show();
        }
    }

    public Form CurrentForm
    {
        get { return _stateMap[_currentState]; }
    }
}

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):

StateManager stateManager = StateManager.Instance;
stateManager.RegisterState(ApplicationState.Form1, new Form1());
stateManager.RegisterState(ApplicationState.Form2, new Form2());
stateManager.RegisterState(ApplicationState.Form3, new Form3());

Set current state and run application:

stateManager.CurrentState = ApplicationState.Form1;
Application.Run(stateManager.CurrentForm);

And last step - change applications states. You can pass stateManager instance to forms constructors, or use static Singleton:

private void previousButton_Click(object sender, EventArgs e)
{
   StateManager.Instance.CurrentState = ApplicationState.Form1;
}

Further you can create forms dynamically, use configuration file for states definition etc.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文