Running Forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua Russell

    #1

    Running Forms

    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



  • Michael Giagnocavo [MVP]

    #2
    Re: Running Forms

    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
    "Joshua Russell" <josh@ojmyster. uk.eu.org> wrote in message news:OuBjSBhpDH A.2676@TK2MSFTN GP11.phx.gbl...
    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



    Comment

    • Joshua Russell

      #3
      Re: Running Forms

      using System;
      using System.Drawing;
      using System.Collecti ons;
      using System.Componen tModel;
      using System.Windows. Forms;

      namespace Threading_Serve r_WF
      {
      /// <summary>
      /// Summary description for Master.
      /// </summary>
      public class MasterForm : System.Windows. Forms.Form
      {
      private System.Windows. Forms.PictureBo x pictureBox1;
      private System.Windows. Forms.Label label1;
      private System.Windows. Forms.Label label2;
      private System.Windows. Forms.NotifyIco n 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.ContextMe nu notifyContext;
      private System.Windows. Forms.MenuItem menuItem4;
      private System.Windows. Forms.MenuItem menuItem5;
      private System.Windows. Forms.Label label3;
      private System.Componen tModel.IContain er components;

      public MasterForm()
      {
      //
      // Required for Windows Form Designer support
      //
      InitializeCompo nent();

      //
      // TODO: Add any constructor code after InitializeCompo nent call
      //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
      if( disposing )
      {
      if(components != null)
      {
      components.Disp ose();
      }
      }
      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 InitializeCompo nent()
      {
      this.components = new System.Componen tModel.Containe r();
      System.Resource s.ResourceManag er resources = new System.Resource s.ResourceManag er(typeof(Maste rForm));
      this.pictureBox 1 = new System.Windows. Forms.PictureBo x();
      this.label1 = new System.Windows. Forms.Label();
      this.label2 = new System.Windows. Forms.Label();
      this.notifyIcon 1 = new System.Windows. Forms.NotifyIco n(this.componen ts);
      this.notifyCont ext = new System.Windows. Forms.ContextMe nu();
      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.SuspendLay out();
      //
      // pictureBox1
      //
      this.pictureBox 1.Image = ((System.Drawin g.Bitmap)(resou rces.GetObject( "pictureBox1.Im age")));
      this.pictureBox 1.Location = new System.Drawing. Point(8, 8);
      this.pictureBox 1.Name = "pictureBox 1";
      this.pictureBox 1.Size = new System.Drawing. Size(48, 48);
      this.pictureBox 1.TabIndex = 0;
      this.pictureBox 1.TabStop = false;
      //
      // label1
      //
      this.label1.Fon t = new System.Drawing. Font("Microsoft Sans Serif", 8.25F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));
      this.label1.Loc ation = new System.Drawing. Point(80, 16);
      this.label1.Nam e = "label1";
      this.label1.Siz e = new System.Drawing. Size(80, 16);
      this.label1.Tab Index = 1;
      this.label1.Tex t = "Mithril Server";
      //
      // label2
      //
      this.label2.Loc ation = new System.Drawing. Point(80, 32);
      this.label2.Nam e = "label2";
      this.label2.Siz e = new System.Drawing. Size(88, 16);
      this.label2.Tab Index = 2;
      this.label2.Tex t = "Version 1.0 Beta";
      //
      // notifyIcon1
      //
      this.notifyIcon 1.ContextMenu = this.notifyCont ext;
      this.notifyIcon 1.Icon = ((System.Drawin g.Icon)(resourc es.GetObject("n otifyIcon1.Icon ")));
      this.notifyIcon 1.Text = "Mithril Server";
      this.notifyIcon 1.Visible = true;
      this.notifyIcon 1.DoubleClick += new System.EventHan dler(this.notif yIcon1_DoubleCl ick);
      //
      // notifyContext
      //
      this.notifyCont ext.MenuItems.A ddRange(new System.Windows. Forms.MenuItem[] {
      this.menuItem4,
      this.menuItem5} );
      //
      // menuItem4
      //
      this.menuItem4. Index = 0;
      this.menuItem4. Text = "Show Info";
      this.menuItem4. Click += new System.EventHan dler(this.notif yIcon1_DoubleCl ick);
      //
      // menuItem5
      //
      this.menuItem5. Index = 1;
      this.menuItem5. Text = "Close Server";
      this.menuItem5. Click += new System.EventHan dler(this.menuI tem3_Click);
      //
      // mainMenu1
      //
      this.mainMenu1. MenuItems.AddRa nge(new System.Windows. Forms.MenuItem[] {
      this.menuItem1} );
      //
      // menuItem1
      //
      this.menuItem1. Index = 0;
      this.menuItem1. MenuItems.AddRa nge(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.EventHan dler(this.menuI tem2_Click);
      //
      // menuItem3
      //
      this.menuItem3. Index = 1;
      this.menuItem3. Text = "Close Server";
      this.menuItem3. Click += new System.EventHan dler(this.menuI tem3_Click);
      //
      // label3
      //
      this.label3.Loc ation = new System.Drawing. Point(80, 56);
      this.label3.Nam e = "label3";
      this.label3.Siz e = new System.Drawing. Size(104, 16);
      this.label3.Tab Index = 3;
      this.label3.Tex t = "Active Sessions: 0";
      //
      // MasterForm
      //
      this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
      this.ClientSize = new System.Drawing. Size(208, 83);
      this.Controls.A ddRange(new System.Windows. Forms.Control[] {
      this.label3,
      this.label2,
      this.label1,
      this.pictureBox 1});
      this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedDia log;
      this.Icon = ((System.Drawin g.Icon)(resourc es.GetObject("$ this.Icon")));
      this.MaximizeBo x = false;
      this.Menu = this.mainMenu1;
      this.Name = "MasterForm ";
      this.ShowInTask bar = false;
      this.Text = "Mithril Server";
      this.Activated += new System.EventHan dler(this.Maste rForm_Activated );
      this.ResumeLayo ut(false);

      }
      #endregion

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

      private void notifyIcon1_Dou bleClick(object sender, System.EventArg s 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.WindowSta te == FormWindowState .Minimized)
      this.WindowStat e = FormWindowState .Normal;

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

      private void menuItem2_Click (object sender, System.EventArg s e)
      {
      this.WindowStat e = FormWindowState .Minimized;
      }

      private void menuItem3_Click (object sender, System.EventArg s e)
      {
      // Clearup
      this.notifyIcon 1.Visible = false;

      // Exit
      Environment.Exi t(0);
      }

      private void MasterForm_Acti vated(object sender, System.EventArg s e)
      {
      WorkerThreadHan dler threadHandler = new WorkerThreadHan dler();
      int count = threadHandler.s essionCount();
      this.label3.Tex t = "Active Sessions: " + count.ToString( );
      }
      }
      /// <summary>
      /// Summary description for Master.
      /// </summary>
      public class MasterForm : System.Windows. Forms.Form
      {
      private System.Windows. Forms.PictureBo x pictureBox1;
      private System.Windows. Forms.Label label1;
      private System.Windows. Forms.Label label2;
      private System.Windows. Forms.NotifyIco n 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.ContextMe nu notifyContext;
      private System.Windows. Forms.MenuItem menuItem4;
      private System.Windows. Forms.MenuItem menuItem5;
      private System.Windows. Forms.Label label3;
      private System.Componen tModel.IContain er components;

      public MasterForm()
      {
      //
      // Required for Windows Form Designer support
      //
      InitializeCompo nent();

      //
      // TODO: Add any constructor code after InitializeCompo nent call
      //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
      if( disposing )
      {
      if(components != null)
      {
      components.Disp ose();
      }
      }
      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 InitializeCompo nent()
      {
      this.components = new System.Componen tModel.Containe r();
      System.Resource s.ResourceManag er resources = new System.Resource s.ResourceManag er(typeof(Maste rForm));
      this.pictureBox 1 = new System.Windows. Forms.PictureBo x();
      this.label1 = new System.Windows. Forms.Label();
      this.label2 = new System.Windows. Forms.Label();
      this.notifyIcon 1 = new System.Windows. Forms.NotifyIco n(this.componen ts);
      this.notifyCont ext = new System.Windows. Forms.ContextMe nu();
      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.SuspendLay out();
      //
      // pictureBox1
      //
      this.pictureBox 1.Image = ((System.Drawin g.Bitmap)(resou rces.GetObject( "pictureBox1.Im age")));
      this.pictureBox 1.Location = new System.Drawing. Point(8, 8);
      this.pictureBox 1.Name = "pictureBox 1";
      this.pictureBox 1.Size = new System.Drawing. Size(48, 48);
      this.pictureBox 1.TabIndex = 0;
      this.pictureBox 1.TabStop = false;
      //
      // label1
      //
      this.label1.Fon t = new System.Drawing. Font("Microsoft Sans Serif", 8.25F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));
      this.label1.Loc ation = new System.Drawing. Point(80, 16);
      this.label1.Nam e = "label1";
      this.label1.Siz e = new System.Drawing. Size(80, 16);
      this.label1.Tab Index = 1;
      this.label1.Tex t = "Mithril Server";
      //
      // label2
      //
      this.label2.Loc ation = new System.Drawing. Point(80, 32);
      this.label2.Nam e = "label2";
      this.label2.Siz e = new System.Drawing. Size(88, 16);
      this.label2.Tab Index = 2;
      this.label2.Tex t = "Version 1.0 Beta";
      //
      // notifyIcon1
      //
      this.notifyIcon 1.ContextMenu = this.notifyCont ext;
      this.notifyIcon 1.Icon = ((System.Drawin g.Icon)(resourc es.GetObject("n otifyIcon1.Icon ")));
      this.notifyIcon 1.Text = "Mithril Server";
      this.notifyIcon 1.Visible = true;
      this.notifyIcon 1.DoubleClick += new System.EventHan dler(this.notif yIcon1_DoubleCl ick);
      //
      // notifyContext
      //
      this.notifyCont ext.MenuItems.A ddRange(new System.Windows. Forms.MenuItem[] {
      this.menuItem4,
      this.menuItem5} );
      //
      // menuItem4
      //
      this.menuItem4. Index = 0;
      this.menuItem4. Text = "Show Info";
      this.menuItem4. Click += new System.EventHan dler(this.notif yIcon1_DoubleCl ick);
      //
      // menuItem5
      //
      this.menuItem5. Index = 1;
      this.menuItem5. Text = "Close Server";
      this.menuItem5. Click += new System.EventHan dler(this.menuI tem3_Click);
      //
      // mainMenu1
      //
      this.mainMenu1. MenuItems.AddRa nge(new System.Windows. Forms.MenuItem[] {
      this.menuItem1} );
      //
      // menuItem1
      //
      this.menuItem1. Index = 0;
      this.menuItem1. MenuItems.AddRa nge(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.EventHan dler(this.menuI tem2_Click);
      //
      // menuItem3
      //
      this.menuItem3. Index = 1;
      this.menuItem3. Text = "Close Server";
      this.menuItem3. Click += new System.EventHan dler(this.menuI tem3_Click);
      //
      // label3
      //
      this.label3.Loc ation = new System.Drawing. Point(80, 56);
      this.label3.Nam e = "label3";
      this.label3.Siz e = new System.Drawing. Size(104, 16);
      this.label3.Tab Index = 3;
      this.label3.Tex t = "Active Sessions: 0";
      //
      // MasterForm
      //
      this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
      this.ClientSize = new System.Drawing. Size(208, 83);
      this.Controls.A ddRange(new System.Windows. Forms.Control[] {
      this.label3,
      this.label2,
      this.label1,
      this.pictureBox 1});
      this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedDia log;
      this.Icon = ((System.Drawin g.Icon)(resourc es.GetObject("$ this.Icon")));
      this.MaximizeBo x = false;
      this.Menu = this.mainMenu1;
      this.Name = "MasterForm ";
      this.ShowInTask bar = false;
      this.Text = "Mithril Server";
      this.Activated += new System.EventHan dler(this.Maste rForm_Activated );
      this.ResumeLayo ut(false);

      }
      #endregion

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

      private void notifyIcon1_Dou bleClick(object sender, System.EventArg s 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.WindowSta te == FormWindowState .Minimized)
      this.WindowStat e = FormWindowState .Normal;

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

      private void menuItem2_Click (object sender, System.EventArg s e)
      {
      this.WindowStat e = FormWindowState .Minimized;
      }

      private void menuItem3_Click (object sender, System.EventArg s e)
      {
      // Clearup
      this.notifyIcon 1.Visible = false;

      // Exit
      Environment.Exi t(0);
      }

      private void MasterForm_Acti vated(object sender, System.EventArg s e)
      {
      WorkerThreadHan dler threadHandler = new WorkerThreadHan dler();
      int count = threadHandler.s essionCount();
      this.label3.Tex t = "Active Sessions: " + count.ToString( );
      }
      }
      }
      "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message news:ueDILzhpDH A.2500@TK2MSFTN GP10.phx.gbl...
      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
      "Joshua Russell" <josh@ojmyster. uk.eu.org> wrote in message news:OuBjSBhpDH A.2676@TK2MSFTN GP11.phx.gbl...
      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



      Comment

      • Michael Giagnocavo [MVP]

        #4
        Re: Running Forms

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

        -mike
        MVP
        "Joshua Russell" <josh@ojmyster. uk.eu.org> wrote in message news:eFPJXfkpDH A.2512@TK2MSFTN GP09.phx.gbl...
        using System;

        <snip>

        }
        "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message news:ueDILzhpDH A.2500@TK2MSFTN GP10.phx.gbl...
        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
        "Joshua Russell" <josh@ojmyster. uk.eu.org> wrote in message news:OuBjSBhpDH A.2676@TK2MSFTN GP11.phx.gbl...
        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



        Comment

        Working...