I have C# windows form application, with all default settings. I am using VS 2008, OS - Windows VIsta.
When my form loses focus, (as in when a user clicks on something behind the form), i need the same form that lost focus to regain it.
I made use of this Event to handle this;
private void Form1_Deactivate_1(object sender, EventArgs e)
{
Console.WriteLine("DEACTIVATE EVENT _______+++++++++_________");
Form1 f = new Form1();
f.show();
}
Here, what you will see is that is when the form loses its focus, the Console.writeline command will execute and a new form will appear on the screen. I do not want this. i want the exact form that lost focus to regain focus and appear back on the screen. How do i do this.
Form.Activate methods activates the form and gives it focus:
form.Activate();
Form.TopMost Property indicates whether the form should be displayed as a topmost form.
A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.
form.TopMost = true;
Related
I have a system in which the main form is the menu, and it pops up a login form on loading.
Originally it loaded the login form below the menu, so I used this.topmost = true to make it come to the front. (as bring to front and send to back did not work)
However, if the user then clicks on something else, say chrome, then it still stays at the top of the z-order, by definition of topmost.
I tried to use the deactivate event, but this meant that on load it once again comes up behind the menu form.
How could I stop it from loading behind my menu form, and yet when it loses focus stop it from being topmost?
private void login_Deactivate(object sender, EventArgs e)
{
// do not want it to remain top most when the application is not in focus.
this.TopMost = false;
}
In the Menu form:
private void Menu_Load(object sender, EventArgs e)
{
openLogin()
}
private void openLogin()
{
Cursor.Current = Cursors.WaitCursor;
login theForm = new login(this);
this.Enabled = false;
theForm.Show();
Cursor.Current = Cursors.Default;
theForm.Activate();
theForm.TopMost = true; // Make the login form display over the Menu
}
Try setting Login's Form Owner property to the menu form.
From above MSDN link:
When a form is owned by another form, it is closed or hidden with the
owner form. ... 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. To determine the forms that are owned by a parent form, use
the OwnedForms property
.
Assuming this is a Win Forms application, then try changing theForm.Show() to theForm.ShowModal()
I have two open winforms, say that winform1 gets a modal dialog, this means that all forms(winform1 and winform2) will be "disabled". If we minimize all forms and then bring up winform1, then the modal dialog will be shown above it. If we again minimize all the forms but this time brings up winform2, it will look like the finform2 is ready to be used while its really disabled like winform1.
What I need is to clearly show that the modal dialog needs to be handled before using winform2 again.
Is there anything built in to handle this or am I on my own here?
In your winform2.Activated event handler, call this:
static void FocusModalForm()
{
foreach (Form form in Application.OpenForms)
if (form.Modal)
{
form.WindowState = FormWindowState.Normal;
form.BringToFront();
}
}
e.g.
Form f2 = new Form();
f2.Activated += (_, __) => FocusModalForm();
f2.Show();
You may need to do the same thing for winform1's Activated event. It depends how winform2 gets created. Just try it and if you find that winform1 (or any other nonmodal form) is still able to get in front of the modal form, just call FocusModalForm() from its Activated event.
I tried this in Windows 7. I tried hiding all windows (click the Show Desktop button on the taskbar) and then selecting form2 directly from the taskbar, and I also tried just selecting form2 from the taskbar without hiding all windows. Form3 always stayed on top.
I have a similiar app (vb.net) where win1 calls win2 & win2 displays win3 and it works as you would like but wins 2 & 3 are both modal. I don't know if that is why it works or not. Perhaps that is an option for you?
If you do this:
var winform2 = new Winform2();
winform2.Show(winform1);
Then winform2 will always be shown above winform1, but it won't be modal. May be this can help you.
Currently I am developing a windows form application in c# that has several forms.
I am running a background form that operates the notifyicon property that allows the icon to appear in the taskbar.
When I launch the program, it will launch a loginForm, after which logging in it will go into a mainForm. After closing the mainForm, the application does not close yet, which in this case works like Windows Live Messenger.
How do I make my program in a way that after I the mainForm, through double clicking it will bring the form back up? (Like how MSN works.)
Or is it a better solution for me to close the whole application when I press the X button in the title bar. Which brings up another problem for me as I cant seem to exit the application when I close other forms other than the main form.
Probably you have NotifyIcon on your main form. Subscribe on the DoubleClick event of this control and change state of your main form in the handler:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
Just set the Visible property of the form to true/false. Or you could call Show()/Hide().
I have a strange problem:
I have a Form that I open using ShowDialog(). The form is populated with some buttons and comboboxes. One of the comboboxes is set as the ActiveControl of the Form and the Form has focus.
What I want to accomplish is that the user can enter its username immediately after the Form opens (without the need to select the combobox first). However, if I press the keyboard, nothing happens. However, when I first click on the form with the mouse, and then enter something using the keyboard, it works. I already tried a lot of things like calling Select() and Focus() on the form. I even tried to simulate a mouseclick event (OnMouseClick) on the Form without any luck.
Someone has an idea would could be the problem here?
many thanks
Chris
Try BringToFront()
var f = new Form1();
f.Show();
f.BringToFront();
Then just use Select on that control
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Select();
}
Assuming you're running ShowDialog() from another form it might be worth changing it to: ShowDialog(this) so the new form has the correct parent and the correct blocking behaviour. Without the 'this' I've seen forms open up behind other forms and other strange behaviour, including focus problems.
Just a thought.
Have you tried calling myComboBox.Focus();
Just because the form has focus doesn't necessarily mean any control within the form has focus. Also try checking the onKey events of both the form and the individual controls. This normally helps me diagnose what exactly has focus. If the form itself and non on its controls are getting any onKey events then try using form.Activate();
I have two form classes (Form1 and Form2) in a Winforms App.
Form1 is like this:
Form2 is like this (ShowInTaskbar = false):
And this code on Form1:
Form2 someForm = new Form2();
private void btOpenAnotherWindow_Click(object sender, EventArgs e)
{
if (someForm.ShowDialog(this) == DialogResult.OK)
MessageBox.Show("OK!!!");
else
MessageBox.Show("Not OK.");
}
That is, a window with a button which opens modally another windows when clicked, and waits for the user to close the second window (by clicking the "OK" or "Cancel" button). And depending how it was closed, do alternating actions (represented here by the MessageBox.Show() calls).
I need:
The user can use only one window at a time. (Modal forms, That's why I used ShowDialog() instead of Show())
When the form closes, do something depending on how the form was closed (the "if (someForm.ShowDialog(this)...")
To be able (as a user) to minimize the WHOLE APP.
To be able to "unminimize" the app to the former state correctly.
The program to respond to WIN+M (minimize all) keys combination.
the above example fails in two ways:
(need 5) Doesn't respond to WIN+M
(need 3) The app seems to minimize when the Minimize title bar button is clicked, but it is an illusion because the main form (Form1) does not minimize and it is in fact just hidden behind the other opened windows. Only running the example with an empty desktop shows what really happens. Pics follow:
Before Minimize button is clicked:
After:
Note:
The Main form is not minimized
The Form2 is in the left botton corner of the screen.
Form2 is a full blown window (not a dialog window per se) and I need the user to interact with it only until it is closed and I also need the user to be able to miminize the whole app in case he needs it.
It is a shame I can't post here the real forms, It would be clearer than these mock-ups.
I need a solution that works with many levels of modal windows (not only two as this example shows). Any suggestions?
I may need a little more information about what you're trying to do here. I have a simple form (Form1) with a button on it, which calls this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 form2 = new Form1();
form2.ShowDialog();
}
When I click the button, I get a second instance of the same form, but it's modal. I still have the option to minimize this second modal form (I obviously can't interact with the first form), and when I do minimize the second form it does minimize the entire application (both forms). Now obviously you're asking the question, so I don't think I'm understanding you. =) What about this scenario do you wish to change?
C
There is probably some way to hack this functionality using API calls, but I would probably suggest doing some type of overlay with a control inside your main form rather than an actual window. This would allow you to make it "modal" and still have the ability to minimize/resize the main window.