Enabling and Disabling Controls

 

A Pop-up Menu (Context Menu)

Most pop-up menus appear only when the user places the mouse pointer on some control or menu item on a window and right-clicks on that item. This is the method we will use to actuate the pop-up context menu you will build.

To place a context menu on the existing main window of project MenuMenu, here is what you do:

  1. Highlight the main window in the window template, move to the Toolbox , and click on the ContextMenuStrip icon under Menus & Toolbars.

  2. Move to the window template and click over it to bring the context menu code into the project. Nothing will show on the main window ” only a rectangle will appear at the bottom of the IDE screen with the words contextMenuStrip1.

    If you right-click on the contextMenu1 rectangle at the bottom of the IDE, a context menu will appear at the top of the window template (where the main menu was originally). Do not be concerned ; you have done nothing wrong. You have not destroyed the main menu created earlier. The IDE is providing a place to enter the context menu items, and the best place (so far as the IDE architects are concerned) is the same place as the main menu.

  3. A context menu usually lists a single column of items from top to bottom (no lower- tier items usually appear to the right of the main column). In the first slot of the context menu, type Jump to the Left ; in the space below, type Jump to the Right ; in the next space below, type Jump into the Middle ; and in the next space below, type And Fight Team, Fight!

There are two questions that remain to be answered in this construct:

  • How does the user actuate this context window?

  • Where are the event handlers for the list of items in the context menu?

The answer to the first question is: There is nothing on the window right now that can act as the catalyst to actuate the context window. The only tool the user has is the mouse. So we will construct a system such that any time the user right-clicks on the main window, the context menu will appear. There are multiple steps in this process:

  1. Highlight the main window template to bring its properties into the Properties window.

  2. Click on the Events icon (the bolt of lightning) at the top of the window to bring the event handlers to the front.

  3. Scroll down the list of event handlers to Mouse and double-click on the MouseUp entry to create an event handler named Form1_MouseUp. It will look like this:

     private void Form1_MouseUp(object sender, System.Windows.Forms. MouseEventArgs e) { } 
  4. Within this new event handler, place this code:

     // If the right mouse button was clicked and released, // display the context menu. if(e.Button == MouseButtons.Right) contextMenuStrip1.Show((Control) sender, new Point(e.X, e.Y)); // Point (e.X, e.Y) is the present location of the mouse on the screen. 

Note that the reference to e above is the argument in the Form1_ MouseUp handler. This code is shown in lines MM037-MM040 of the MenuMenu listing below.

The answer to the second question is: In the window template, click on the contextMenu1 rectangle at the bottom of the IDE to highlight the context menu and display it on the template. Click on each item in the context menu to highlight each entry, and double-click on each to create an event handler. Each time an event handler is created you will be shown the new code in the FormX.cs file.

This completes the discussion of main menus and context menus. A complete listing of MenuMenu follows . The project (in addition to the one you have created) is located at Visual Studio 2005\Projects\DemosSourceCode\MenuMenu. The project you have just created with the same name , MenuMenu, is located at Visual Studio 2005\Projects\ MenuMenu.

