Tray Icon Problem

Z

Zeljko

Hi,

I have a form with a tray icon. However, when I minimize the form, it
stays visible - not in the task bar (I have showintaskbar property set
to false), but in a form of a rectangle sitting above the task bar.
Can't figure out why that happens - it looks very ugly...

Any ideas ?

Zeljko
 
B

Brian W

When you minimize the form you must also set the Visible property to false

this.Visible = false;


HTH
Brian W
 
Z

Zeljko

But If I do that, I don't see tray icon anymore and can't maximize the
form again ?

Zeljko
 
B

Brian W

No, it won't, the tray icon remains visible. Have you tried?

You restore the form in response to a click on the icon.
 
Z

Zeljko

Yes, I have tried. tray icon becomes invisible, and there is nothing I
can click on to make it visible again.

Zeljko
 
B

Brian W

This technique has always worked for me, and is working here now. Have you
assigned an Icon to the icon property of your notifyicon object.

Can you post a small, complete example that reproduces this behavior?


Regards
Brian W
 
Z

Zeljko

Sure - here it is:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//PENDING_ICON etc are string constants - I added 3 icons to
the app, changing current notify icon based on the status of a windows
service
micoPending = new Icon(typeof(Form1), PENDING_ICON);
micoRunning = new Icon (typeof (Form1) , RUNNING_ICON);
micoStopped = new Icon (typeof (Form1) , STOPPED_ICON);

}

protected override void OnLayout(LayoutEventArgs levent)
{

switch (WindowState){
case FormWindowState.Minimized:
this.WindowState = FormWindowState.Minimized;
this.Hide();
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
break;
}
base.OnLayout (levent);
}
 
B

Brian W

Sorry, this is not complete, for example it does not show me what is going
on in InitializeComponent(), and does not show me what you are doing with
micoPending, micoRunning & micoStopped. The example also does not show me
how you are responding to either the Click or DoubleClick events of the
NotifyIcon object. For that matter it doesn't show that you even have a
TrayIcon object.

With all that said, if I add your code to mine the window minimizes and
hides with the tray icon still visible. So I'm sorry to say it still works
for me here.

Paste the following code as your form make sure you assign an icon to the
NotifyIcon object, then tell me if this works for you.


Regards
Brian W

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.NotifyIcon notifyIcon1;

private System.ComponentModel.IContainer components;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

//

// notifyIcon1

//

this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));

this.notifyIcon1.Text = "notifyIcon1";

this.notifyIcon1.Visible = true;

this.notifyIcon1.DoubleClick += new
System.EventHandler(this.notifyIcon1_DoubleClick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Name = "Form1";

this.Text = "Form1";

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_SizeChanged(object sender, System.EventArgs e)

{

if ( this.WindowState == FormWindowState.Minimized )

this.Hide();

else

this.Show();

}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)

{

this.Show();

}

}
 
Z

Zeljko

Hi,

Sorry if this code was incomplete - I left out only the obvious things
- setting icon property of notify icon to one of the 3 icons,
generated code in Initializecomponent etc.

Anyhow - in the meantime, I solved the problem by moving my hide /
minimize code from Layout to Resize event. For some reason it works
that way :)

I'm not sure why it didn't work before - if u have any ideas, I'd be
interested in hearing them :)

Zeljko

Sorry, this is not complete, for example it does not show me what is going
on in InitializeComponent(), and does not show me what you are doing with
micoPending, micoRunning & micoStopped. The example also does not show me
how you are responding to either the Click or DoubleClick events of the
NotifyIcon object. For that matter it doesn't show that you even have a
TrayIcon object.

With all that said, if I add your code to mine the window minimizes and
hides with the tray icon still visible. So I'm sorry to say it still works
for me here.

Paste the following code as your form make sure you assign an icon to the
NotifyIcon object, then tell me if this works for you.


Regards
Brian W

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.NotifyIcon notifyIcon1;

private System.ComponentModel.IContainer components;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

//

// notifyIcon1

//

this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));

this.notifyIcon1.Text = "notifyIcon1";

this.notifyIcon1.Visible = true;

this.notifyIcon1.DoubleClick += new
System.EventHandler(this.notifyIcon1_DoubleClick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Name = "Form1";

this.Text = "Form1";

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_SizeChanged(object sender, System.EventArgs e)

{

if ( this.WindowState == FormWindowState.Minimized )

this.Hide();

else

this.Show();

}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)

{

this.Show();

}

}





