【问题标题】:How to close multiple dialogs in the right way?如何以正确的方式关闭多个对话框?
【发布时间】:2015-08-13 04:42:47
【问题描述】:

我在下面粘贴了我的代码中最重要的部分。

如您所见,我希望使用多个表单。但这就是我的表单的行为方式:

它会打开 Selector,当我按下第二个按钮时,它会找到 .ini 文件并打开 ExplorerForm,但 Selector 仍处于打开状态。我当然不想那样。我无法单击选择器,我只是听到错误声音并且资源管理器窗口闪烁。当我关闭资源管理器时,资源管理器和选择器都会关闭。 但是现在路径表单打开了......

当我按下选择器中的其他按钮之一时,它找不到 .INI 文件(没错)并打开路径表单(并以正确的方式关闭它)。

我已经使用了搜索,甚至在那里实现了其中一个答案:

How I can close a 1st form without closing the full application?

我的 Program.cs:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Selector());

选择器.cs:

private void button1_Click(object sender, EventArgs e)
    {
        Path pathForm = new Path(0);
        this.Hide();
        pathForm.ShowDialog();
        this.Close();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        Path pathForm = new Path(1);
        this.Hide();
        pathForm.ShowDialog();
        this.Close();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Path pathForm = new Path(2);
        this.Hide();
        pathForm.ShowDialog();
        this.Close();
    }

Path.cs:

public Path(int currGame)
    {


        intGame = currGame;

        if(MyIni.KeyExists("Path"+intGame))
        {
        var GamePath = MyIni.Read("Path"+intGame);
            if(Directory.Exists(GamePath))
            {
                if (Directory.GetFiles(
                  GamePath, gameEXE(intGame), SearchOption.TopDirectoryOnly).Count() > 0)
                {

                    InitializeComponent();



                    Explorer explorerForm = new Explorer();
                    this.Hide();
                    explorerForm.ShowDialog();
                    this.Hide();
                 }
            }
        }

        InitializeComponent();
        label1.Text = label1.Text + " " + gameString(currGame) + "!";
        RegistryKey rk = Registry.LocalMachine;
        RegistryKey sk1 = rk.OpenSubKey(
              @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12100"); 
              // III Steam
        RegistryKey sk2 = rk.OpenSubKey(
              @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12110"); 
             // Vice City Steam
        RegistryKey sk3 = rk.OpenSubKey(
              @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12120"); 
             // San Andreas Steam

        if(intGame == 0)
        {
            if (sk1 != null)
            {
                if (sk1.GetValueNames().Contains("InstallLocation") 
                && sk1.GetValue("InstallLocation").ToString() != "")
                {
                    textBox1.Text = sk1.GetValue("InstallLocation").ToString();
                }
            }  
        }
        else if(intGame == 1)
        { 
        if(sk2 != null)
        {
            if(sk2.GetValueNames().Contains("InstallLocation") 
            && sk2.GetValue("InstallLocation").ToString() != "")
            { 
            textBox1.Text = sk2.GetValue("InstallLocation").ToString();
            }
        }
        }
        else if (intGame == 2)
        {
            if (sk3 != null)
            {
                if (sk3.GetValueNames().Contains("InstallLocation") 
                 && sk3.GetValue("InstallLocation").ToString() != "")
                {
                    textBox1.Text = sk3.GetValue("InstallLocation").ToString();
                }
            }
        }




    }

【问题讨论】:

  • 您能否解释一下您对打开/关闭每个表单时的期望是什么?
  • 无论如何,你应该使用Form.Show()方法而不是ShowDialog()。由于ShowDialog() 之后的剩余代码将在对话框关闭后执行,这似乎不是您的期望。
  • 我看到您在单击其中一个按钮时正在关闭Selector 表单。由于这是主要形式(由于Application.Run(new Selector()) 行,这将杀死整个应用程序。这是故意的吗?也许您可以详细说明一下您的意图。因为不清楚您是否希望能够打开是否有多个窗口,主窗口(@98​​7654330@)是否保持打开,等等...
  • @MangoWong 我不希望表单关闭所有其他表单。
  • @MangoWong 当我关闭 Selector 表单时,它应该只关闭 swelector 表单,但不是所有表单:-) 我不想要一个关闭所有其他表单的 MAIN 表单:-)

标签: c# .net windows forms


【解决方案1】:

尝试用这种方式修改你Selector.cs的相关代码:

private void button1_Click(object sender, EventArgs e)
{
    this.StartPathForm(1);
}
private void button2_Click(object sender, EventArgs e)
{
    this.StartPathForm(2);
}
private void button3_Click(object sender, EventArgs e)
{
    this.StartPathForm(3);
}
private void StartPathThread(int currGame)
{
    System.Threading.Thread pathThread = new System.Threading.Thread(PathThreadStart);
    pathThread.SetApartmentState(System.Threading.ApartmentState.STA);
    pathThread.Start(currGame);
    this.Close();
}
private void PathThreadStart(object currGame) {
    Application.Run(new Path((int) currGame));
}

通过此修改,将初始化一个新线程并在其上运行Path 表单。然后,Selector 表单将立即关闭。希望这适合您的使用。

【讨论】:

  • 感谢您的回复!一小时前我自己修复了我的代码。我不知道只有第一个 Window 导致其他 Window 退出。这就是为什么我现在总是使用这段代码:explorerForm.Closed += (s, args) => Application.Exit();
  • 但是我现在隐藏所有表单而不是现在关闭。没关系,您的解决方案也有效。这就是我选择它作为工作答案的原因:-)
  • 如果不再使用表单,为什么要隐藏表单而不是关闭它们?例如,如果您隐藏Selector 表单但不关闭它,应用程序线程仍将运行。因此,即使您稍后关闭Path 表单,程序仍会运行。您需要进一步处理此问题。
  • newForm.Closed += (s, args) => Application.Exit();这就是为什么我每次在打开新表单之前都使用这行代码。如果 newForm 被关闭,它也会关闭所有当前打开的表单。如果它被隐藏,它不会做任何事情。 :-)
  • 我不太了解你的程序逻辑,但很高兴你解决了这个问题。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多