Form.MenuComplete : Form « System.Windows.Forms « C# / C Sharp by API






Form.MenuComplete

  
   using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  
  internal struct TheFontSize
  {
    public static int Huge = 30;
    public static int Normal = 20;
    public static int Tiny = 8;
  }
  
  public class mainForm : System.Windows.Forms.Form
  {
    Color currColor = Color.MistyRose;
    private int currFontSize = TheFontSize.Normal;
    private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
    private StatusBarPanel sbPnlTime = new StatusBarPanel();
    private MainMenu mainMenu = new MainMenu();
    
    private MenuItem currentCheckedItem;
    private MenuItem checkedHuge;
    private MenuItem checkedNormal;
    private MenuItem checkedTiny;


    public mainForm()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);

      this.MenuComplete += new EventHandler(StatusForm_MenuDone);

      BuildMenuSystem();
      BuildStatBar();
    }

    static void Main() 
    {
      Application.Run(new mainForm());
    }

    private void FileExit_Clicked(object sender, EventArgs e) 
    {
      Console.WriteLine("File | Exit Menu item handler");
      this.Close();
    }

    private void FileSave_Clicked(object sender, EventArgs e) 
    {
            Console.WriteLine("File | Save Menu item handler");
    }

    private void ColorItem_Clicked(object sender, EventArgs e) 
    {
      MenuItem miClicked = (MenuItem)sender;
      string color = miClicked.Text.Remove(0,1);
      
      this.BackColor = Color.FromName(color);
      currColor = this.BackColor;
    }

    private void PopUp_Clicked(object sender, EventArgs e) 
    {
      currentCheckedItem.Checked = false;

      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text;
      
      if(item == "Huge") {
        currFontSize = TheFontSize.Huge;
        currentCheckedItem = checkedHuge;
      }else if(item == "Normal") {
        currFontSize = TheFontSize.Normal;
        currentCheckedItem = checkedNormal;
      }else if(item == "Tiny") {
        currFontSize = TheFontSize.Tiny;
        currentCheckedItem = checkedTiny;
      }
      currentCheckedItem.Checked = true;
      Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;

      g.DrawString("www.java2s.com", 
        new Font("Times New Roman", (float)currFontSize), 
        new SolidBrush(Color.Black), 
        this.DisplayRectangle);
    }
    
    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      Invalidate();
    }

    private void HelpAbout_Clicked(object sender, EventArgs e) 
    {
      Console.WriteLine("The amazing final app...", "About...");
    }
        
    private void FileMenuItem_Selected(object sender, EventArgs e) 
    {
      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text.Remove(0,1);
      
      if(item == "Save..."){
        sbPnlPrompt.Text = "Save current settings.";     
      }else{
        sbPnlPrompt.Text = "Terminates this app.";     
        } 
    }

    private void ColorMenuItem_Selected(object sender, EventArgs e) 
    {
      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text.Remove(0,1);
      sbPnlPrompt.Text = "Select " + item;            
    }

    private void HelpAbout_Selected(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Displays app info";
    }

    private void StatusForm_MenuDone(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Ready";
    }

    private void timer1_Tick(object sender, EventArgs e) 
    {
      DateTime t = DateTime.Now;
      string s = t.ToLongTimeString() ;
      sbPnlTime.Text = s ;    
    }

    private void BuildMenuSystem()
    {
      MenuItem miFile = mainMenu.MenuItems.Add("&File");           
      miFile.MenuItems.Add(new MenuItem("&Save...", new EventHandler(this.FileSave_Clicked), Shortcut.CtrlS));     
      miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));

      miFile.MenuItems[0].Select += new EventHandler(FileMenuItem_Selected);
      miFile.MenuItems[1].Select += new EventHandler(FileMenuItem_Selected);

      MenuItem miColor = mainMenu.MenuItems.Add("&Background Color");
      miColor.MenuItems.Add("&DarkGoldenrod", new EventHandler(ColorItem_Clicked));
      miColor.MenuItems.Add("&GreenYellow", new EventHandler(ColorItem_Clicked));
      
      for(int i = 0; i < miColor.MenuItems.Count; i++){
        miColor.MenuItems[i].Select += new EventHandler(ColorMenuItem_Selected);
            }

      MenuItem miHelp = mainMenu.MenuItems.Add("Help");  
      miHelp.MenuItems.Add(new MenuItem("&amp;About", new EventHandler(this.HelpAbout_Clicked), Shortcut.CtrlA));
      miHelp.MenuItems[0].Select += new EventHandler(HelpAbout_Selected);

      this.Menu = mainMenu;  

            ContextMenu popUpMenu = new ContextMenu();

      popUpMenu.MenuItems.Add("Huge", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Normal", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Tiny", new EventHandler(PopUp_Clicked));

      this.ContextMenu = popUpMenu;

      checkedHuge = this.ContextMenu.MenuItems[0];
      checkedNormal = this.ContextMenu.MenuItems[1];        
      checkedTiny = this.ContextMenu.MenuItems[2];
      
      if(currFontSize == TheFontSize.Huge)
        currentCheckedItem = checkedHuge;
      else if(currFontSize == TheFontSize.Normal)
        currentCheckedItem = checkedNormal;
      else
        currentCheckedItem = checkedTiny;

      currentCheckedItem.Checked = true;
    }

    private void BuildStatBar()
    {
        Timer timer1 = new Timer();
      timer1.Interval = 1000;
      timer1.Enabled = true;
      timer1.Tick += new EventHandler(timer1_Tick);

        StatusBar statusBar = new StatusBar();
        
      statusBar.ShowPanels = true;
      statusBar.Panels.AddRange((StatusBarPanel[])new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});

      sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
      sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
      sbPnlPrompt.Width = 62;
      sbPnlPrompt.Text = "Ready";

      sbPnlTime.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
      sbPnlTime.Width = 76;

      try
      {
        Icon i = new Icon("icon1.ico");
        sbPnlPrompt.Icon = i;
      } catch(Exception e) {
        MessageBox.Show(e.Message);
      }

      this.Controls.Add(statusBar);  
    }

  }

   
    
  








