C# How to avoid multiple tray icons when opening multiple windows - c#

I'm working on a C# Windows Forms application with multiple forms and a tray icon.
When my application starts, it adds a tray icon to systemtray, if i show a subform using .ShowDialog I get a new Instance of my application with a second tray Icon.
Here is my code to show the subform:
btnEditor_Click(object sender, EventArgs e)
{
CSV_Editor editor = new CSV_Editor(listEntrys,conf);
editor.ShowDialog();
}
Both forms are winforms and only the mainform has a trayicon.
How to avoid getting two Icons?
Thanks for your help!

Set ShowInTaskbar in your 2nd form to false.

Related

Duplicate icons in system tray when the single instance app is reopened again without fully closed

Recently I found an issue in my single instance c# wpf desktop application.
Initially, I opened my application and the icon was shown in the system tray. If I close the app using the close icon on windows, it will be run in the background and it can be opened from where it is left off using the system tray icon.
If I tried to open the app again like a regular way instead of using the system tray, there exists a duplicate icon in the system tray. However, hovering on the duplicate icons makes them disappear.
Is there any way to halt this issue of creating duplicates?
As Raymond Chen stated, you are not properly deleting your notification icon in your code. When your app closes, you need to hide and dispose the NotifyIcon properly that you are using.
If you don't properly hide and dispose the icon, then the icon will remain in the system tray even though the process has terminated. If you hover the mouse over the icon, it will then disappear. To prevent this "phantom" tray icon, you need to clean it up.
For example:
MainWindow.xaml.cs:
using System.Windows.Forms;
public partial class MainWindow : Window
{
private NotifyIcon taskbarIcon;
public MainWindow()
{
InitializeComponent();
this.Closing += MainWindow_Closing;
}
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (taskbarIcon != null)
{
taskbarIcon.Visible = false;
taskbarIcon.Dispose();
taskbarIcon = null;
}
}
}

Closed form still showing in taskbar

Creating a multi-form program linked to a database with login features in c#.
Tried moving between forms using show(), showdialog() and close(), dispose(), hide().
Once past the login form, the program will not properly close the forms.
It closes the form so the user can no longer access its controls, however, the 'closed' form remains completely visible in the windows taskbar and tab menus.
The user can even hover over the taskbar icon for said 'closed' form and see all the information there!
As the program will be handling sensitive personal information. I need help to stop this problem from happening.
Code from a back button on secondary form aimed to completely close the active form and open the main form.
"Curform" is defined outside the method as it is used in multiple buttons within the program.
Form CurForm = Form.ActiveForm;
public void Btn_Back_Click()
{
var MainForm = new MainForm();
MainForm.Show();
CurForm.Close();
}
Use directly active form instead of curForm
Form.ActiveForm.Close();
If you want to use curForm in other parts of the code update curForm when you have created the new form, like this:
Form CurForm;
public void Btn_Back_Click()
{
CurForm= new MainForm();
CurForm.Show();
// Do some things
CurForm.Close();
}

c# create system tray application that remains in tray after application closed

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
}
//Its not working to show my application icon in tray on application Exit.
When you exit the application, the tray icon goes away.
Also, when you close the last window, the application exits.
You want to minimize and hide the main window instead of exiting. You can do this with Hide(); somewhere in your Windows Forms code. Then, when the user wants to show the form, just run form.Show().

Block all windows with MessageBox.Show()

I'm currently working on a WPF app which has multiple windows. From the "main" window, you should be able to close the entire app. Before the app will be closed, the client wants it to show a dialog box which basically asks "are you sure you want to close the app" and blocks every other window until the user answers.
I'm currently using MessageBox.Show() to create this dialog box, but for some reason it only blocks the main window.
Here's the simplest example of what I'm talking about; if you create a WPF window with two buttons:
private void openChildWindowButton_Click(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.Show();
}
private void openDialogButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "This should freeze all other windows");
}
Opening a dialog will completely freeze the first window. If you click on it or attempt any sort of interaction, the OS makes a "ding!" sound and flashes the border on the message box. But all of the other windows you've opened can be clicked, moved, resized, etc., and that's what I want to prevent.
As it turns out, there is a way to do this, but it's not pretty. It involves using the WinForms version of MessageBox and passing an undocumented option as the last property.
var result = System.Windows.Forms.MessageBox.Show("Are you sure you want to exit this app?", "Exit", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button2, (System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);
Source: http://social.msdn.microsoft.com/Forums/vstudio/en-US/8d1bd4a2-455e-4e3f-8c88-7ed49aeabc09/messagebox-is-not-applicationmodal?forum=wpf
Hopefully this is helpful to somebody else in the future!
If this works in WPF like in Windows Forms you could just use:
MessageBox.ShowDialog()
chris
Above not working...
Edit:
But there is a workaround: style a Form like a MessageBox (use a fixed Border-Type) and then Show it using ShowDialog(). Then set the Forms Cacel and Ok Button in Properties to your Buttons and you can get a DialogResult just like in a MessageBox. Hope that Helps but it is also from Windows-Forms ;)

How do I open my closed form through the notifyicon in the taskbar?

Currently I am developing a windows form application in c# that has several forms.
I am running a background form that operates the notifyicon property that allows the icon to appear in the taskbar.
When I launch the program, it will launch a loginForm, after which logging in it will go into a mainForm. After closing the mainForm, the application does not close yet, which in this case works like Windows Live Messenger.
How do I make my program in a way that after I the mainForm, through double clicking it will bring the form back up? (Like how MSN works.)
Or is it a better solution for me to close the whole application when I press the X button in the title bar. Which brings up another problem for me as I cant seem to exit the application when I close other forms other than the main form.
Probably you have NotifyIcon on your main form. Subscribe on the DoubleClick event of this control and change state of your main form in the handler:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
Just set the Visible property of the form to true/false. Or you could call Show()/Hide().

Categories