Pop Up in System Tray not showing - c#

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

Related

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!

WebForm's WebBrowser consecutive navigation

VS 10, WinFormApp
I have a list of url's. I am trying to navigate to the URL's one by one. After completing the load of the document of a page, then the integrated webbrowser will navigate to the next URL.
Here is some code:
private void btnHit_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(lstUrls[counter].url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url != webBrowser1.Url)
{
lblStatus.Text = "Page is loading....";
lblStatus.ForeColor = Color.Red;
return;
}
else
{
funcMethod();
}
}
public void funcMethod()
{
lblStatus.Text = "Page is loaded";
lblStatus.ForeColor = Color.Green;
try
{
webBrowser1.Document.Focus();
webBrowser1.Navigate(lstUrls[++counter].url);
}
catch { }
}
The problem is, when I debug for each element line by line, it works for all(100+) items in the List of URL's. but when I just press the btnHit, then sometimes it loads 2/3/4/5/.. number of urls. and stop loading further. In that time, If I hit the btnHit again, the process starts and same thing happens as before.
What am I missing?

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();

Duplex printing with WebBrowser control only printing 1 side

I'm having an issue with my .NET application only printing the second page of my HTML file, and completely ignoring the first page (no other page is printed, and the back of it is blank).
When I pull up my printer's queue window, it does show it go from "Spooling" to "Printing" and lists both pages, so I'm at a loss as to why it's not printing the first page.
(My printer IS set to duplex printing, and if I literally just print the HTML document from my browser, it works as expected)
Here's what I'm doing:
private void Form1_Load(object sender, EventArgs e)
{
// Create a FileSystemWatcher to monitor all files on drive C.
FileSystemWatcher fsw = new FileSystemWatcher("C:\\COAForms");
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Register a handler that gets called when a
// file is created, changed, or deleted.
//fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Error += new ErrorEventHandler(fsw_Error);
//fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.EnableRaisingEvents = true;
fsw.SynchronizingObject = this;
PrinterSettings settings = new PrinterSettings();
label2.Text = settings.PrinterName;
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
}
void fsw_Error(object sender, ErrorEventArgs e)
{
MessageBox.Show(e.ToString());
}
private void OnChanged(object source, FileSystemEventArgs e)
{
notifyIcon1.BalloonTipText = "Printing document " + e.Name + "...";
notifyIcon1.BalloonTipTitle = "Printing Application";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(500);
PrintCOAPage(e.Name);
}
private void PrintCOAPage(string name)
{
try
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(#"C:\COAForms\" + name);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
PrinterSettings ps = new PrinterSettings();
ps.Duplex = Duplex.Vertical;
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.Activate();
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
Hide();
}
}
I only just recently added the PrinterSettings to the code and it changed nothing.
I would greatly appreciate any help you guys can provide on this! Thank you!
Wow, didn't think the CSS would impact it, but for whatever reason the CSS I was using (involving z-index) made the first page NOT display when I print previewed under the WebBrowser control, but worked just fine in actual IE8.. after changing the CSS around a bit it now works as intended.

Categories