Zeljko said:
Sure - here it is:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//PENDING_ICON etc are string constants - I added 3 icons to
the app, changing current notify icon based on the status of a windows
service
micoPending = new Icon(typeof(Form1), PENDING_ICON);
micoRunning = new Icon (typeof (Form1) , RUNNING_ICON);
micoStopped = new Icon (typeof (Form1) , STOPPED_ICON);

}

protected override void OnLayout(LayoutEventArgs levent)
{

switch (WindowState){
case FormWindowState.Minimized:
this.WindowState = FormWindowState.Minimized;
this.Hide();
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
break;
}
base.OnLayout (levent);
}
 
L

Lou

How do you assign an icon?


Brian W said:
Sorry, this is not complete, for example it does not show me what is going
on in InitializeComponent(), and does not show me what you are doing with
micoPending, micoRunning & micoStopped. The example also does not show me
how you are responding to either the Click or DoubleClick events of the
NotifyIcon object. For that matter it doesn't show that you even have a
TrayIcon object.

With all that said, if I add your code to mine the window minimizes and
hides with the tray icon still visible. So I'm sorry to say it still works
for me here.

Paste the following code as your form make sure you assign an icon to the
NotifyIcon object, then tell me if this works for you.


Regards
Brian W

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.NotifyIcon notifyIcon1;

private System.ComponentModel.IContainer components;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

//

// notifyIcon1

//

this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));

this.notifyIcon1.Text = "notifyIcon1";

this.notifyIcon1.Visible = true;

this.notifyIcon1.DoubleClick += new
System.EventHandler(this.notifyIcon1_DoubleClick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Name = "Form1";

this.Text = "Form1";

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_SizeChanged(object sender, System.EventArgs e)

{

if ( this.WindowState == FormWindowState.Minimized )

this.Hide();

else

this.Show();

}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)

{

this.Show();

}

}





Zeljko said:
Sure - here it is:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//PENDING_ICON etc are string constants - I added 3 icons to
the app, changing current notify icon based on the status of a windows
service
micoPending = new Icon(typeof(Form1), PENDING_ICON);
micoRunning = new Icon (typeof (Form1) , RUNNING_ICON);
micoStopped = new Icon (typeof (Form1) , STOPPED_ICON);

}

protected override void OnLayout(LayoutEventArgs levent)
{

switch (WindowState){
case FormWindowState.Minimized:
this.WindowState = FormWindowState.Minimized;
this.Hide();
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
break;
}
base.OnLayout (levent);
}
 
B

Brian W

See the .Icon property of the NotifyIcon class.

Regards
Brian W


Lou said:
How do you assign an icon?


Brian W said:
Sorry, this is not complete, for example it does not show me what is going
on in InitializeComponent(), and does not show me what you are doing with
micoPending, micoRunning & micoStopped. The example also does not show me
how you are responding to either the Click or DoubleClick events of the
NotifyIcon object. For that matter it doesn't show that you even have a
TrayIcon object.

With all that said, if I add your code to mine the window minimizes and
hides with the tray icon still visible. So I'm sorry to say it still works
for me here.

Paste the following code as your form make sure you assign an icon to the
NotifyIcon object, then tell me if this works for you.


Regards
Brian W

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.NotifyIcon notifyIcon1;

private System.ComponentModel.IContainer components;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

//

// notifyIcon1

//

this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));

this.notifyIcon1.Text = "notifyIcon1";

this.notifyIcon1.Visible = true;

this.notifyIcon1.DoubleClick += new
System.EventHandler(this.notifyIcon1_DoubleClick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Name = "Form1";

this.Text = "Form1";

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_SizeChanged(object sender, System.EventArgs e)

{

if ( this.WindowState == FormWindowState.Minimized )

this.Hide();

else

this.Show();

}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)

{

this.Show();

}

}





Zeljko said:
Sure - here it is:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//PENDING_ICON etc are string constants - I added 3 icons to
the app, changing current notify icon based on the status of a windows
service
micoPending = new Icon(typeof(Form1), PENDING_ICON);
micoRunning = new Icon (typeof (Form1) , RUNNING_ICON);
micoStopped = new Icon (typeof (Form1) , STOPPED_ICON);

}

protected override void OnLayout(LayoutEventArgs levent)
{

switch (WindowState){
case FormWindowState.Minimized:
this.WindowState = FormWindowState.Minimized;
this.Hide();
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
break;
}
base.OnLayout (levent);
}






On Tue, 18 Nov 2003 10:20:32 -0800, "Brian W"

This technique has always worked for me, and is working here now.
Have
you
assigned an Icon to the icon property of your notifyicon object.

Can you post a small, complete example that reproduces this behavior?


Regards
Brian W



Yes, I have tried. tray icon becomes invisible, and there is
nothing
I maximize
the property
to task
bar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top