工具栏toolBar演示程序

.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyTools
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  p rivate System.Windows.Forms.ImageList imageList1;
  p rivate System.Windows.Forms.ToolBar toolBar1;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton1;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton2;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton3;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton4;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton5;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton6;
  p rivate System.Windows.Forms.ToolBarButton toolBarButton7;
  p rivate System.Windows.Forms.ContextMenu contextMenu1;
  p rivate System.Windows.Forms.MenuItem menuItem1;
  p rivate System.Windows.Forms.MenuItem menuItem2;
  p rivate System.Windows.Forms.MenuItem menuItem3;
  p rivate System.Windows.Forms.OpenFileDialog openFileDialog1;
  p rivate System.Windows.Forms.RichTextBox richTextBox1;
  p rivate System.Windows.Forms.SaveFileDialog saveFileDialog1;
  p rivate System.ComponentModel.IContainer components;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  p rivate void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   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.contextMenu1 = new System.Windows.Forms.ContextMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.SuspendLayout();
   //
   // imageList1
   //
   this.imageList1.ImageSize = new System.Drawing.Size(16, 15);
   this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
   this.imageList1.TransparentColor = System.Drawing.Color.White;
   //
   // toolBar1
   //
   this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                      this.toolBarButton1,
                      this.toolBarButton2,
                      this.toolBarButton3,
                      this.toolBarButton4,
                      this.toolBarButton5,
                      this.toolBarButton6,
                      this.toolBarButton7});
   this.toolBar1.DropDownArrows = true;
   this.toolBar1.ImageList = this.imageList1;
   this.toolBar1.Location = new System.Drawing.Point(0, 0);
   this.toolBar1.Name = "toolBar1";
   this.toolBar1.ShowToolTips = true;
   this.toolBar1.Size = new System.Drawing.Size(344, 42);
   this.toolBar1.TabIndex = 0;
   this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
   //
   // toolBarButton1
   //
   this.toolBarButton1.Text = "新建";
   this.toolBarButton1.ToolTipText = "新建一个文档";
   //
   // toolBarButton2
   //
   this.toolBarButton2.Text = "打开";
   this.toolBarButton2.ToolTipText = "打开文件";
   //
   // toolBarButton3
   //
   this.toolBarButton3.DropDownMenu = this.contextMenu1;
   this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
   this.toolBarButton3.Text = "保存";
   this.toolBarButton3.ToolTipText = "保存文件";
   //
   // contextMenu1
   //
   this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem1,
                       this.menuItem2,
                       this.menuItem3});
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.Text = "文本格式文件";
   this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
   //
   // menuItem2
   //
   this.menuItem2.Index = 1;
   this.menuItem2.Text = "-";
   //
   // menuItem3
   //
   this.menuItem3.Index = 2;
   this.menuItem3.Text = "RTF格式文件";
   this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // toolBarButton4
   //
   this.toolBarButton4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
   //
   // toolBarButton5
   //
   this.toolBarButton5.Text = "剪切";
   this.toolBarButton5.ToolTipText = "剪切文本";
   //
   // toolBarButton6
   //
   this.toolBarButton6.Text = "复制";
   this.toolBarButton6.ToolTipText = "复制文本";
   //
   // toolBarButton7
   //
   this.toolBarButton7.Text = "粘贴";
   this.toolBarButton7.ToolTipText = "粘贴文本";
   //
   // openFileDialog1
   //
   this.openFileDialog1.Filter = "文本文件(*.txt)|*.txt|RTF格式文件(*.rtf)|*.rtf";
   //
   // richTextBox1
   //
   this.richTextBox1.AutoSize = true;
   this.richTextBox1.AutoWordSelection = true;
   this.richTextBox1.ContextMenu = this.contextMenu1;
   this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.richTextBox1.Location = new System.Drawing.Point(0, 42);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(344, 148);
   this.richTextBox1.TabIndex = 1;
   this.richTextBox1.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(344, 190);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.toolBar1);
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "演示如何使用工具栏";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  ******* void Form1_Load(object sender, System.EventArgs e)
  {//加载工具栏按钮图象
   this.toolBar1.ImageList=this.imageList1;
   this.toolBarButton1.ImageIndex=0;
   this.toolBarButton2.ImageIndex=1;
   this.toolBarButton3.ImageIndex=2;
   this.toolBarButton5.ImageIndex=3;
   this.toolBarButton6.ImageIndex=4;
   this.toolBarButton7.ImageIndex=5;   
  }

  p rivate void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  {//响应工具栏按钮单击事件
   if(e.Button==this.toolBarButton1)
   {//新建文件
      this.richTextBox1.Text="";
   }
   if(e.Button==this.toolBarButton2)
   {//打开文件
    this.openFileDialog1.ShowDialog();
    string StrFileName=this.openFileDialog1.FileName;
    if(StrFileName.Trim()=="")
       return;
    try
    {
     this.richTextBox1.LoadFile(StrFileName,RichTextBoxStreamType.PlainText);
    }
    catch(Exception Error)
    {
     MessageBox.Show("文件格式无效,可先新建一个文档,保存后再打开测试!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
   }  
   if(e.Button==this.toolBarButton5)
   {//剪切文本
    this.richTextBox1.Cut();
   }
   if(e.Button==this.toolBarButton6)
   {//复制文本
    this.richTextBox1.Copy();
   }
   if(e.Button==this.toolBarButton7)
   {//粘贴文本
    this.richTextBox1.Paste();
   }   
  }

  p rivate void menuItem1_Click(object sender, System.EventArgs e)
  {//以文本文件格式保存文件
   this.saveFileDialog1.Filter="文本格式文件(*.txt)|*.txt";
   this.saveFileDialog1.ShowDialog();
   string StrFileName=this.saveFileDialog1.FileName;
   if(StrFileName.Trim()=="")
    return;
   this.richTextBox1.SaveFile(StrFileName,RichTextBoxStreamType.PlainText);
  
  }
  p rivate void menuItem3_Click(object sender, System.EventArgs e)
  {//以RTF文件格式保存文件
   this.saveFileDialog1.Filter="RTF格式文件(*.rtf)|*.rtf";
   this.saveFileDialog1.ShowDialog();
   string StrFileName=this.saveFileDialog1.FileName;
   if(StrFileName.Trim()=="")
    return;
   this.richTextBox1.SaveFile(StrFileName,RichTextBoxStreamType.RichText);    
  }
 
 }
}

posted @ 2010-02-15 11:33  猪悟能  阅读(363)  评论(0编辑  收藏  举报