C# windows app Minimize in system tray error - c#

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;
}
}

Related

C# App - Video stream does not start at 1st attempt

My C# application starts minimised and shows the video stream of a webcam for 5 seconds when a certain event occurs. After that it is minimised again. This works well, but only from the 2nd event. On the first call, only an empty form is displayed.
Is there an event in which I can add a videoSourcePlayer1.Show() so that something is also displayed in the 1 event? It does nothing in the Form1_Shown event.
My code:
...
private readonly UdpClient udp = new UdpClient(port);
public IntPtr myHandle;
public void wait(int milliseconds)
{
...
}
private void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ip), port);
byte[] bytes = udp.EndReceive(ar, ref ip);
string message = Encoding.ASCII.GetString(bytes);
if (message == "ON")
{
ShowWindow(myHandle, SW_RESTORE);
Thread.Sleep(50);
SetForegroundWindow(myHandle);
wait(5000);
ShowWindow(myHandle, SW_SHOWMINIMIZED);
}
StartListening();
}
private void Form1_Load(object sender, EventArgs e)
{
myHandle = this.Handle;
videoSourcePlayer1.VideoSource = new MJPEGStream("http://ip/video.mjpg");
videoSourcePlayer1.Start();
this.WindowState = FormWindowState.Minimized;
StartListening();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
videoSourcePlayer1.Hide();
}
if (this.WindowState == FormWindowState.Normal)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
videoSourcePlayer1.Show();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer1.Stop();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
What can I do?
Got it. I changed the execution order in Form1_Resize and added a delay.
if (this.WindowState == FormWindowState.Normal)
{
videoSourcePlayer1.Show();
Thread.Sleep(250);
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
Show();
}
Add this method to continue the stream
private void bntContinue_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Continue();
}

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!

Pop Up in System Tray not showing

I am trying to show a message on loading of the application. I am trying to implement this using the Windows notifyIcon tool. Below is the method I implemented and calling this in the form load. However no message is being shown when opening my application.
private void Displaynotify()
{
try
{
//notifyIcon1.Icon = new System.Drawing.Icon(Path.GetFullPath(#"image\graph.ico"));
notifyIcon1.Text = "Finance - Treasury Ticket Notification";
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = "Welcome, you have 3 Pending Tickets.";
notifyIcon1.BalloonTipText = "Click Here to see details";
notifyIcon1.ShowBalloonTip(100);
}
catch (Exception ex)
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
Displaynotify();
}

NotifyIcon and Form.Resize event in Windows application

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.

minimizing to notification area and double click is not opening it back up

I am trying to get my application to minimize to the notification area and that part is working. The problem is, when I double click it, it is not showing the window again.
This is what I'm doing, I hope it's something simple I'm doing wrong:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
CreateNotifyIcon();
}
private void CreateNotifyIcon()
{
mynotifyicon.BalloonTipIcon = ToolTipIcon.Info;
mynotifyicon.BalloonTipText = "[Balloon Text when Minimized]";
mynotifyicon.BalloonTipTitle = "[Balloon Title when Minimized]";
mynotifyicon.Icon = Resources.lightning;
mynotifyicon.Text = "[Message shown when hovering over tray icon]";
}
private void MainLoad(object sender, EventArgs e)
{
Resize += MainResize;
MouseDoubleClick += MainMouseDoubleClick;
}
private void MainResize(object sender, EventArgs e)
{
try
{
if (FormWindowState.Minimized == WindowState)
{
mynotifyicon.Visible = true;
mynotifyicon.ShowBalloonTip(3000);
ShowInTaskbar = false;
Hide();
}
else if (FormWindowState.Normal == WindowState)
{
mynotifyicon.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void MainMouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
Show();
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
mynotifyicon.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
Something I forgot to mention was I put a debug stop on MainMouseDoubleClick and it's never hitting that point.
Thanks for the help!
** EDIT **
I changed the double click to have a try/catch and it's not being reached at all. Not even to the try.
You need to add the click event to the notifyicon. At the moment you are registering the handler on the form.
mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;
Attach the MouseDoubleClick event at the end of CreateNotifyIcon:
mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;
I would recommend renaming the event handler MainMouseDoubleClick to something more appropriate and unregistering it from the form's double click event.
I have used this and its working :
this.WindowState = FormWindowState.Normal;
this.Activate();

Categories