I have a form that is launched modally like this:
private void find_street_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
Form findForm = new FindStreet();
findForm.ShowDialog();
this.WindowState = FormWindowState.Normal;
}
The form launches correctly, and the cursor is in the first text box, whose TabIndex is set to 1.
Along with the InitializeComponent(); call, these commands are present.
public FindStreet()
{
InitializeComponent();
this.TopMost = true;
this.BringToFront();
this.Focus();
}
I have looked at and tried a number of examples. The cursor appears in the correct control, but the form's window does not have the focus. The problem is that if a user starts typing, even though the newly launched form is visible, those keystrokes are not going into the text box.
Remove the code in public FindStreet() and in load event of FindStreet add:
this.TopMost = true; //i don't know why you need this.
this.Activate();
When you minimize your main form the next one in z-order get the cursor. this.Focus() doesn't do anything. You need to Activate the dialog.
A dialog requires an owner, that cannot be a minimized window. Now accidents start to happen, starting with your WindowState assignment. Your app doesn't have a window left that can receive the focus so Windows is forced to find another one, that will be one owned by another application. Same problem happens when you close the dialog.
You can still get the intended effect, you must hide your main window after the dialog is displayed, show it again before the dialog closes. That requires a bit of hackorama:
using (var dlg = FindStreet()) {
// Show main window when dialog is closing
dlg.FormClosing += new FormClosingEventHandler((s, cea) => {
if (!cea.Cancel) this.Show();
});
// Hide main window after dialog is shown
this.BeginInvoke(new Action(() => {
this.Hide();
}));
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
// etc...
}
}
And remove the hacks from the FindStreet constructor. Watch out for event order if you have a FormClosing event handler in FindStreet, be sure to override OnFormClosing() instead.
If you want to set a specific control as the current active control then try this:
this.ActiveControl = myTextBox;
This will place the cursor you want as the main focus, when the form loads. So try this out:
public FindStreet()
{
InitializeComponent();
this.TopMost = true;
this.BringToFront();
this.Focus();
this.ActiveControl = myTextBox;
}
Here is the link to Focus() which should explain why your focus call was not working.
Related
I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:
Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...
WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:
this.Visible = false;
this.ShowInTaskbar = false;
When the NotifyIcon is double-clicked I have the following:
this.WindowState = FormWindowState.Normal;
this.Visible = true;
this.ShowInTaskbar = true;
Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!
The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) {
allowVisible = true;
Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
allowClose = true;
Application.Exit();
}
}
Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.
I'm reading all the answers and see hacks and black magic... (no offense, mates)
No hacks needed. You don't even have to set "ShowInTaskbar=false" and other stuff. Just do this:
//"Form Shown" event handler
private void Form_Shown(object sender, EventArgs e)
{
//to minimize window
this.WindowState = FormWindowState.Minimized;
//to hide from taskbar
this.Hide();
}
NOTE: I strongly recommend NOT TOUCHING the "ShowInTaskbar" property. For example, if your application registers system-wide hotkeys or other similar stuff (hooks, etc) - setting ShowInTaskBar=false and minimizing your app will prevent Windows from sending some messages to your window... And your hooks/hotkeys/etc will stop working.
In the constructor, remove these two lines:
this.Visible = false;
this.ShowInTaskbar = false;
and add after InitializeComponent();:
this.WindowState = FormWindowState.Minimized;
In designer, set ShowInTaskbar to false & FormWindowState to Normal.
EDIT:
If you post the same in Load event, the window does get minimized but still shows minimized on the desktop. I think this is a bug.
When minimizing an application and you want to hide it from Alt+Tab:
You also need to set the Opacity to stop the titlebar showing near the Start Menu when you set the Border Style to a Tool Window.
On Minimize Event:
this.Visible = false;
this.Opacity = 0;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ShowInTaskbar = false;
On Normalize Event:
this.Visible = true;
this.Opacity = 100;
this.FormBorderStyle = FormBorderStyle.FixedSingle; //or whatever it was previously set to
this.ShowInTaskbar = true;
Move the following code from the Form's constructor to Form_Main_Load(). With the same setup on Notification_Icon when Form_Resize().
// Hide the Form to System Tray
this.WindowState = FormWindowState.Minimized;
This "quick and dirty fix" worked for me:
$form1.FormBorderStyle = "fixedtoolwindow"
$form1.top = -1000000
$form1.Left = -1000000
$form1.Width = 10
$form1.Height = 10
$form1.WindowState = "normal"
$form1.ShowInTaskbar = $False
$form1.Opacity = 0
$form1.Hide()
Hope it helps someone else...
I have a Windows Form in a C# program with the FormBorderStyle set to FixedToolWindow.
The window doesn't have the normal control box, but the user can still minimize it from a menu. There is also a setting for the window to start in a minimized state.
When the user clicks the minimize button it called the following function:
private void minimizeWindow()
{
timer1.Enabled = false;
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
The window becomes a small box in the bottom left of the screen, then disappears (from the this.hide call).
When the same function is called from within the Form_Load method (when the setting is to start minimized) it minimizes but does not disappear.
My guess is that because I am hiding the form before reaching the end of Form_Load it is being shown again when it reaches the end of the method. Is there any way to ensure the form hides when it is loaded (it is maximised again from a system tray icon)?
Edit: Included all code from form load
private void Form1_Load(object sender, EventArgs e)
{
this.Left = windowXPos;
this.Top = windowYPos;
sysTrayIcon.MouseDoubleClick += new MouseEventHandler(sysTrayIcon_MouseDoubleClick);
sysTrayIcon.BalloonTipText = "Timers Running";
this.sysTrayIcon.Icon = this.Icon;
sysTrayIcon.Visible = true;
sysTrayIcon.ShowBalloonTip(500);
Start(); //sets up timers
if (startMinimized)
{
minimizeWindow();
}
}
It might be best to change your logic slightly, and instead of showing the form at all, create an instance of it (var foo = new MyForm()) but don't call .Show() so it will never be shown until triggered from your system tray icon.
I am developing multiple instance WPF application. Application has a grid in a main screen and on double click of grid row, it opens child window. It has also functionality to open multiple child windows on double click of grid row from main screen.
Can anybody help me to prevent parent window to be close if child windows are active? So user can not able to close main window if child windows are active.
Set Owner property for those childs to Main Windows:
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window();
wnd.Owner = this;
wnd.Show();
}
Then in Main Window closing event handler:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.OwnedWindows.Count > 0)
{
MessageBox.Show("Child windows exists, you have to close'em first");
e.Cancel = true;
}
}
As a final point it could be helpful for you to know that you can get from anywhere in your code the app main windows with this:
System.Windows.Application.Current.MainWindow
So if you are using MVVM the above will help you in setting the Owner property.
You have two options:
1- You can use ShowDialog() to open the child window, but user can't interact with the parent window till the child is closed.
2- You can check all windows that are currently opened by checking
Application.Current.Windows
and then you can determine whether you want to close your window or not
Edit:
add the following event handler to your Parent.Closing event
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
foreach (var item in Application.Current.Windows)
{
Window window = item as Window;
if (window.Title == "YourChildWindowTitle")
{
// show some message for user to close childWindows
e.Cancel = true;
break;
}
}
}
In window closing command pass that if the child window is open disable closing functionality.
Or
you what you can do is make canexecute = false when a pop up is open and closing command is triggered.
Attach a function to the main window's 'Closing' event, and check to see if the child window is open. if it is, set
e.cancel = true;
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();
This form has a NotifyIcon object. When the user clicks on the Close button, I want the form not to close but to become invisible. And then, if the user wants to see the form again, he can double click on the icon in the systray. If the user wants to close the form, he can right click on the icon and select "close".
Can someone tell me how to make the close button not close the form but make it invisible?
(or if someone can think of a better way to achieve the same objective by all means)
First, you want to handle your Main form's .FormClosing event (or by overriding the OnFormClosing method). Cancel that by setting e.Cancel to true.
Then, you use a NotifyIcon to add an icon to the system tray.
Finally, hide the form by calling .Hide().
protected override void OnFormClosing(FormClosingEventArgs e) {
if (IActuallyWantToCloseFlag)
return;
var ni = new NotifyIcon(this.components)
{
Icon = someIcon,
Text = "My text",
Visible = true
};
ni.DoubleClick += (sender, args) => { this.Show(); };
this.Hide();
e.Cancel = true;
}
This should get you started. You probably want to make ni a member variable, so that you can continue to hide/show the icon as you show/hide your form.
You could try making it a tray icon app. There's a good tutorial here for that: http://www.developer.com/net/net/article.php/3336751/C-Tip-Placing-Your-C-Application-in-the-System-Tray.htm
The simplest way to achieve what you want would be to subscribe to the FormClosing event and set visible false, by including this sort of code:
FormClosing += (sender, args) => { args.Cancel = true; Visible = false; };