Bring form2 to front from a deactivated form1 - c#

I have a form1 that opens form2 as a "fake popup"
this.enabled = false;
MyForm2 myform2 = new MyForm2();
myform2.Show();
myform2.BringToFront();
When i dispose form2 i enable form1 back.
Now if the user minimizes both forms, and then clicks on form1, form1 will pop in his disenabled state.
I need to pop form2 insted.
So I created a static class to hold my last active form. On load of form2(or any other form) i save my last active form.
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
MyStaticClass.lastForm = this;
}
In form1(and other similar forms) i use:
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
if (!this.Enabled && MyStaticClass.lastForm!= null && MyStaticClass.lastForm != this)
{
MyStaticClass.lastForm.Show();
MyStaticClass.lastForm.BringToFront();
MyStaticClass.lastForm.Activate();
}
}
OnGotFocus gets executed and my show/activate etc does too, but form2 never pops up. What am I doing wrong?
Thank you,
gg

You are "showing" and "activating" your form, the problem is it's still minimized. Change the WindowState to Normal.
MyStaticClass.lastForm.WindowState = FormWindowState.Normal;

I think it would be confusing to the user to click on one window's taskbar button and get another.
Instead of going through this rigamarole, try calling ShowDialog() instead of Show() in form1; this will block all access to Form1 until Form2 closes. ShowDialog also has an overload that accepts a "parent" or "owner" form; if this overload is used (just pass this), the dialog will always show in front of Form1.

Related

winform show windows to Foreground replace temporary form dialog

I have a WinForm program,have two Form.form1 is main form,watch a socket port.when Request arrive,show from1:
this.Show();
this.ShowInTaskbar = true;
and when user click form1's button,I hide from1 show form2:
this.Hide();
this.ShowInTaskbar = false;
From2 form = new From2 (info);
var value = form.ShowDialog();
So far so normal.when From2 click OK,I want show Form1 again,but Form1 Often be covered by other programs:
if (DialogResult.OK == value)
{
reponse();
}
this.Show();
this.ShowInTaskbar = true;
I want form1 show seems to replace form2 position.how can i do?
You should call ShowDialog passing the current form as the owner:
var value = form.ShowDialog(this);
Do this instead of hiding form1. This keeps the two forms linked together in the windows Z-Order and automatically disables input on form1 whilst form2 is displayed on top of it. So when the time comes for the user to perform any activity of form2, even if it had previously been lost in the Z-Order, when they bring form2 to the front, form1 will come forwards also.

WinForm Show and Hide Puzzle

I am having problems showing and hiding two forms.
My app begins by creating a form with one button (btnToggle) and one checkbox and another form which remains hidden (form2).
I am not getting the behaviour I am expecting which is explained below.
private void btnToggle_Click(object sender, EventArgs e)
{
// note that form1 is big enough to contain form2 but form1 not maximised and form2 is not a modal form
// start with form1 visible form2 not visible chk box not checked
// click btnToggle and form2 is shown on top of form1
// click on form1 form2 now behind form1
// check chksecondFormAlwaysOnTop
// click btnToggle form2 is shown on top of form1 but
// should not go behind form1 when form1 is clicked but it does.
// what is wrong with the code below.
// I want form2 to always be on top when chksecondFormAlwaysOnTop
// is checked but it isnt.
// My code below:
if (Form2.Visible && Form2.TopMost)
{
Form2.Hide();
}
else if(Form2.Visible && !Form2.TopMost )
{
Form2.BringToFront();
}
else if (!Form2.Visible && chksecondFormAlwaysOnTop.Checked)
{
Form2.Show();
Form2.TopMost = true;
}
else
{
Form2.Show();
Form2.TopMost = false;
}
}
What you really need is to use the Form.Owner property
To make a form owned by another form, assign its Owner property a reference to the form that will be the owner.
When a form is owned by another form, it is closed or hidden with the owner form. For example, consider a form named Form2 that is owned by a form named Form1. If Form1 is closed or minimized, Form2 is also closed or hidden.Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not disappear when the owner form is selected.
Assuming you have the following declaration in your Form1 class
Form2 Form2;
Inside your Form1 load event, put the following
Form2 = new Form2 { Visible = false, Owner = this };
and then use simple
private void btnToggle_Click(object sender, EventArgs e)
{
Form2.Visible = !Form2.Visible;
}

