0

I'm trying to work with multiple forms, what I want is change the way the form is depending on the selected index of a combobox, the only way I could think of is hide form1 and show form2, but the problem is when you close form2, the process does not end...I tried the code below

private void Form2_FormClosing(object sender, FormClosedEventArgs e)
    {
        foreach (var process in Process.GetProcessesByName("Process Name.exe"))
        {
            process.Kill();
        }
    }

if there isn't, is there a way the form can change on combobox selected index?

MiRaN
  • 641
  • 1
  • 6
  • 11
  • My crystal ball says that you'll see a "First chance exception" notification when you look in the Output window. Caused by Kill() failing. Why this exception is swallowed with the debugger not saying anything is explained in [this post](http://stackoverflow.com/a/4934010/17034). – Hans Passant Jul 05 '15 at 11:48

4 Answers4

1

Try Application.Exit();

It exits your entire application and closes all your forms and threads.

Afshin Aghazadeh
  • 547
  • 4
  • 17
1

Simply pass an instance of Form1 to the constructor of Form2, keep a reference to it in a form1 member

public class Form2 : Form{
    private Form _form1;

    public Form2(Form form1):this()
    {
        _form1 = form1;
        InitializeComponent();
    }
}

later you can simply use that reference :

_form1.Close();

This is a cleaner way to do it. Other mechanisms are also ok, like implementing an eventhandler on form1 for an event in form2.

based on your pastebin code change this:

Form2 HeadquarterForm = new Form2(this);

you also only need the closed eventhandler and call close on the _form1 only once. So you don't really need the closing event handler.

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
  • I just tried that with `Form2_FormClosed` and `Form2_FormClosing` and it still didn't work :/ – MiRaN Jul 05 '15 at 10:22
  • If you don't show any code I cannot figure out what you are doing wrong. – Philip Stuyck Jul 05 '15 at 10:24
  • okay so...on combobox1, when the index changes to a specific one, it does `Form2 HeadquarterForm = new Form2(); HeadquarterForm.Show(); this.Hide();` so it hides this which is Form1 and shows Form2, and I added your code in Form2, and I tried the `_form1.Close();` in `Form2_FormClosing` and `Form2_FormClosed`, but it didn't work :/ – MiRaN Jul 05 '15 at 10:29
  • added some code in my answer based on your pastebin – Philip Stuyck Jul 05 '15 at 11:22
  • **(System.Windows.Forms.Form) is inaccessible due to its protection level** – MiRaN Jul 05 '15 at 11:27
  • on this line `Form2 HeadquarterForm = new Form2(this);` – MiRaN Jul 05 '15 at 11:34
  • change class Form2 to public class Form2. Just put public in front of what you have. – Philip Stuyck Jul 05 '15 at 11:36
  • your comment didn't fix it, I fixed it by changing `public Form2() { InitializeComponent(); }` to `protected Form2() { InitializeComponent(); }` but now it gives me this error on the same line **'MW3_Config_Creator.Form2' does not contain a constructor that takes 1 arguments** – MiRaN Jul 05 '15 at 11:41
  • You only need the constructor with the parameter, I changed my answer accordingly. That constructor needs to be public. – Philip Stuyck Jul 05 '15 at 11:45
  • ok now there are no errors but it still doesn't end the process – MiRaN Jul 05 '15 at 11:48
  • Is the event handler even correct. How did you create it ? I hope using the IDE by double clicking on the close event and then filling in the code in the editer. Just adding the method without using the IDE will not work in your case. – Philip Stuyck Jul 05 '15 at 11:49
0

The process is still running because form1 is still alive but hidden.

Try using Environmental.exit() to kill the process

Stevie
  • 31
  • 1
  • 1
0

Looking at your code in pastebin. Problem is, you're not passing Form1 in constructor of your Form2 when creating it. Change the part of your switch-case (4) to:

Form2 HeadquarterForm = new Form2(this);
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • thank you but Philip mentioned that and it gave me this error after I added `this` between the brackets **(System.Windows.Forms.Form) is inaccessible due to its protection level** – MiRaN Jul 05 '15 at 11:29
  • @user3542613 Try declaring your Form1 and Form2 as public class Form1 and public class Form2. – msmolcic Jul 05 '15 at 11:30
  • I fixed the error in the comment above by changing `public Form2() { InitializeComponent(); }` to `protected Form2() { InitializeComponent(); }` but now it gives me this error **'MW3_Config_Creator.Form2' does not contain a constructor that takes 1 arguments** on the line in your answer – MiRaN Jul 05 '15 at 11:37
  • That's strange, it should have constructor that takes 1 argument if you still have: Form2(Form form1) : this() { _form1 = Form1; } like in your pastebin. – msmolcic Jul 05 '15 at 11:39