Separating NotifyIcon_Click and ItemMenu_Click events - c#

I have an application which runs mostly within the taskbar, and its icon may be left clicked in order to open a Windows form.
The NotifyIcon also has a few MenuItems which can be accessed by right clicking, and each have their own click events to serve their functionality.
private void menuItem4_Click(object Sender, EventArgs e)
{
if (programEnabled == false)
{
programEnabled = true;
Console.WriteLine("Enabled!");
}
else
{
programEnabled = false;
Console.WriteLine("Disabled!");
}
}
//Icon clicked!
private void notifyIcon1_MouseClick(object Sender, EventArgs e)
{
//Reopen the form
Form f = new TemperForm();
}
Every time a MenuItem click event is activated however, NotifyIcon_Click also runs and I need that event for opening the Windows form. I don't want every time I click any MenuItem to also run the code that I wish to run when the icon is clicked.
How can I still use the NotifyIcon_Click event while not having it run every time I click a MenuItem?
I've been at this for hours and it's driving me up the wall!

Related

How to setup a universal mouse click event WPF C#?

Is it possible to have a universal / global event fire on each mouse click, even if the actual program is minimized, or the click isn't on a window that is part of the application?
Here is code I have tried so far :
// Initialize Global Pre-Process Input
InputManager.Current.PreProcessInput += Current_PreProcessInput;
private void Current_PreProcessInput(object sender, PreProcessInputEventArgs e)
{
if (e.StagingItem.Input is MouseButtonEventArgs)
{
GlobalClickEventHandler(sender, (MouseButtonEventArgs)e.StagingItem.Input);
}
}
private void GlobalClickEventHandler(object sender, MouseButtonEventArgs input)
{
if (input.LeftButton == MouseButtonState.Released)
{
}
}
Problem here is; this only works when a click is in a window part of the application.
Is there some alternative way to have an event fire even if the application is minimized or the click is outside any application window?
Any help or advice will be appreciated.

How to launch method when window got focus again in WPF?

My app is multi-window, here is quickly how it works:
In main window I have a list of items, when I click on one, it opens another window where I can modify it. When I close that window, I want main window to refresh its content. I've tried many event handlers including GotFocus() but it doesn't want to launch my method to refresh the list. Any advise?
If you want something to happen when the other window is closed, you can subscribe to its closed event. This will fire when the windows is closed.
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window1();
wnd.Closed += wnd_Closed;
wnd.Show();
}
void wnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
}

wpf application c# How to minimize/bring to front sub forms

My WPF application is a media player that has a button that launches another window to a certain location and is my webbrowser. Problem is, once i click my button to launch the browser, my 'hide' button does not hide the browser window :/, from what i researched i need to inherit button click events, take a look at the code:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
//opens a web browser if 'Web' is clicked on the WPF Media PLayer
Window1 f1 = new Window1();
f1.Show();
}
the ideas i thought would work is create a 'toggle button' like such:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if ((bool)tb.IsChecked)
{
f1.Show();
//or
f1.WindowState = WindowState.Normal;
}
else
{
f1.Hide();
f1.WindowState = WindowState.Minimized;
}
}
this approach failed, and yes i tried variations and moving things around with NO success, i was told that i need to make a class of f1 to inherit button click events but i have no idea how to do that :/ any ideas?
here is how the app looks like:

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

Changing theme calls UserControl_Loaded event

I am not getting why behavior of WPF user control and Windows forms user control is different . I added window loaded event which just shows message box as :
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Main Window Loaded","WPF");
}
Also I created one user control and added loaded event as :
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("User Control Loaded.","WPF");
}
I have put this user control in main window.
When i launch this , I get both message box, User controls as well as windows.
Now , When i change my theme from Aero to any High contrast then user control's message box is shown again.
Why this is happening ? Why this is different from Windows form ? What should I do to avoid showing it multiple times ?
Wajeed
You could have a boolean field which stores the state of whether the dialog was shown yet or not. If you change the theme the UI-elements will reload so naturally the event will fire again.
if (!_diagWasShown)
{
_diagWasShown = true;
//Show dialogue
}
you can create bool variable, which will indicate if MessageBox was shown.
bool isUserMessageBoxShown = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (!isUserMessageBoxShown)
{
MessageBox.Show("User Control Loaded.","WPF");
isUserMessageBoxShown = true;
}
}

Categories

Resources