NotifyIcon and Form.Resize event in Windows application - c#

I am trying to minimize my winapp to system tray. I have downloaded a sample project from codeproject. But it goes to systary on Form.Resize event. Code -
private void Form_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
Is it necessary to handle it on resize event? Can i do it on button click event?

You can do this in your button. For obvious reasons you cannot rely on the WindowState in your button, because it can only be clicked when the window is no minimized to tray anyway.
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
This should work to "minimize" to tray. Although it should really be called hide-on-button-click-to-tray.

Related

Close nested form

So i have that aplication:
Menu Picture
and when i press the button "Terminar sessão" it's supposed to log out (and its works)
but when i went back to login the menu does not close!
Login Picture after log out
I am using nested form, the form ends session is closing but not the menu.
This is my menu.cs:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelChildForm.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
openChildForm(new Transferências());
Slidepanel.Height = (button2.Height - 15);
Slidepanel.Top = (button2.Top + 10);
}
and on "Transferências" form i have it:
private void Sessao_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Tem a certeza que deseja terminar sessão?", "Terminar Sessão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Hide();
Login login = new Login();
login.ShowDialog();
}
}
i already tried Menu.Close(); but it does not work
With the amount of information you provide, it is quite hard to tell what the problem might be. Did you try to debug it to verify that your program actually tries to execute Menu.Close()?
If you show your code people might be able to help you more.

C# windows app Minimize in system tray error

Hi Friends i have written the program for minimize the form into system tray it is working fine for the minimize and in tray i can see new icon is appeared.
and in double click it restores, But the error is when i restore the form into its normal state then some labels are automatically goes visible false and the timer i have used in form that timer automatically stops please let me know if anyone have faced the same issue and have any idea about this issue.
please refer below code
private void User_Projects_SizeChanged(object sender, EventArgs e)
{
try
{
bool Mousepointontaskbar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if (this.WindowState == FormWindowState.Minimized && Mousepointontaskbar)
{
notifyIcon1.Icon = SystemIcons.Application;
notifyIcon1.BalloonTipText = "Tracker Has bin Minimized in System Tray";
notifyIcon1.ShowBalloonTip(1000);
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
//this.SizeChanged += new EventHandler(User_Projects_SizeChanged);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void User_Projects_Resize(object sender, EventArgs e)
{
this.Resize += new EventHandler(User_Projects_SizeChanged);
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
if (this.WindowState == FormWindowState.Normal)
{
this.ShowInTaskbar = true;
this.Visible = true;
notifyIcon1.Visible = false;
}
}

Notify Icon duplicates itself, when any event occurs

I have a notify icon in my Winforms form and it seems that when any kind of event happens the tray icon is duplicated.
I have debugged one of the issues, being that it is duplicated when the dialog box is closed after using it.
It happens in debug and when released.
The other issue it with a timer that runs method.
I cannot see why this happens. My timer ran 60 times last night and each time it has four methods to run and there were hundreds of icons in the tray.
My code is as follows:
public Form1()
{
InitializeComponent();
notifyIcon1.BalloonTipText = "Mappi CSV Manager is running.";
notifyIcon1.BalloonTipTitle = "Mappi CSV Manager";
notifyIcon1.Text = "Mappi CSV Manager";
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowIcon = false;
ShowInTaskbar = false;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Call Dispose to remove the icon out of notification area of Taskbar.
notifyIcon1.Dispose();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (CloseCancel() == false)
{
e.Cancel = true;
};
}
//When closing the form
public static bool CloseCancel()
{
const string message = "If you close the program, no files will be generated!";
const string caption = "Stop!";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
return true;
else
return false;
}
//Set new value for timer
private void UdTimerValue_ValueChanged(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(udTimerValue.Value) * 60000;
}
//Start generating CSV's
private void Timer1_Tick(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (AutoGenerateEnabled)
{
richLogWindow.AppendText("CSV Created at: " + DateTime.Now + "\r\n");
var startdate = "";
if(DateTime.Now.Hour == 1)
{
richLogWindow.Clear();
startdate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
else
{
startdate = DateTime.Today.ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
}
else
{
return;
}
}
}
Why is this code producing another tray icon every time a button is clicked or an event happens.
TIA
I found the error. I have put RichTextBoxAppend.AddNewText("test me", new Form1()); the new form was created each time a process was run. I am an idiot!

c# winforms, focus on form until form.Close()

I have winform app that have main form and logging form. When logging form is shown I want it to have focus until it will be closed. I tried:
loggingForm = new LoggingForm();
loggingForm.FormClosing += loggingForm_FormClosing;
loggingForm.bOK.Click += bOK_Click;
loggingForm.Show();
loggingForm.Activate();
loggingForm.Focus();
loggingForm.TopMost = true;
loggingForm.TopMost = false;
void loggingForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (isValidPass)
e.Cancel = false;
else
e.Cancel = true;
}
Try this:
loggingForm.ShowDialog();

WPF dialogBox Don't close the application

I made a custom dialog box in WPF. I want , when the user clicks X the dialogBox will be opened, if he wants to exit he should click "yes", if not he should click "no".
static public bool? close;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Hide();
WindowClose windowClose = new WindowClose();
windowClose.ShowDialog();
close = windowClose.DialogResult;
if (close == true)
{
Application.Current.Shutdown();
}
else
{
//DONT CLOSE THE APPLICATION AND SHOW THE CURRENT WINDOW
}
}
the code of the dialog box:
private void button1_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
Not sure if it works differently in WPF, but in standard winforms in the code for the dialog box you'd set (for example) the DialogResult to DialogResult.OK (yes) and DialogResult.Cancel (no). You wouldn't need to close the form as this is done automatically when you set the DialogResult
Then in the main form you'd have
If (windowClose.ShowDialog() == DialogResult.OK
{
Application.Current.Shutdown();
}
else
{
//DONT CLOSE THE APPLICATION AND SHOW THE CURRENT WINDOW
}

Categories