小的文本编辑器

类别:.NET开发 点击:0 评论:0 推荐:

http://true5460.muicc.com/program/textEdit.zip
--------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button6;
  private System.Windows.Forms.Button button7;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  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>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.label2 = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
   this.button7 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.label1.Location = new System.Drawing.Point(24, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "文件名";
   //
   // textBox1
   //
   this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.textBox1.Location = new System.Drawing.Point(24, 32);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(304, 21);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "";
   //
   // textBox2
   //
   this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.textBox2.Location = new System.Drawing.Point(24, 85);
   this.textBox2.Multiline = true;
   this.textBox2.Name = "textBox2";
   this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.textBox2.Size = new System.Drawing.Size(408, 283);
   this.textBox2.TabIndex = 2;
   this.textBox2.Text = "";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(21, 64);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(56, 16);
   this.label2.TabIndex = 3;
   this.label2.Text = "文件内容";
   //
   // button1
   //
   this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button1.Location = new System.Drawing.Point(504, 80);
   this.button1.Name = "button1";
   this.button1.TabIndex = 4;
   this.button1.Text = "创建";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button2.Location = new System.Drawing.Point(504, 136);
   this.button2.Name = "button2";
   this.button2.TabIndex = 5;
   this.button2.Text = "读取";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button3.Location = new System.Drawing.Point(504, 192);
   this.button3.Name = "button3";
   this.button3.TabIndex = 6;
   this.button3.Text = "追加";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button4.Location = new System.Drawing.Point(504, 360);
   this.button4.Name = "button4";
   this.button4.TabIndex = 7;
   this.button4.Text = "退出";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button5.Location = new System.Drawing.Point(504, 248);
   this.button5.Name = "button5";
   this.button5.TabIndex = 8;
   this.button5.Text = "清除";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // button6
   //
   this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button6.Location = new System.Drawing.Point(344, 32);
   this.button6.Name = "button6";
   this.button6.Size = new System.Drawing.Size(64, 23);
   this.button6.TabIndex = 9;
   this.button6.Text = "浏览";
   this.button6.Click += new System.EventHandler(this.button6_Click_1);
   //
   // button7
   //
   this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.button7.Location = new System.Drawing.Point(504, 304);
   this.button7.Name = "button7";
   this.button7.TabIndex = 10;
   this.button7.Text = "复制";
   this.button7.Click += new System.EventHandler(this.button7_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(640, 430);
   this.Controls.Add(this.button7);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "文本编辑器_swl";
   this.ResumeLayout(false);

  }
  #endregion

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

  private void button3_Click(object sender, System.EventArgs e)
  {
   FileStream file;
   StreamWriter file_in;
   String filename=textBox1.Text;
   if(filename=="") MessageBox.Show("\n     文件名空,请输入文件名!      ","错误操作");
   else
   {
    try 
    {
     file=new FileStream(filename,FileMode.Append);
     file_in=new StreamWriter(file,Encoding.Default);
     file_in.Write(textBox2.Text);
     file_in.Close();
     file.Close();
     MessageBox.Show("     操作成功!     ","成功");
    }
    catch 
    {
     MessageBox.Show("\n     错误操作!      ","错误操作");
    }
   }
     }

  private void button1_Click(object sender, System.EventArgs e)
  {
   FileStream file;
   StreamWriter file_in;
   String filename=textBox1.Text;
   if(filename=="") MessageBox.Show("\n     文件名空,请输入文件名!      ","错误操作");
   else
   {
    try 
    {
     file=new FileStream(filename,FileMode.Create);
     file_in=new StreamWriter(file,Encoding.Default);
     file_in.Write(textBox2.Text);
     file_in.Close();
     file.Close();
     MessageBox.Show("     操作成功!     ","成功!");
    }
    catch 
    {
     MessageBox.Show("\n     错误操作!      ","错误操作");
    }
   }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   FileStream file;
   StreamReader file_out;
   String content,filename=textBox1.Text;
   textBox2.Text="";
   if(filename=="") MessageBox.Show("\n     文件名空,请输入文件名!      ","错误操作");
   else
   {
    try 
    {
     file=new FileStream(filename,FileMode.Open);
     file_out=new StreamReader(file,Encoding.Default);
     while((content=file_out.ReadLine())!=null) 
     {
      textBox2.Text+=content+"\r\n";
     }
     file_out.Close();
     file.Close();
     
     MessageBox.Show("     操作成功!     ","成功");
    }
    catch 
    {
     MessageBox.Show("\n     错误操作!      ","错误操作");
    }
   }
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   Application.Exit();
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   textBox2.Clear();
   
  }

  private void button6_Click_1(object sender, System.EventArgs e)
  {
   OpenFileDialog openFileDialog1 = new OpenFileDialog();
   openFileDialog1.InitialDirectory = "" ;
   openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
   openFileDialog1.FilterIndex = 2 ;
   openFileDialog1.RestoreDirectory = false ;

   if(openFileDialog1.ShowDialog() == DialogResult.OK)
   {
    textBox1.Text=openFileDialog1.FileName;
   }

  }

  private void button7_Click(object sender, System.EventArgs e)
  {
   if(textBox2.Text=="") MessageBox.Show("     文本框内无信息!     ","警告");
   else
   { 
    textBox2.SelectAll();
    textBox2.Copy();
    MessageBox.Show("     已将文本制到剪贴板!     ","成功");
   }
  }


 }
}

本文地址:http://com.8s8s.com/it/it43043.htm