Running Forms

  • Thread starter Thread starter Joshua Russell
  • Start date Start date
J

Joshua Russell

Hi,
Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of master form. Method 2 opens the form but when I right click on the Notify Icon I don't get the context menu that I should be seeing. I can interact with the main form window but I cannot interact with the NotifyIcon. Method 2 gives a object reference that I require (masterForm) where as method 1 doesn't.
Any Ideas Anyone?

Method 1:
class FormHandler
{
public void ShowForm()
{
Application.Run(new MasterForm());
}
}

Method 2:
class FormHandler
{
private static MasterForm masterForm = new MasterForm();

public void ShowForm()
{
Application.Run(masterForm);
}
}

Thanx Josh
 
What NotifyIcon? Since we don't know how your form is coded, we'll need more information (like revelant parts of source code).

-mike
MVP
Hi,
Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of master form. Method 2 opens the form but when I right click on the Notify Icon I don't get the context menu that I should be seeing. I can interact with the main form window but I cannot interact with the NotifyIcon. Method 2 gives a object reference that I require (masterForm) where as method 1 doesn't.
Any Ideas Anyone?

Method 1:
class FormHandler
{
public void ShowForm()
{
Application.Run(new MasterForm());
}
}

Method 2:
class FormHandler
{
private static MasterForm masterForm = new MasterForm();

public void ShowForm()
{
Application.Run(masterForm);
}
}

Thanx Josh
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Threading_Server_WF
{
/// <summary>
/// Summary description for Master.
/// </summary>
public class MasterForm : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.ContextMenu notifyContext;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.Label label3;
private System.ComponentModel.IContainer components;

public MasterForm()
{
//
// 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(MasterForm));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.notifyContext = new System.Windows.Forms.ContextMenu();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(48, 48);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(80, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Mithril Server";
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Version 1.0 Beta";
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.notifyContext;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Mithril Server";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// notifyContext
//
this.notifyContext.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5});
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Show Info";
this.menuItem4.Click += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "Close Server";
this.menuItem5.Click += new System.EventHandler(this.menuItem3_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "Main";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Hide";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Close Server";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(80, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 16);
this.label3.TabIndex = 3;
this.label3.Text = "Active Sessions: 0";
//
// MasterForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 83);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label3,
this.label2,
this.label1,
this.pictureBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "MasterForm";
this.ShowInTaskbar = false;
this.Text = "Mithril Server";
this.Activated += new System.EventHandler(this.MasterForm_Activated);
this.ResumeLayout(false);

}
#endregion

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg != 0x0010)
{
base.WndProc(ref m);
}
else
{
this.WindowState = FormWindowState.Minimized;
}
}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
// Show the form when the user double clicks on the notify icon.

// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;

// Activate the form.
this.Activate();
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
// Clearup
this.notifyIcon1.Visible = false;

// Exit
Environment.Exit(0);
}

private void MasterForm_Activated(object sender, System.EventArgs e)
{
WorkerThreadHandler threadHandler = new WorkerThreadHandler();
int count = threadHandler.sessionCount();
this.label3.Text = "Active Sessions: " + count.ToString();
}
}
/// <summary>
/// Summary description for Master.
/// </summary>
public class MasterForm : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.ContextMenu notifyContext;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.Label label3;
private System.ComponentModel.IContainer components;

public MasterForm()
{
//
// 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(MasterForm));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.notifyContext = new System.Windows.Forms.ContextMenu();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(48, 48);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(80, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Mithril Server";
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Version 1.0 Beta";
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.notifyContext;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Mithril Server";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// notifyContext
//
this.notifyContext.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5});
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Show Info";
this.menuItem4.Click += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "Close Server";
this.menuItem5.Click += new System.EventHandler(this.menuItem3_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "Main";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Hide";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Close Server";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(80, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 16);
this.label3.TabIndex = 3;
this.label3.Text = "Active Sessions: 0";
//
// MasterForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 83);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label3,
this.label2,
this.label1,
this.pictureBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "MasterForm";
this.ShowInTaskbar = false;
this.Text = "Mithril Server";
this.Activated += new System.EventHandler(this.MasterForm_Activated);
this.ResumeLayout(false);

}
#endregion

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg != 0x0010)
{
base.WndProc(ref m);
}
else
{
this.WindowState = FormWindowState.Minimized;
}
}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
// Show the form when the user double clicks on the notify icon.

// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;

// Activate the form.
this.Activate();
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
// Clearup
this.notifyIcon1.Visible = false;

// Exit
Environment.Exit(0);
}

private void MasterForm_Activated(object sender, System.EventArgs e)
{
WorkerThreadHandler threadHandler = new WorkerThreadHandler();
int count = threadHandler.sessionCount();
this.label3.Text = "Active Sessions: " + count.ToString();
}
}
}
What NotifyIcon? Since we don't know how your form is coded, we'll need more information (like revelant parts of source code).

-mike
MVP
Hi,
Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of master form. Method 2 opens the form but when I right click on the Notify Icon I don't get the context menu that I should be seeing. I can interact with the main form window but I cannot interact with the NotifyIcon. Method 2 gives a object reference that I require (masterForm) where as method 1 doesn't.
Any Ideas Anyone?

Method 1:
class FormHandler
{
public void ShowForm()
{
Application.Run(new MasterForm());
}
}

Method 2:
class FormHandler
{
private static MasterForm masterForm = new MasterForm();

public void ShowForm()
{
Application.Run(masterForm);
}
}

Thanx Josh
 
Sorry, I meant *relevant* parts of source code. Can you show us your problem without pasting 451 lines of code?

-mike
MVP
using System;

<snip>

}
What NotifyIcon? Since we don't know how your form is coded, we'll need more information (like revelant parts of source code).

-mike
MVP
Hi,
Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of master form. Method 2 opens the form but when I right click on the Notify Icon I don't get the context menu that I should be seeing. I can interact with the main form window but I cannot interact with the NotifyIcon. Method 2 gives a object reference that I require (masterForm) where as method 1 doesn't.
Any Ideas Anyone?

Method 1:
class FormHandler
{
public void ShowForm()
{
Application.Run(new MasterForm());
}
}

Method 2:
class FormHandler
{
private static MasterForm masterForm = new MasterForm();

public void ShowForm()
{
Application.Run(masterForm);
}
}

Thanx Josh
 
Back
Top