Search code examples
c#winformstrayicon

After loading my application is not completly hide in tray


I have code like this:

public form()
{
    InitializeComponent();
    init(); //read ini and try to minimize
}

private void timer1_Tick(object sender, EventArgs e)
{
}

and in ini method I minimize form and hide it(in debug i can see form.visible = false), but when it leaves init method then it jumps on timer and change visible = true and i can see my app in taskbar and tray. I want see only tray icon. I use this to minimize form to tray.

So far i made this,but maybe is implemented wrong way because when form is showed form made something like refresh and it looks strange.

private void notifyIcon1_Click(object sender, EventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
    }

    private void minimizeWindow()//this method is called on form resize
    {
        if (FormWindowState.Minimized == this.WindowState)
        {
            notifyIcon1.Visible = true;
            this.Hide();
            this.ShowInTaskbar = false;
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;
        }
    }

Solution

  • solution is simple:

            private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }
    
        private void minimizeWindow()
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }
    

    and

            private void AAL_Load(object sender, EventArgs e)
        {
            minimizeWindow();//call it again
        }
    

    Dont use form.ShowInTaskbar = false; because that make many problems.