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?
Try Application.Exit();
It exits your entire application and closes all your forms and threads.
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.
The process is still running because form1 is still alive but hidden.
Try using Environmental.exit() to kill the process
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);
Related
I have two forms, Form1 and Form2, after opening Form1 I have to open Form2, if I click on Form1 close dialog that should not be closed before closing the Form2.
Use the Form.ShowDialog() method instead of Show() when you display the child form.
You can simply open Form2 using Form2.ShowDialog(): it this is called from Form1 code then Form2 is modal with respect to Form1 (that is you can interact no more with Form1 until you close Form2) and so the user is forced to close Form2 before being able to close Form1.
Please mark this answer as accepted if it responds your question.
Do you need interact with both windows?
If only Form2 gets the attention, you can solve it with the mentioned ShowDialog ().
Otherwise you can check with OnClosing if Form2 still open and refuse it.
Here you have a nice code snip to disable the close button:
http://social.msdn.microsoft.com/Forums/windows/en-US/32148556-2640-4d79-a7b6-f13f9ce3420a/how-to-enabledisable-the-close-buttonx-in-window-form-?forum=winforms
Code below. Maybe this is what you need.
private void btnExit_Click(object sender, EventArgs e)
{
this.Visible = false;
}
I have two forms in my code. When I click a button in Form1, it shows the second form (Form2). There is an ILPanel in Form2. The first time that I click the button, Form2 is shown without any problem, but if I close Form2 and then click the button on Form1 again, I get the following error message when Form2 is re-shown. Does anybody know why this is happening? Thank you.
The code is very simple but here it is again
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
This looks like a bug. One workaround - of course - is to reuse the form. "Closing" the form would not unload it, but only hide it. Clicking on button1 would only create the form for the first time and Show() it otherwise. That way, the OpenGL context (which seems to cause the problem) is not re-created every time you click on button1.
You can file a bugreport at http://ilnumerics.net/mantis
I have 2 forms in C# desktop application. Form1 and Form2.
Form1 contains a public method that adds item in ListBox control as follows:
public void AddToList(string item)
{
listBox.Items.Add(item);
}
When I call this method directly on some button press then it works fine. But when I call this method from Form2, it doesn't add anything in ListBox control on Form1. Code in Form2 is as below:
Form1 frm = new Form1();
frm.AddToList("something");
When I run this nothing happens. No error nothing. It just doesn't add any item into ListBox.
What am I doing wrong?
You're creating a new instance of Form1 and adding an item to it's listbox, rather than getting an instance of the Form1 you no doubt already have and calling the method on that.
The naive approach is to have a parameter in the Form2 constructor that takes an instance of Form1 and saves it as an instance variable for use in this event handler.
I don't much like that approach from a design perspective.
I would suggest creating a public event in Form2, having Form1 subscribe to that event and add a handler that add the item to the listbox. The Event in Form2 would look something like this:
public event EventHandler ButtonClick
{
add
{
button1.Click += value;
}
remove
{
button1.Click += value;
}
}
Then you'll have a property that looks something like this:
public string SomeValueForm1NeedsOnButtonClick
{
get
{
return texbox1.Text;
}
}
Then in Form1 you'll have something like:
Form2 otherForm = new Form2();
otherForm.ButtonClick += (sender, args) =>
{
listbox1.Items.Add(otherForm.SomeValueForm1NeedsOnButtonClick);
};
This approach ensures that each form only knows as little as possible about each other form. It reduces Coupling between the two classes and makes it clearer to future users/readers of the forms exactly what communication takes place between them.
Form1 frm = new Form1();
this line is creating a brand new instance of Form2... not the same instance that is already displayed on the screen. so you are adding to the listbox of this secondary instance that is never shown on the screen.
Hmm
If you followed that code with frm.Show() you would have seen it. Isuspect that's not waht you want.
Your approach to the is problem is a tad naive.
You could add a property to form2 and set it to the Form1 instnace you want to use. (PS form1 and form2 are not helping, give them propernames. MainForm and DetailForm or somesuch).
The problem with the above is you've implemented a horrible dependancy.
Lots of ways to go with this, one would be a seperate class to hold the list (an interface and a class would be even better).
Then add a property to Form1 and Form2 of teh ihneterface or class type.
Form2 can then add things to the list.
That raises a list changed event.
Form1 hooks into with an event handler and then refreshes the listbox it's using to display the the doings.
Once you have the infrasturure in polace you can do all sorts of things with it, whereas the method you are using is a lot of code and messing about for very little reward.
I have two forms ,for example form1 and form2
the form1 is parent form ,from form1 im calling form2 and show it ,but the problem is when second form "form2" has been opened the previous form will be inactive ?
Please tell me how to have multiple form active at same time.
You probably want to use Form.Show() instead of Form.ShowDialog(). The first one will show a form along side anther one while the other will "pause" the first form until you close the 2nd one.
Use
Form.Show()
Instead of
Form.ShowDialog()
set TopMost property of form2 to true
and then use form2.Show() instead of ShowDialog()
OR
you can open form 2 in another thread like what i done
private void ShowForm2()
{
new Form2().ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(ShowForm2));
th.Start();
}
How are you showing your 2nd form? Sounds like you're showing it in a modal way - you don't want to!
Copy and paste this code into your second form class:
protected override bool ShowWithoutActivation {
get {
return true;
}
}
When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).