Reputation: 6828
I am trying to make an application with hotkeys and an option to startup in the taskbar tray.
Now the problem is, that using this.Hide()
in the load event, won't have any effect. I can add this.ShowInTaskbar = false
, but after I set it on true again, to show the window, it disables my hotkey out of itself.
Is there an other way to hide my form on startup, or to prevent my hotkey from unregistering?
My code to hide the form:
private void frmMain_Load(object sender, EventArgs e)
{
if (StartBG())
{
this.Hide();
this.ShowInTaskbar = false;
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(3000, "Kore screenshots", "The application is started and will run on the background.", ToolTipIcon.Info);
}
}
After the code above, the hotkey still works,
private void showform()
{
this.Show();
this.ShowInTaskbar = true;
notifyIcon.Visible = false;
this.WindowState = FormWindowState.Normal;
}
After this code, the hotkey is disabled.
Upvotes: 4
Views: 3040
Reputation: 239664
What I've done in the past is to create a separate form, which is always minimized and ShowInTaskbar false (so it's never user visible), and this form owns the NotyifyIcon.
Then, any UI I want to display, I develop as separate forms which I show/hide as required. I find this works more cleanly than trying to get one form which might be shown/hidden/showing in taskabr/owning a notifyicon.
Upvotes: 5
Reputation: 941455
Preventing the form from getting visible but still creating its Handle so that the notify icon works requires overriding SetVisibleCore(). Like this:
protected override void SetVisibleCore(bool value) {
if (value && !this.IsHandleCreated) {
value = false;
CreateHandle();
// Put your Load event code here
//...
}
base.SetVisibleCore(value);
}
Beware that the Load event or OnLoad() method won't run until the form actually gets visible later. So move any code you got there into this override.
Upvotes: 4