Status bar: display time and prompt message for menu item : StatusBar « GUI Windows Form « C# / C Sharp






Status bar: display time and prompt message for menu item

Status bar: display time and prompt message for menu item
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    private StatusBar statusBar = new StatusBar();
    private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
    private StatusBarPanel sbPnlTime = new StatusBarPanel();

    private Timer timer1 = new Timer();
    private MainMenu mainMenu;
    private System.ComponentModel.Container components;

    public MainForm()
    {
      InitializeComponent();

      Text = "Status Bar Example";
      CenterToScreen();
      BackColor = Color.CadetBlue;

      // Configure the timer.
      timer1.Interval = 1000;
      timer1.Enabled = true;
      timer1.Tick += new EventHandler(timer1_Tick);
      this.MenuComplete += new EventHandler(StatusForm_MenuDone);
      BuildMenuSystem();
      BuildStatBar();
    }

    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "Form1";
    }
    static void Main() 
    {
      Application.Run(new MainForm());
    }

    // Clicked handlers.
    private void FileExit_Clicked(object sender, EventArgs e) 
    {    
      this.Close();
    }
        
    // Help | About Menu item handler
    private void HelpAbout_Clicked(object sender, EventArgs e) 
    {
      MessageBox.Show("The amazing menu app...");
    }
        
    // Selected handlers.
    private void FileExit_Selected(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Terminates this app";     
    }

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

    // Other handlers...
    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()
    {
      mainMenu = new MainMenu();
      
      MenuItem miFile = mainMenu.MenuItems.Add("&File");          
      miFile.MenuItems.Add(new MenuItem("E&xit",new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
      miFile.MenuItems[0].Select += new EventHandler(FileExit_Selected);

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

      this.Menu = mainMenu;      
    }

    private void BuildStatBar()
    {
      // Configure the status bar.
      statusBar.ShowPanels = true;
      statusBar.Panels.AddRange(new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
      
      // Configure prompt panel.
      sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
      sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
      sbPnlPrompt.Width = 62;
      sbPnlPrompt.Text = "Ready";
      
      // Configure time pane.
      sbPnlTime.Alignment = HorizontalAlignment.Right;
      sbPnlTime.Width = 76;

      try
      {
        Icon i = new Icon("status.ico");
        sbPnlPrompt.Icon = i;
      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
      }

      this.Controls.Add(statusBar);  
    }
  }


           
       








Related examples in the same category

1.Display message in StatusBar
2.Use Label as status barUse Label as status bar
3.Add icon to statusbarAdd icon to statusbar
4.Display current time on statusbarDisplay current time on statusbar
5.Display menu item alert message on the statusbarDisplay menu item alert message on the statusbar
6.StatusBar with two panelsStatusBar with two panels
7.StatusBar ExampleStatusBar Example
8.Status Strip Example
9.Use StatusBarPanel
10.Add StatusPanels to StatusBar
11.Set Text to Statusbar