This question already has answers here:
How can I display a system tray icon for C# window service.?
(2 answers)
Closed 7 years ago.
I have created a windows service in C#. I want this service to be moved to the system tray and have a popup menu with start/stop options and a settings form should also open from the menu.
Can anyone please guide me.
I also wanted to do this once, you can do it by adding a NotifyIcon to the service.
Then adding this to the script
notifyIcon1.MouseClick += notifyIcon1_MouseClick;
void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
//yourcodehere
}
Then add a ServiceController class to handle stopping and starting the service
you can also add the following to handle the workstation being locked
Microsoft.Win32.SystemEvents.SessionSwitch +=
new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) {
//yourcodehere
}
Related
This question already has answers here:
Change pinned taskbar icon (windows 7)
(9 answers)
C# - Changing the icon of the taskbar
(7 answers)
Closed 4 years ago.
I have a button which runs a job, let's replace the job with just a 'Sleep' for a few seconds. When clicking the button, the form's icon changes:
private void ButtonStartClick(object sender, EventArgs e)
{
this.Icon = Properties.Resources.RED_32x32_ICON;
System.Threading.Thread.Sleep(4000);
this.Icon = Properties.Resources.GRE_32x32_ICON;
}
This successfully changes the icon for the 'Form' that the button is on - but it's NOT changing the taskbar icon?!
I'm fairly sure I had something similar running in VB last year whereby changing the currently visible form icon would change the taskbar icon too. This was great for showing when my app was "busy"
I tried changing the default icon for my app and inheriting it, and also just applying my icon at form load...
private void MainForm_Load(object sender, EventArgs e)
{
// this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
// this.Icon = Properties.Resources.GRE_32x32_ICON;
}
There are a few related hits when googling this but mainly to do with just changing the icon via the app settings and not during run time?!
This question already has answers here:
How to set hotkeys for a Windows Forms form
(8 answers)
Closed 8 years ago.
Currently I want to add a hotkey to my button in C# windows application.
I went to the event, I didn't see anywhere I can assign hotkeys.
I wanna assign "F6' as the hotkey, which means whenever I press F6 it would trigger the button and run the code in it
You could try to handle the Forms Keypress-event (and route all components keypress-events to that)
Assigning hotkeys via the GUI works just for MenuItems, if I'm not mistaken.
Edit: some code:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F6)
button1_Click(this, new EventArgs());
}
Just what I quickly tried. If you register to the forms (or any other controls for that matter) KeyUp-event, you can query the released key and act accordingly. I suppose there are more elegant ways to do this, but this should get you started.
This question already has answers here:
Executing code when my form is closed
(3 answers)
Closed 8 years ago.
I want to create a method that when the x button that closes the application is pressed (it is in the upper right hand corner) I save all the text into a file. From my limited c# experience when I click on a button or text box a method is created, however when I click on the x it takes me to the method for when the windows form is loaded. When I did a Google search the results pointed me to if I wanted to close the application not make an event happen when the form is closed. How do I get create an event when the form is closed?
If you have the form selected in the Designer in Visual Studio, you can select the FormClosed event from the Properties window and it will automatically generate an event method for when the form is closed, which you can then add your relevant code to.
You can register to Form.FormClosing event:
Form form1 = new Form();
form1.FormClosing += (o, e) =>
{
// Do stuff you want
}
Add this code on FormLoad:
this.FormClosing += MainPage_FormClosing; // occurs before closing of the form
this.FormClosed += MainPage_FormClosed; // occurs after the closing of the form
private void MainPage_FormClosed(object sender, FormClosedEventArgs e)
{
// your code here to do something before closing the form
}
private void MainPage_FormClosing(object sender, FormClosingEventArgs e)
{
// your code here to do something after the form is closed
}
I think this is the answer to your question.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Global keyboard capture in C# application
Hi all,
I am making a .NET application (Window Forms) which is written by C# and I get a problem. How to hide my personal .NET application using a keyboard shortcut and then displaying it back from same keyboard shortcut.
Thanks !
First you should probably clarify which technology you're using: WinForms, WPF?
Anyway, using WinForms You can use the KeyDown event to process such occurrences:
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Shift && e.KeyCode == Keys.M)
{
WindowState = FormWindowState.Minimized;
}
}
Note that the modifier key/s and regular key/s you require to be pressed to carry out the action are obviously changeable.
A further note is that once this action has been executed then the window no longer has 'focus' and so repeating the keystrokes to display the window again will not work - for this to happen you will need to register a hot-key that Windows itself knows about or use a keyboard hook to intercept keystroke input to the system to consume in your application, AFAIK.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the proper way to minimize to tray a C# WinForms app?
How can I create a program that runs in the background, and can be accessed via the Windows' "Notification Area" (Where the date and time are in the lower right hand corner)?
In other words, I want to be able to create a program that runs and can toggle between having a display window and not having a display window.
Drag and drop a NotifyIcon and a ContextMenuStrip.
Set de NotifyIcon's context menu to the one you added
Add 2 menuitems (e.g. Restore, Exit)
Set the Form event resize and do the following check
private void MyForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) this.Hide();
else this.Show();
}
// you could also restore the window with a
// double click on the notify icon
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
For a example can download this project
Don't worry about the right click event, the NotifyIcon will automatically detect it and show the ContextMenu