0

I pasted the most important parts of my code below.

As you can see I'd like to work with multiple Forms. But this is how my Form behaves:

It opens Selector, when I press the second button it find the .ini file and opens ExplorerForm, but Selector IS STILL OPEN. Ofcourse I don't want that. I can't click the Selector, I just hear an error sound and the Explorer Window blinks. When I close the Explorer, both, the Explorer AND the Selector close. But NOW the Path form opens...

When I press one of the other buttons in the Selector, it doesn't find the .INI file (that's right) and opens the Path form (and closes it in the right way).

I already used search and even implemented one of the answers there:

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

My Program.cs:

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

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();
                }
            }
        }




    }
Community
  • 1
  • 1
Felix
  • 824
  • 8
  • 21
  • May you explain what is your expectation of when you open / close each form? – Mango Wong Aug 13 '15 at 04:59
  • Anyway, you should use `Form.Show()` method instead of `ShowDialog()`. As the remaining code after `ShowDialog()` would be executed after the dialog closed, which seems not to be your expectation. – Mango Wong Aug 13 '15 at 05:13
  • I see that you are closing the `Selector` form when one of the buttons is clicked. Since this is the main form (due to the line `Application.Run(new Selector())` this will kill the whole application. Is this intentionally? Maybe you can eleborate a bit more about you intentions. Since it isn't clear if you want to be able to open multiple windows or not, should the main window (the `Selector`) stay open or not, etc... – Maurits van Beusekom Aug 13 '15 at 05:26
  • @MangoWong I don't want the forms to close all others. – Felix Aug 13 '15 at 07:06
  • @MangoWong When I close the Selector form it should ONLY close the swelector form, but not all forms :-) I don't want a MAIN form that closes all others :-) – Felix Aug 13 '15 at 07:07
  • You can start a new thread and run `Path` on it, so closing `Selector` form would not affect `Path` form. I would also like to know the your step-by-step expectation after clicking any button on `Selector` form. Would `Selector` be closed immediately? And what would be the behavior of `Explorer` form? – Mango Wong Aug 13 '15 at 07:42
  • @MangoWong If you close one form via the X it should only close the current opened form. – Felix Aug 13 '15 at 12:09
  • @MangoWong If I open a new form with one of the buttons, the currently opened form should close and a new one should open. – Felix Aug 13 '15 at 12:09
  • Do you mean that at the beginning, there is only a Selector form. Then after pressing any buttons, specific `Path` form would pop-up and the `Selector` form would close? Would any two forms exist at the same time? And what is the use of `Explorer` form? – Mango Wong Aug 13 '15 at 12:36
  • @MangoWong Explorer is the form that will be opened after selecting the path. No, there won't be any 2 forms at the same time. – Felix Aug 13 '15 at 12:59

1 Answers1

1

Try modify involved code of your Selector.cs in this way:

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));
}

With this modification, a new thread would be initialized and the Path form will run on it. Then, the Selector form would close immediately. Hope this fit your use.

Mango Wong
  • 629
  • 6
  • 16
  • Thank you for your reply! I fixed my code by my own an hour ago. I didn't know that only the first Window caused the others to exit. That's why I always use this piece of code now: explorerForm.Closed += (s, args) => Application.Exit(); – Felix Aug 13 '15 at 16:29
  • But I hide all forms now instead of closing now. Doesn't matter, your solution works, too. That's why I select it as a working answer :-) – Felix Aug 13 '15 at 16:30
  • Why you would hide the forms instead of closing them if the forms are no longer used? For example, if you hide `Selector` form but not close it, the application thread would still be running. Therefore, even if you close the `Path` form later, the program would still be running. You would need further handling for this issue. – Mango Wong Aug 13 '15 at 16:36
  • newForm.Closed += (s, args) => Application.Exit(); That's why I'm using this line of code everytime before opening a new form. If the newForm gets closed, it closes all currently opened forms, too. If it gets hidden, it doesn't do anything. :-) – Felix Aug 13 '15 at 16:38
  • I do not understand your program logic well, but it is glad that you fix the problem. – Mango Wong Aug 13 '15 at 16:52