Focus form after use popup menu in notifyicon

P

Petr Dana

Hello,
I have form with notifyIcon and popup menu for notifyIcon. I modified
Close event - I hide form instead close. When I clik to icon in
systray, form shows. But when I selected item from popup menu over
notifyIcon, form shows too - but I don't show it, only run any code.
How do I it?
Ehh, sorry for my english :/

There is source code (I wrote it in C#Builder Personal):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Project1
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;

private bool closeFromMenu;
private System.Windows.Forms.MenuItem menuItem2;

public WinForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

closeFromMenu = false;
}

/// <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(WinForm));
this.notifyIcon1 = new
System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
this.notifyIcon1.Click += new
System.EventHandler(this.notifyIcon1_Click);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 1;
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new
System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Click";
this.menuItem2.Click += new
System.EventHandler(this.menuItem2_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(264, 237);
this.Name = "WinForm";
this.Text = "WinForm";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.WinForm_Closing);
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
closeFromMenu = true;
this.Close();
}

private void WinForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (!closeFromMenu)
{
this.Visible = false;
e.Cancel = true;
}

}

private void notifyIcon1_Click(object sender, System.EventArgs e)
{
if (!this.Visible)
{
this.Visible = true;
} else
{
this.Activate();
}

}

private void menuItem2_Click(object sender, System.EventArgs e)
{
// any code
}
}
}
 

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