Form 2 not showing up using system tray - c#

I got 2 forms for a application and I am using a system tray to get it as a popup. But when I enter my username at my first form and open my second form and click at the popup icon in the bottom it doesn't work for the second form anymore.
I also have add this code at my second form.
here is the code;
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}

Maybe because you have the name Form1 and Form2's resize event is not pointing to the correct function.
In design time, open the Form, press F4 to bring up the Properties window, click the Lightning Bolt icon to see the Forms events, then scroll down to Resize and choose Form2_Resize:
private void Form2_Resize(object sender, EventArgs e)

Related

I want a WinForms application to minimize into the system tray on the click of a Toolstrip Menu Item

I have a simple WinForms program with a toolstrip menu with several buttons (e.g. closing, new profile, etc...), but I want it to minimize on a click of the "move to tray" menu item. I'm using C# on VS 2019 with .NET 3.1, this is my code so far for moving it to the system tray and bringing it back up again from the tray on a double click:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon.Visible = true;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
Note that the application only moves to the system tray when the user is manually clicking on the minus-sign or respectively when the resize-event is triggered, but I want it to close on the click on a menu item of a toolstrip menu.

Where do I put custom code to handle a resize event C# windows form

I cannot work out where to put nor get the code to trigger when my main form windows are resized (ie minimize button clicked)
I am trying to trigger this code when ANY resize of the DigiDocketMain window is minimized etc, or also how I can specifically code the minimize button to do something - the ideal goal is to get the program - n minimize button click to hide the taskbar icon and show a tray icon.
I have tried placing this is the main code body and the designer code but nothing triggers it. any help would be appreciated.
private void DigiDocketMain_Resize(object sender, System.EventArgs e)
{
MessageBox.Show("You are in the Form.ResizeEnd event.");
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
mainTrayIcon.Visible = true;
}
}
In your code behind add the following to the Form_Load Event
this.SizeChanged += Form1_SizeChanged;
Then implement the function, autocomplete may do this for you.
private void Form1_SizeChanged(object sender, EventArgs e)
{
// Add the code that will be called on resize events.
}
According to your description, when clicking the minimize button, you want to hide the
taskbar icon and display the tray icon.
I suggest that you set the Visible of notifyIcon1 to false in the property bar, and select a icon format image as the icon, then try the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}

Minimize to tray [duplicate]

This question already has answers here:
How do I minimize a WinForms application to the notification area?
(4 answers)
Closed 9 years ago.
my app is for chatting, and i think if someone needs to hide it quick, but doesn't want to close it, i came up with this:
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
however, instead of going to the taskbar, i want it to appear (no popup) in the tray, just the apps icon, and when someone clicks it it needs to set this
this.WindowState = FormWindowState.Normal;
Is this possible, how?
Also by system tray i mean the one in the bottom right corner, next to the time
I still can't get this to work, nothing appears in the notification bar if I do what you guys said (btw: this is the full code to minimise)
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
Why isn't this working?
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. This can be done by doing the
following in your form’s Resize event handler:
Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information.
Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false.
Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s MouseDoubleClick event. Here, you show the form using the Show() method.
In the form resize event, do the check there and hide the form
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Then when clicking on the taskbar icon just restore it.
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
Refer:
How do I minimize a WinForms application to the notification area?
minimize app to system tray
Use following code:
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
When you minimize the form, simply hide it.
You will have to implement above code in Form_Resize event.
Then on clicking taskbar icon just restore its state as follows:
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
You will need to use notifyIcon_Click event for this purpose.
Hope its helpful.

Right click when form is minimized to tray

I overridden the FormClosing event to minimize to system tray when clicked. Here is my code:
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
notifyIcon.BalloonTipText = "Server minimized.";
notifyIcon.ShowBalloonTip(3000);
}
else
{
this.Close();
}
}
And I also set the notifyIcon's DoubleClick event as well, here is the code:
private void showWindow(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
I have two questions regarding this:
1) Now, when the upper-right "X" button is clicked, the application is minimized to the tray, but I can't close it (makes sense...). I wish to click right click on the icon in the system tray, and that it will open a menu with, let's say, these options: Restore, Maximize and Exit.
2) (This is may be related to me exiting the program with shift+f5 since I can't, for now, close my application because of the changes I mentioned).
When the application quits, after I minimzed it to the tray, the icon is left in the tray, until I pass over it with my mouse.
How can I fix it?
Just add a variable that indicates that the close was requested by the context menu. Say:
private bool CloseRequested;
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
CloseRequested = true;
this.Close();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.UserClosing && !CloseRequested) {
e.Cancel = true;
this.Hide();
}
}
Be sure to not call Close() in the FormClosing event handler, that can cause trouble when the Application class iterates the OpenForms collection. The possible reason that you are left with the ghost icon. No need to help.

Show window after notify icon click

I'm trying to show my form when the user clicks on my notifyicon after the form has been minimized to the system tray. However upon calling Show() the form reappears but is always minimized, i have to click the taskbar icon to un-minimize. Here is my code.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
Show();
//BringToFront();
}
I've also tried adding BringToFront() but to no avail.
I need noifyIcon click to show the form, but for it not to be minimized afterward.
Set the WindowState property to Normal.
Set your window state back to not minimized. For example :
this.WindowState = FormWindowState.Maximized;
Try adding
this.WindowState = FormWindowState.Normal;

Categories