Csharp/CSharp Tutorial/GUI Windows Forms/StatusBar

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add Icon to StatusBar

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormWithStatusBarIcon : 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.ruponentModel.Container components;
  public FormWithStatusBarIcon()
  {
    InitializeComponent();
    timer1.Interval = 1000;
    timer1.Enabled = true;
    timer1.Tick += new EventHandler(timer1_Tick);
    this.MenuComplete += new EventHandler(StatusForm_MenuDone);
    BuildMenuSystem();
    BuildStatBar();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.ruponents = new System.ruponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormWithStatusBarIcon());
  }
  private void FileExit_Clicked(object sender, EventArgs e) 
  {    
    this.Close();
  }
    
  private void HelpAbout_Clicked(object sender, EventArgs e) 
  {
    sbPnlPrompt.Text = "Help";
  }
    
  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";
  }
  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()
  {
    statusBar.ShowPanels = true;
    statusBar.Panels.AddRange(new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
    
    sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
    sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
    sbPnlPrompt.Width = 62;
    sbPnlPrompt.Text = "Ready";
    Icon i = new Icon("status.ico");
    sbPnlPrompt.Icon = i;
    
    sbPnlTime.Alignment = HorizontalAlignment.Right;
    sbPnlTime.Width = 76;
    this.Controls.Add(statusBar);  
  }
}

Date and Time in StatusBar

using System;
using System.Drawing;
using System.Windows.Forms;
   
class DateAndTimeStatus: Form
{
     StatusBarPanel sbpMenu, sbpDate, sbpTime;
   
     public static void Main()
     {
          Application.Run(new DateAndTimeStatus());
     }
     public DateAndTimeStatus()
     {
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.ShowPanels = true;
   
          sbpMenu = new StatusBarPanel();
          sbpMenu.Text = "Reserved for menu help";
          sbpMenu.BorderStyle = StatusBarPanelBorderStyle.None;
          sbpMenu.AutoSize = StatusBarPanelAutoSize.Spring;
   
          sbpDate = new StatusBarPanel();
          sbpDate.AutoSize = StatusBarPanelAutoSize.Contents;
          sbpDate.ToolTipText = "The current date";
   
          sbpTime = new StatusBarPanel();
          sbpTime.AutoSize = StatusBarPanelAutoSize.Contents;
          sbpTime.ToolTipText = "The current time";
   
          sb.Panels.AddRange(new StatusBarPanel[] 
                                        { sbpMenu, sbpDate, sbpTime });
   
          Timer timer = new Timer();
          timer.Tick += new EventHandler(TimerOnTick);
          timer.Interval = 1000;
          timer.Start();
     }
     void TimerOnTick(object obj, EventArgs ea)
     {
          DateTime dt = DateTime.Now;
   
          sbpDate.Text = dt.ToShortDateString();
          sbpTime.Text = dt.ToShortTimeString();
     }
}

Form with StatusBar

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormWithStatusBar : 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.ruponentModel.Container components;
  public FormWithStatusBar()
  {
    InitializeComponent();
    timer1.Interval = 1000;
    timer1.Enabled = true;
    timer1.Tick += new EventHandler(timer1_Tick);
    this.MenuComplete += new EventHandler(StatusForm_MenuDone);
    BuildMenuSystem();
    BuildStatBar();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.ruponents = new System.ruponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormWithStatusBar());
  }
  private void FileExit_Clicked(object sender, EventArgs e) 
  {    
    this.Close();
  }
    
  private void HelpAbout_Clicked(object sender, EventArgs e) 
  {
    sbPnlPrompt.Text = "Help";
  }
    
  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";
  }
  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()
  {
    statusBar.ShowPanels = true;
    statusBar.Panels.AddRange(new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
    
    sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
    sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
    sbPnlPrompt.Width = 62;
    sbPnlPrompt.Text = "Ready";
    
    sbPnlTime.Alignment = HorizontalAlignment.Right;
    sbPnlTime.Width = 76;
    this.Controls.Add(statusBar);  
  }
}

Simple Status Bar with Panel

using System;
using System.Drawing;
using System.Windows.Forms;
   
class SimpleStatusBarWithPanel: Form
{
     public static void Main()
     {
          Application.Run(new SimpleStatusBarWithPanel());
     }
     public SimpleStatusBarWithPanel()
     {
          Panel panel = new Panel();
          panel.Parent = this;
          panel.BackColor = SystemColors.Window;
          panel.ForeColor = SystemColors.WindowText;
          panel.AutoScroll = true;
          panel.Dock = DockStyle.Fill;
          panel.BorderStyle = BorderStyle.Fixed3D;
   
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.Text = "My initial status bar text";
   
          Label label = new Label();
          label.Parent = panel;
          label.Text = "Upper left";
          label.Location = new Point(0, 0);
   
          label = new Label();
          label.Parent = panel;
          label.Text = "Lower right";
          label.Location = new Point(250, 250);
          label.AutoSize = true;
     }
}

Status Bar and Auto-Scroll

using System;
using System.Drawing;
using System.Windows.Forms;
   
class StatusBarAndAutoScroll: Form
{
     public static void Main()
     {
          Application.Run(new StatusBarAndAutoScroll());
     }
     public StatusBarAndAutoScroll()
     {
          AutoScroll = true;
   
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.Text = "My initial status bar text";
   
          Label label = new Label();
          label.Parent = this;
          label.Text = "Upper left";
          label.Location = new Point(0, 0);
   
          label = new Label();
          label.Parent = this;
          label.Text = "Lower right";
          label.Location = new Point(250, 250);
          label.AutoSize = true;
     }
}