I have a program which opens two forms
and I want when I click on Form1
then Focus on Form2.
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Focus();
}
But this doesn't work, what's wrong in my code?
EDIT:
I found already answered Here by #moguzalp at the comments
First of all that Form2 is never visible.
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.Focus();
}
If that Form is visible though with your code, that means you need to get same reference and call Focus() against it.
EDIT:
Then you need to have a reference to that Form.
At some point you created that Form and assigned it to a vairable/field or anything like that.
You need to call Focus or Activate against it.
Example:
Inside Form1 when you create a Form2 instance:
public class Form1 : Form
{
private Form _frm2;
//That code you probably have somewhere. You need to make sure that this Form instance is accessible inside the handler to use it.
public void Stuff() {
_frm2 = new Form2();
_frm2.Show();
}
private void Form1_Click(object sender, EventArgs e)
{
_frm2.Focus(); //or _frm2.Activate();
}
}
If you can have a form opened, try finding it:
using System.Linq;
...
// Do we have any Form2 instances opened?
Form2 frm2 = Application
.OpenForms
.OfType<Form2>()
.LastOrDefault(); // <- If we have many Form2 instances, let's take the last one
// ...No. We have to create and show Form2 instance
if (null == frm2) {
frm2 = new Form2();
frm2.Show();
}
else { // ...Yes. We have to activate it (i.e. bring to front, restore if minimized, focus)
frm2.Activate();
}
If you want to Show your frm2, you should call frm2.Show(); or frm2.ShowDialog();.
Also, before 'Show' call you can set frm2.TopMost = true; if you want this form to be on the top.
So it could be:
private void Form1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TopMost = true;
frm2.Show();
}
If you already opened the form elsewhere, then this code will not work as it's a new instance of Form2 and not the one that is opened.
You will have to keep a reference to the form that is opened, and then use Focus or might be better Activate on it.
if the form is opened from within Form1 then:
Add a field for holding current reference of Form2
Save it when showing the form.
Use it when focusing
private Form2 currentForm2;
....
this.currentForm2 = new Form2();
this.currentForm2.Show();
...
...
this.currentForm2.Activate();
Related
So I want to show a form and close the other form so I did this in form1:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
//didn't use this.close() cause then the whole program will close instead of
//just the current form
}
Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 x = new Form1();
x.Show();
}
Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background.
what should I do to make the form get closed when I exit it?
I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.
When you write:
Form1 x = new Form1();
you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.
Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:
// ... all in Form1 ...
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 x = new Form2();
x.FormClosed += X_FormClosed;
x.Show();
}
private void X_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
So then when you close Form2 your instance of Form1 will automatically re-appear.
I have Form1 and its my main form.
In some part of my code, I do this:
Form2 f2=new Form2();
f2. ShowDialog()
I want to do following: In Form1, I want to detect when the Form2 closed and do some stuff in that case?
How to do that?
First of all you need to change your .ShowDialog(); to .Show(this); as your code after calling second form would be run after form closed.
You should create a new method for Closing event like this:
private void FormIsClosing(object sender, FormClosingEventArgs e)
{
// Some stuff you want to do on close
}
Then somewhere where you create your Form2 you need to assign this method to Closing event. Do it like this:
var f2 = new Form2(); // Create a form
f2.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormIsClosing); // Add event handler
f2.Show(this); // Run this form
This should do the trick
try this:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
f2.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form2_FormClosed);
f2.ShowDialog();
}
void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("my code");
}
I want to do following: In Form1, i want to detect when the Form2 closed and do some stuff in that case?
"Detection" is not necessary as you are using a modal dialog. You could add an intercept in the dialog class but unless you want to prevent closure, simply perform the following.
Change:
Form2 f2=new Form2();
f2.ShowDialog()
...to:
Form2 f2=new Form2();
f2.ShowDialog(); // this will block your app until Form2 closes
// If we get here, then it means Form2 has closed
// Add your code here
// ...
// ...
// ...
I've got three forms, Form1, Form2 ,Form3.
A button in Form1 can open Form3 and a button in Form2 can also open Form3.
When either button is pressed the respective form is hidden and Form3 is opened.
When Form3 is closed it should open the form that has been hidden.
How would I go about doing this?
Form.Show method can take OwnerForm as argument so call it like that:
var frm = new Form3();
frm.Show(this);
you can access parent in Form3 by property Owner so in closing event:
private void FormIsClosing(object sender, FormClosingEventArgs e)
{
var owner = this.Owner;
if (owner != null)
{
owner.Show();
}
}
Another way is to use Acivated and FormClosed events.
Let's say in Form1 you Click a Button to show Form2
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Activated += new EventHandler(frm2_Activated);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
}
Now, this one is when the Form2 showed or is Activated you hide the calling form, in this case the Form1
private void frm2_Activated(object sender, EventArgs e)
{
this.Hide(); // Hides Form1 but it is till in Memory
}
This one when the Called form is Closed in this case Form2 it will then show the hidden Form1 again.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show(); // Show hidden Form1
}
I am trying to detect that form2 is closed in form1. I have this so far
private void AddStageBtn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
if (form2.IsDisposed)
{
MessageBox.Show("it was closed!");
}
}
Any suggestions? Thanks again!
Adhere to the FormClosed event of form2.
Wherever you create it do:
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
Then create the method:
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Do whatever you want here
}
You'll also want to use .Show() instead of .ShowDialog() if you want to be able to use either form, otherwise form1 will be unavailable until form2 is closed (which I am assuming is not the behavior you are looking for).
In this particular situation
private void AddStageBtn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
MessageBox.Show("it was closed!");
}
will work fine. If you want to be able to do action within form1 while form2 is open you need to use Show instead of ShowDialog. Then you can create a handler for form closed within form1.
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
I am working in c# windows application.I have two windows from name as form1 and form2.i am calling form2 by clicking the button in form1 but i create the object for form2 in constrcutor of form1 .if i click the button first time form2 showed successfully after that i close the form2 by clicking the default close button and again click the button now i am getting object disposed exception was unhandled exception.how can avoid this?
Don't handle the exception, fix the bug in your code. The form instance is dead after the form is closed, you cannot show it again. Either write it like this:
private void button1_Click(object sender, EventArgs e) {
var frm = new Form2();
frm.Show(this);
}
Or if you want only one instance of the form to be ever visible:
Form2 theForm;
private void button1_Click(object sender, EventArgs e) {
if (theForm != null) {
theForm.WindowState = FormWindowState.Normal;
theForm.BringToFront();
}
else {
theForm = new Form2();
theForm.FormClosed += delegate { theForm = null; };
theForm.Show(this);
}
}
You are keeping a reference to the object (window here) but you are closing it. Object is disposed but is not garbage collected. Your reference here is invalid now as the object has lost its usable state.
You need to hide the form instead of close if you need to re-use it. Or create a new instance to load it again.
You can use events in order to let form1 know when form2 has been closed and clear its reference to it. Then form1 doesn't need to call form2 if it has been closed.
We do something similar here with a few of our tools that plug into third-party apps. Code sample below:
public class Form1 : Form
{
private Form2 otherForm;
private void ActivateForm2_Click(object sender, EventArgs e)
{
if (otherForm == null || otherForm.IsDisposed)
{
otherForm = new Form2();
otherForm.FormClosed += new FormClosedEventHandler(otherForm_closed);
}
otherForm.Show(this);
}
private void otherForm_Closed(object sender, FormClosedEventArgs e)
{
otherForm.Dispose();
otherForm = null;
}
}