How to display current window to notification bar NOT HIDE in WPF - c#

I've created one WPF application. It has one window and it's hide on close button. But i want to show it in notification-bar. and when user click on that then windows should display.
Here is my code:
public MainWindow()
{
InitializeComponent();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
aTimer.Interval = 3000;
aTimer.Enabled = true;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lblProcess.Content = "Test Window"
}
// minimize to system tray when applicaiton is closed
protected override void OnClosing(CancelEventArgs e)
{
// setting cancel to true will cancel the close request
// so the application is not closed
e.Cancel = true;
this.Hide();
//this.Show();
base.OnClosing(e);
}
I've already read this: create-popup-toaster-notifications-in-windows-with-net
And minimizing-application-to-system-tray-using-wpf-not-using-notifyicon
minimizing-closing-application-to-system-tray-using-wpf
But didn't get you how can i do this?
Any help appreciated!

I have done using this code:
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("D:\\images.ico");
ni.Visible = true;
ni.DoubleClick +=delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};

Related

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

Timer doesn't stop after closing form C#

i create timer in my form, but after i close form, timer still working.
I tried different ways to stop this timer, but did not have any success.
what is problem?
private Timer timer100;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer100 = new System.Windows.Forms.Timer(this.components);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
timer100.Stop();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
timer100.Dispose();
timer100 = null;
}
I did not put InitializeComponent() in MainForm's constructor as jhmt mentioned in comments.

Bring dialog window to front

I am showing a window on a button click like this:
private void showWindow(object obj)
{
var dialog = new AddItemView();
dialog.Show();
}
If the button is clicked again, while this window is still open, how do I bring this window to the front and not create a new one?
Just store the dialog object and check whether it's already been created in showWindow.
Used the windows Closed event to clear the reference to the dialog object.
AddItemView dialog;
private void showWindow(object obj)
{
if ( dialog == null )
{
dialog = new AddItemView();
dialog.Show();
dialog.Owner = this;
dialog.Closed += new EventHandler(AddItemView_Closed);
}
else
dialog.Activate();
}
void AddItemView_Closed(object sender, EventArgs e)
{
dialog = null;
}
Just a quick sketch but this should do what you want:
Window1 W = new Window1();
private void Button_Click(object sender, RoutedEventArgs e)
{
if (W.IsVisible)
W.Activate();
else
W.Show();
}
If this does not do it, maybe I have misread your question.
Edited to correct a bug.
Add this on the class constructor where you are instantiating the window. A window cannot be closed after is opened.
W.Closing += (s, e) =>
{
e.Cancel = true;
((Window)s).Hide();
};

Why is a disabled button clickable?

This case is using C# WPF. I want to instantly disable a button after clicking it to prevent clicking it twice in short succession. I disabled the button in OnClick_Event but still clickable.
Part of source is as below.
private void Button_Click_UpdateBurndownChart(object sender, RoutedEventArgs e)
{
if(threadNotWorking)
{
updateButton.IsEnabled = false;
startWorkThread();
}
}
private void startWorkThread()
{
... ...
//after finish required process
updateButton.IsEnabled = true;
}
Is there any way to accomplish this?
you may want to use a dispatcher, there is probably a threading problem (callback function running on seperate thread and trying to access ui which runs on another thread). try this . .
updateButton.Dispatcher.BeginInvoke(
new ThreadStart(() => updateButton.IsEnabled = false),
System.Windows.Threading.DispatcherPriority.Input, null);
instead of
updateButton.IsEnabled = false;
What happens if you were instead to change the order of your events from:
updateButton.IsEnabled = false;
startWorkThread();
To
startWorkThread();
updateButton.IsEnabled = false;
Let me know how this goes.
What it looks like is that you are starting your thread then immediatly enabling your button before your thread has finished. You would be better off using a BackgroundWorker and enable your Button in the RunWorkerCompleted Event. Though you can do something similar by enabling your button using a BeginInvoke at the end of your Process.
public void doWork()
{
System.Threading.Thread.Sleep(10000); //Simulating your Process
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate() { updateButton.IsEnabled = true; }), System.Windows.Threading.DispatcherPriority.Background);
}
Example with BackgroundWorker
using System.ComponentModel;
public partial class MainWindow : Window
{
BackgroundWorker bgw;
public MainWindow()
{
InitializeComponent();
bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
updateButton.IsEnabled = true;
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(10000); //Simulating your work
}
private void startWorkThread()
{
bgw.RunWorkerAsync();
}
private void updateButton_Click(object sender, RoutedEventArgs e)
{
if (bgw.IsBusy != true)
{
updateButton.IsEnabled = false;
startWorkThread();
}
}
}

Categories