Closing multiple windows form with single click

I want to close two forms at the same time. I have one main form that starts with program. when user click button, the main form will hide and other form will pop up. on the second form if user click "back to main " button, it should hide second form and show main form. But the problem is if user tries to close the second form it should close the main form as well. How can i close the main form as well
I would just use the Application.Exit() for what is requested by this thread.
Application.Exit();
UPDATE: corrected
I had said this will not call the form closing events but in documentation it does actually call it here is a link to the documentation
http://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx
It was better if you specified what codes you wrote for going back to main form, so I could help you by changing your codes. But now because I don't know how you did it, I have to write codes for both of those tasks.
It can be possible using a Boolean variable to do what you want. Follow bellow codes:
public partial class MainForm : Form
{
//"Click" event of the button that should opens the second form:
private void goToSecondForm_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //Or you can write it out of this method.
this.Hide(); //Hides the main form.
f2.ShowDialog(); // Shows the second form.
this.Show(); // Shows the main form again, after closing the second form using your own button.
}
}
public partial class Form2 : Form
{
bool selfClose = false; //False shows that user closed the second form by default button and true shows that user closed it by your own button.
//"Click" event of the button that should closes just the second form and returns user to the main form:
private void ownCloseButton_Click(object sender, EventArgs e)
{
selfClose = true; //Means user clicked on your own button.
this.Close(); //So the program closes the second form and runs f2_FormClosed method, but because selfClose became true here, happened nothing there and program will go back to goToSecondForm_Click method in the main form and will run this.Show() .
}
//"FormClosed" event of the second form :
//Whether user clicked on your own button or on the default one, this method will run.
private void f2_FormClosed(object sender, FormClosedEventArgs e)
{
if (!selfClose) //It means user didn't click on your own button and both of forms must be closed .
Application.Exit(); //So the program closes all of forms (actually closes the program) and couldn't access to any other commands (including this.Show() in goToSecondForm_Click method).
}
}
As others have said, you need to somehow call .Close() on the main form when your child form is closed. However, as you've pointed out, you don't have a reference to the main form automatically in your child form! That leaves you with a few options.
1. Exit the application immediately.
This is done by calling Application.Exit(); in your child form's "back to main" button's click event handler. It will immediately close all forms, which might simply be what you want.
// .. ChildForm code ..
void OnBackToMainClicked(object sender, EventArgs e)
{
Application.Exit();
}
2. Pass a reference to the main form to the child form.
This is probably the most common way to solve this problem in general. When you create your child form in your main form, you'll need to pass a reference as follows:
// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
var childForm = new ChildForm(this);
childForm.Show();
}
// .. ChildForm code ..
private MainForm mainForm; // This is where the child form will keep a reference to
// the main form that you can use later
public ChildForm(MainForm mainForm)
{
// This is the child form's constructor that we called above,
// and it's where we'll save the reference to the main form
this.mainForm = mainForm;
}
// This also needs to be the event handler for the close event
void OnBackToMainClicked(object sender, EventArgs e)
{
this.Close();
mainForm.Close();
}
3. Add an event handler on the child form's FormClosed event. This is a safe way to solve the problem if you are concerned about keeping your main application logic under the control of the main form. It's similar to the solution suggested by Lamloumi above, but it's all done in the main form's code.
// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
var childForm = new ChildForm(this);
childForm.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
childForm.Show();
}
void SecondForm_FormClosed(object sender, EventArgs e)
{
// Perform any final cleanup logic here.
this.Close();
}
Form1 _FirstForm = New Form1();
Form2 _SecondForm = New Form2();
MainForm _MainForm = new MainForm();
_FirstForm.Close();
_SecondForm.Close();
_MainForm.Show();
Normally , in the Home form you have some ting like this :
SecondForm second= new SecondForm ();
second.Show();
this.Hide();
In the SecondForm you must ovveride the event of closure like this :
public class SecondForm :Form{
public SecondForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
}
void SecondForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
To be sure that your application is closed after you close the form. because the Home from is still active and hidden if you don't.
use this in Form2
private void button1_Click(object sender, EventArgs e)
{
Form1.FromHandle(this.Handle);
}
There Handle are the same now ;
hope this work.

