How to make a form always top of taskbar - c#

I want create a form in C# that always be on top of taskbar and other programs.
I try with Topmost but when I click on Alt+Tab or start button on keyboard, taskbar is top of my form.

Ugly approach: use Timer.
private void timer1_Tick(object sender, EventArgs e)
{
this.TopMost = true;
}
Better not to do so.

Related

Perform mouse click on TextBox or window

I know that this is not a good practice, but I would like to simulate a mouse click event on a textbox, or a window in my wpf application.
I need this, because I am calling this application with some kind of interceptor and the window doesn't get the full focus, it is in front, textbox has a focus but u can't directlly type on textbox.
on window load I call these methods
this.Activate();
this.Topmost = true;
TextBox1.Focus();
Keyboard.Focus(TextBox1);
Thank you.
I had the same issue in VB. i just wanted the focus in the SpinEdit1 (or any control) when the form load. Here is:
private void formX_Load(object sender, EventArgs e)
{
this.ActiveControl = SpinEdit1;
}

How to create a full-screen form with max/min/close button in c#?

I made full-screen by: FormBorderStyle = FormBorderStyle.None;
but there is no max/min/close button? How to get it?
Set the WindowState property of your form to Maximized instead of removing the form border if you want a full screen form to retain the controls.
private void Form1_Shown(object sender, EventArgs e)
{
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
If you actually want a full screen form with no border the best way to do this would likely be a custom user control to emulate the standard min/max/close functions.

Minimizing a system windows form in tray in C# without seeing it hanging where taskbar

this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
//this.visible = false (Hotkeys stop working forever but the grey rest of the form would disappear from above taskbar)
ReRegisterHotkeys();
I use the code above to minimize my application with a tray icon. Now the minimized rest of my form hangs still in the left right corner a little above the taskbar where the start button resides. Visible is only the forms grey titlebar with the little "x" to close it and its caption text above the title bar.
This is very weird. I set my form to "mimimized" and set not to show in taskbar and it still does.
I have registered Hotkeys with my form so I may not set it to "invisible" else the hotkeys somehow cease to work even if I re-register the Hotkeys afterwards again.
I found no alternative yet to remove this minimized form caption other than set it to "invisible"
or removing its titlebar what I also don't want to do. I need titlebar, titlebar icon and titlebar control area in this program, the form shall not become a toolwindow or without borders.
How do I make this grey rest of the form above the taskbar disappearing without setting my form to a toolwindow and without setting it totally invisible. My Hotkeys must still work after it and the form must still keep its titlebar, icon and control area when I set it back to normal again.
I took my hotkey code from this example. The only difference is that I packed the procedure to register the hotkey into a sub-function named "ReRegisterHotkeys()".
Important:
The issue with the titlebar showing when form is minimized is not connected with the registered hotkeys.
It's a common "C# issue". If I have a form and minimize it and set it to invisible in taskbar
it is still shown the titlebar with the "x" in the taskbar. This I want to remove without making
the form invisible or without removing the window style.
"this.show" or "this.hide" behaves the same fatal as "this.visible=true/false" since the hotkeys are gone. I create my form as shown by default and want not to create it hidden already.
This is what shall be not there - how to remove it without hurting:
All you have to do is call Hide() and Show() when you want to hide and show your form.
NOTE: Hide() will hide from taskbar as well.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
Hide();
}
You may hide and show the NotifyIcon opposite to the form to not have a icon when the form is shown.
Obviously you need a NotifyIcon to display your app in the system tray.
Finally your code will look like this:
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}

Why when i close my application from the icon tray sometimes the icon is left on even after the application is closed?

I have this code :
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void CloseApplication_Click(object sender, EventArgs e)
{
this.Close();
}
Then i resize hide the form or not resizing it i'm going with the mouse to the tray icon right click then select close application the Form is closed the application is closed.
But sometimes the icon of the application is stay in the tray icon and only if i move my mouse cursour over the icon he is gone .
How can i make sure that when i close my application the icon in the tray icon will move/gone away ?
You can either set
notifyIcon1.Visible = false;
or
notifyIcon.Icon = null;
in the form closing event.
(from NotifyIcon remains in Tray even after application closing but disappears on Mouse Hover )
I've had the same problem. For me, setting
notifyIcon.Visible = false;
works fine and notify icon immediately dissapears from system tray.

Transition between two forms using show and hide does not behave as I expect

I have two forms. The first is the parent and contains a reference to the second and a button:
public class Form1:Form {
private Form2 frm2;
private Button btnShow;
...
}
When I press the button frm2 must be shown (visible and in front of all other windows) and Form1 must hide.
When I press the closebox of frm2, frm2 must hide and Form1 must be shown (reverse).
I used the click event of btnShow to register a handle that all it does is:
private void click(object sender, EventArgs e)
{
Hide();
frm2.Show(this);
}
and in the FormClosing event of frm2:
private void byebye(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
Owner.Show();
}
}
My codes works for 90% of the time but sometimes (i cannot specify when) the shown form
is not brought to front or loses focus (I am not sure about that i think the first).
What I am doing wrong. I've tried alternating the order of Show and Hide and using of a new reference instead of Owner, I also used Activate,BringToFront, Focus but with no success...
Why not simply:
{
Form2 fm = new Form2();
this.Visible = false;
fm.ShowDialog();
this.Visible = true;
}
Works for me.
Hide();
Owner.Show();
You are doing this the wrong way around. For a split second, there's no single window left in your application that can receive the focus. The Windows window manager is forced to find another window to give the focus to. Which will be the window of another application. With good odds that this is a large enough window to cover your own. Your Show() call will thus make your window visible again, but now underneath that window that got moved into the foreground. This doesn't always happen btw, the window manager appears to use a small timeout. The longer the owner has been hidden, the greater the odds that its code has been swapped out and that showing it takes more time, thus tripping the timeout.
The workaround is simple, just swap the two statements so you'll always have window that can be focused. Fix:
Owner.Show();
Hide();

Categories

Resources