MenuMenu (MM) Listing

 Form1.cs: MM002:        using System; MM003:        using System.Collections.Generic; MM004:        using System.ComponentModel; MM005:        using System.Data; MM006:        using System.Drawing; MM007:        using System.Windows.Forms; MM009:        namespace MenuMenu MM010:        { MM011:          partial class Form1 : Form MM012:          { MM013:            public Form1() MM014:            {InitializeComponent(); } //-----------------------------------------------------------------------------------------// MM020:            private void itemsShippedToolStripMenuItem_Click(object sender, EventArgs e) MM021:            { } MM022:            private void catalogSalesToolStripMenuItem_Click(object sender, EventArgs e) MM023:            { } MM024:            private void specialSalesToolStripMenuItem_Click(object sender, EventArgs e) MM025:            { } MM026:            private void facilitySupportToolStripMenuItem_Click(object sender,                           EventArgs e) MM027:            { } MM028:            private void awaitingShipmentToolStripMenuItem_Click(object sender,                           EventArgs e) MM029:            { } MM030:            private void shippedOutToolStripMenuItem_Click(object sender, EventArgs e) MM031:            { }  MM032:            private void itemstobeReworkedToolStripMenuItem_Click(object sender,                           EventArgs e) MM033:            { } MM034:            private void inventoryReconciliationToolStripMenuItem_Click(object                           sender,EventArgs e) MM035:            { } //-----------------------------------------------------------------------------------------// MM036:            private void Form1_MouseUp(object sender, MouseEventArgs e) MM037:            { // If the right mouse button was clicked and released, MM038:              // display the context menu. MM039:              if (e.Button == MouseButtons.Right) MM040:              contextMenuStrip1.Show((Control)sender, new Point(e.X, e.Y));                     // Point (e.X, e.Y) is the present location of the 'mouse' on the screen. MM041:            } //-----------------------------------------------------------------------------------------// MM042:            private void jumptotheleftToolStripMenuItem_Click(object sender,                           EventArgs e) MM043:            { MM044:              MessageBox.Show("Jump to the Left"); MM045:              return; MM046:            } //-----------------------------------------------------------------------------------------// MM047:            private void jumptotherightToolStripMenuItem_Click(object sender,                           EventArgs e) MM048:            { MM049:              MessageBox.Show("Jump to the Right"); MM050:              return; MM051:            } //-----------------------------------------------------------------------------------------// MM052:            private void jumpinthemiddleToolStripMenuItem_Click(object sender,                           EventArgs e) MM053:            { MM054:              MessageBox.Show("Jump into the Middle"); MM055:              return; MM056:            } //-----------------------------------------------------------------------------------------// MM057:            private void andfightteamfightToolStripMenuItem_Click(object sender,                           EventArgs e) MM058:            { MM059:              MessageBox.Show("And Fight, Team Fight!"); MM060:              return; MM061:            } MM062:          } MM063:        } //=========================================================================================// Form1.Designer.cs: MM070:        namespace MenuMenu MM071:        { MM072:          partial class Form1 MM073:          {                   // Required designer variable. MM074:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. MM075:            protected override void Dispose(bool disposing) MM076:            { MM077:              if (disposing && (components != null)) components.Dispose(); MM078:              base.Dispose(disposing); MM079:            } MM080:            #region Windows Form Designer generated code                   // Required method for Designer support. MM081:            private void InitializeComponent() MM082:            { MM083:              this.components = new System.ComponentModel.Container();                     System.ComponentModel.ComponentResourceManager resources = new                       System.ComponentModel.ComponentResourceManager(typeof(Form1)); MM084:              this.menuStrip1 = new System.Windows.Forms.MenuStrip(); MM085:              this.leftRaftingContainer = new System.Windows.Forms.RaftingContainer();                     [50 lines of code] MM136:              this.topRaftingContainer.SuspendLayout(); MM137:              ((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)). MM138:              this.SuspendLayout();                     //                     // menuStrip1                     //                     [264 lines of code]                     //                     // Form1                     // MM403:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); MM404:              this.ClientSize = new System.Drawing.Size(492, 116); MM405:              this.Controls.Add(this.leftRaftingContainer); MM406:              this.Controls.Add(this.rightRaftingContainer); MM407:              this.Controls.Add(this.topRaftingContainer); MM408:              this.Controls.Add(this.bottomRaftingContainer); MM409:              this.Name = "Form1"; MM410:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; MM411:              this.Text = "Menu Menu"; MM412:              this.MouseUp += new                     System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); MM413:              this.menuStrip1.ResumeLayout(false); MM414:              ((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer)).                              EndInit(); MM415:              ((System.ComponentModel.ISupportInitialize)(this.rightRaftingContainer)).                              EndInit(); MM416:              ((System.ComponentModel.ISupportInitialize)(this.topRaftingContainer)).                              EndInit(); MM417:              this.topRaftingContainer.ResumeLayout(false); MM418:              this.topRaftingContainer.PerformLayout(); MM419:              ((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)).                              EndInit(); MM420:              this.contextMenuStrip1.ResumeLayout(false); MM421:              this.ResumeLayout(false); MM422:              this.PerformLayout(); MM423:            } MM424:            #endregion MM425:            private System.Windows.Forms.MenuStrip menuStrip1; MM426:            private System.Windows.Forms.RaftingContainer leftRaftingContainer;                   [48 lines of code] MM476:          } MM477:        } 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net