Hiding a form, switch to a second form, close second form and unhide first form

I've looked at all the suggested answers and nothing seems to fit what I'm looking for. I want to call a second form from my main form, hide my main form while the second form is active, and then unhide the main form when the second form closes. Basically I want to "toggle" between the two forms.
So far I have:
In my main form:
private void countClick(object sender, EventArgs e)
{
this.Hide();
subForm myNewForm = new subForm();
myNewForm.ShowDialog();
}
and in my second form I have:
private void totalClick(object sender, EventArgs e)
{
this.Close();
}
How do I get the main form to show?
ShowDialog opens your secondary Form as Modal Dialog, meaning that the MainForm's code execution will stop at that point and your secondary Form will have the focus. so all that you need to do is put a this.Show after your ShowDialog call.
From above link:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
private void countClick(object sender, EventArgs e)
{
this.Hide();
subForm myNewForm = new subForm();
myNewForm.ShowDialog();
this.Show();
}
Let's say in Form1 you click a Button to show Form2
Form2 frm2 = new Form2();
frm2.Activated += new EventHandler(frm2_Activated); // Handler when the form is activated
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed); // Hander when the form is closed
frm2.Show();
Now, this one is when the Form2 is shown 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
}
Then when Form2 is Closed it will Unhide Form1.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show(); // Unhide Form1
}
This is difficult to do correctly. The issue is that you must avoid having no window at all that can get the focus. The Windows window manager will be forced to find another window to give the focus to. That will be a window of another application. Your window will disappear behind it.
That's already the case in your existing code snippet, you are hiding your main window before showing the dialog. That usually turns out okay, except when the dialog is slow to create. It will definitely happen when the dialog is closed.
So what you need to do is hide your window after you display the dialog and show it again before the dialog closes. That requires tricks. They look like this:
private void countClick(object sender, EventArgs e)
{
this.BeginInvoke(new Action(() => this.Hide()));
using (var dlg = new subForm()) {
dlg.FormClosing += (s, fcea) => { if (!fcea.Cancel) this.Show(); };
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
The BeginInvoke() call is a trick to get code to run after the ShowDialog() method runs. Thus ensuring your window is hidden after the dialog window is shown. The FormClosing event of the dialog is used to get the window to be visible again just before the dialog closes.
You need to find some way to pass a reference to the main form to the second form click event handler.
You can do this either by setting the form as a member variable of the second form class or pass it via the event arguments.
If you are working in the same namespace, you have the context, using mainform or the name you gave the "main form", try:
mainform.show();

Winforms: bring 2 obscured forms to front

I'm trying to solve this problem for a long time.
I have 2 forms, my objectives are:
When user minimize form2, form1 must minimize too.
When user maximize form2, form1 must maximize too.
When both forms are obscured by another window, and user clicks in the form2 icon in taskbar, form1 must also come to front.
The first 2 things I solved with the a_Resize method. But I can't do the third one. I tried with activate event but when I do that the form2 keeps blocked.
Here is my code:
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show();
form2.Resize += new EventHandler(a_Resize);
}
void a_Resize(object sender, EventArgs e)
{
if (((Form)sender).WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Minimized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
}
If I add a handler to the form2 activate event:
form2.Activated += new EventHandler(form2_Activated);
And call for instance the Focus method (I tried other methods too), the form2 keeps blocked behind form1.
void form2_Activated(object sender, EventArgs e)
{
this.Focus();
}
Someone have any ideas how I can do that?
When you create form2, just pass this as a parameter to Show() to signify that form1 is the owner. With an owner link, the forms will always be raised together (at least in my experience -- I don't have a specification to back me up on this).
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show(this); //pass 'this' as argument to Show() to link them
form2.Resize += new EventHandler(a_Resize);
}

Categories