当前位置: 首页>>代码示例>>C#>>正文


C# ToolBarButton类代码示例

本文整理汇总了C#中System.Windows.Forms.ToolBarButton的典型用法代码示例。如果您正苦于以下问题:C# ToolBarButton类的具体用法?C# ToolBarButton怎么用?C# ToolBarButton使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ToolBarButton类属于System.Windows.Forms命名空间,在下文中一共展示了ToolBarButton类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeMyToolBar

public void InitializeMyToolBar()
 {
    // Create and initialize the ToolBar and ToolBarButton controls.
    toolBar1 = new ToolBar();
    ToolBarButton toolBarButton1 = new ToolBarButton();
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = new ToolBarButton();
 
    // Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open";
    toolBarButton2.Text = "Save";
    toolBarButton3.Text = "Print";
 
    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
    
    // Add the event-handler delegate.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
       this.toolBar1_ButtonClick);
    
    // Add the ToolBar to the Form.
    Controls.Add(toolBar1);
 }
 
 private void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
 {
   // Evaluate the Button property to determine which button was clicked.
   switch(toolBar1.Buttons.IndexOf(e.Button))
   {
      case 0:
         openFileDialog1.ShowDialog();
         // Insert code to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert code to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert code to print the file.    
         break; 
    }
 }
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:47,代码来源:ToolBarButton

示例2: new ToolBarButton

//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class ToolBarButtonAction : System.Windows.Forms.Form
{
  private System.Windows.Forms.ToolBar toolBar1;
  private System.Windows.Forms.ToolBarButton toolBarButton1;
  private System.Windows.Forms.ToolBarButton toolBarButton2;
  private System.Windows.Forms.ToolBarButton toolBarButton3;
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem Open;
  private System.Windows.Forms.MenuItem Test;
  private System.Windows.Forms.MenuItem Exit;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.TextBox textBox1;
  private System.ComponentModel.IContainer components;

  public ToolBarButtonAction()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.components = new System.ComponentModel.Container();
    this.toolBar1 = new System.Windows.Forms.ToolBar();
    this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.Open = new System.Windows.Forms.MenuItem();
    this.Test = new System.Windows.Forms.MenuItem();
    this.Exit = new System.Windows.Forms.MenuItem();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // toolBar1
    // 
    this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                          this.toolBarButton1,
                                          this.toolBarButton2,
                                          this.toolBarButton3});
    this.toolBar1.DropDownArrows = true;
    this.toolBar1.ImageList = this.imageList1;
    this.toolBar1.Name = "toolBar1";
    this.toolBar1.ShowToolTips = true;
    this.toolBar1.Size = new System.Drawing.Size(292, 39);
    this.toolBar1.TabIndex = 0;
    this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
    // 
    // toolBarButton1
    // 
    this.toolBarButton1.ImageIndex = 0;
    this.toolBarButton1.Text = "Open";
    this.toolBarButton1.ToolTipText = "Opens a file";
    // 
    // toolBarButton2
    // 
    this.toolBarButton2.ImageIndex = 1;
    this.toolBarButton2.Text = "Test";
    this.toolBarButton2.ToolTipText = "Test";
    // 
    // toolBarButton3
    // 
    this.toolBarButton3.ImageIndex = 2;
    this.toolBarButton3.Text = "Exit";
    this.toolBarButton3.ToolTipText = "Close Program";

    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.menuItem1});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.Open,
                                          this.Test,
                                          this.Exit});
    this.menuItem1.Text = "Main Menu Options";
    // 
    // Open
    // 
    this.Open.Index = 0;
    this.Open.Text = "Open";
    this.Open.Click += new System.EventHandler(this.OpenMenuItemClick);
    // 
    // Test
    // 
    this.Test.Index = 1;
    this.Test.Text = "Test";
    this.Test.Click += new System.EventHandler(this.TestMenuItemClick);
    // 
    // Exit
    // 
    this.Exit.Index = 2;
    this.Exit.Text = "Exit";
    this.Exit.Click += new System.EventHandler(this.ExitMenuItemClick);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(8, 64);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(128, 32);
    this.button1.TabIndex = 1;
    this.button1.Text = "Open File Dialog";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(160, 72);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(120, 32);
    this.button2.TabIndex = 2;
    this.button2.Text = "FontDialog";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(40, 120);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(136, 32);
    this.button3.TabIndex = 3;
    this.button3.Text = "ColorDialog";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(88, 184);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(168, 20);
    this.textBox1.TabIndex = 4;
    this.textBox1.Text = "textBox1";
    // 
    // ToolBarButtonAction
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.textBox1,
                                    this.button3,
                                    this.button2,
                                    this.button1,
                                    this.toolBar1});
    this.Menu = this.mainMenu1;
    this.Name = "ToolBarButtonAction";
    this.Text = "ToolBarButtonAction";
    this.Load += new System.EventHandler(this.ToolBarButtonAction_Load);
    this.ResumeLayout(false);

  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new ToolBarButtonAction());
  }

  private void ToolBarButtonAction_Load(object sender, System.EventArgs e)
  {
  
  }

  private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  {
    if ( e.Button == toolBarButton1 )
    {
      MessageBox.Show( "Open Button Clicked ");
    }
    if ( e.Button == toolBarButton2 )
    {
      MessageBox.Show( "Test Button Clicked ");
    }
    if ( e.Button == toolBarButton3 )
    {
      MessageBox.Show( "Exit Button Clicked ");
    } 

  }

  private void TestMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Test Menu ItemClicked ");
  }

  private void OpenMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Open Menu ItemClicked ");
  }

  private void ExitMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Exit Menu ItemClicked ");
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
    OpenFileDialog fdlg = new OpenFileDialog(); 
    fdlg.Title = "C# Corner Open File Dialog" ; 
    fdlg.InitialDirectory = @"c:\" ; 
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ; 
    fdlg.FilterIndex = 2 ; 
    fdlg.RestoreDirectory = true ; 
    if(fdlg.ShowDialog() == DialogResult.OK) 
    { 
      textBox1.Text = fdlg.FileName ; 
    }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
    FontDialog fntDlg = new FontDialog(); 
    fntDlg.ShowColor = true; 
    if(fntDlg.ShowDialog() != DialogResult.Cancel ) 
    { 
      textBox1.Font = fntDlg.Font ; 
      textBox1.ForeColor = fntDlg.Color; 
    }
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
    ColorDialog colorDlg = new ColorDialog();
    colorDlg.ShowDialog();
    textBox1.BackColor = colorDlg.Color;
    button1.BackColor = colorDlg.Color;
    button3.BackColor = colorDlg.Color;

  }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:256,代码来源:ToolBarButton


注:本文中的System.Windows.Forms.ToolBarButton类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。