Related examples in the same category

1.new Form()
2.extends Form
3.Form.AcceptButton
4.Form.ActiveForm.Location
5.Form.Activated
6.Form.AddOwnedForm
7.Form.AutoScaleBaseSize
8.Form.AutoScroll
9.Form.AutoScrollMinSize
10.Form.BackColor
11.Form.CancelButton
12.Form.CenterToScreen()
13.Form.ClientRectangle
14.Form.ClientSize
15.Form.Closed
16.Form.Closing
17.Form.ContextMenu
18.Form.ControlBox
19.Form.Controls
20.Form.Cursor
21.Form.DesktopBounds
22.Form.DialogResult
23.Form.DisplayRectangle
24.Form.Dispose
25.Form.FormBorderStyle
26.Form.ForeColor
27.Form.GotFocus
28.Form.Height
29.Form.Hide()
30.Form.Invalidate
31.Form.IsMdiContainer
32.Form.KeyPress
33.Form.KeyPreview
34.Form.KeyUp
35.Form.Load
36.Form.Location
37.Form.MaximizeBox
38.Form.MaximumSize
39.Form.MdiParent
40.Form.Menu
41.Form.MinimizeBox
42.Form.MinimumSize
43.Form.MouseDown
44.Form.MouseLeave(All other events)
45.Form.MouseUp
46.Form.OnDragDrop
47.Form.OnDragOver
48.Form.OnInputLanguageChanged
49.Form.OnKeyDown
50.Form.OnKeyPress
51.Form.OnMouseEnter
52.Form.OnMouseHover
53.Form.OnMouseLeave
54.Form.OnMouseMove
55.Form.OnMouseWheel
56.Form.OnPaint(PaintEventArgs e)
57.Form.OnResize
58.Form.Opacity
59.Form.Paint
60.Form.Region
61.Form.Resize
62.Form.ResizeRedraw
63.Form.SetStyle
64.Form.ShowInTaskbar
65.Form.Size
66.Form.Show
67.Form.StartPosition
68.Form.SuspendLayout
69.Form.Text
70.Form.Top
71.Form.TransparencyKey
72.Form.Visible
73.Form.Width
74.Form.WindowState