C#实现简单记事本程序

本文实例为大家分享了使用C#写出一个简单的记事本程序,供大家参考,具体内容如下

编程语言: C#
编程环境: Visual Studio 2013
运行环境: .NET Framework 4.5

预览:

功能:

标题栏
显示文件标题

菜单栏
各类菜单命令

文件- 新建
- 打开
- 保存
- 另存为
- 页面设置
- 打印
- 退出

编辑
- 撤销
- 剪切
- 复制
- 粘贴
- 全选
- 时间/日期

格式
- 自动换行
- 字体

视图
- 状态栏
- 工具栏
- 全屏模式

帮助
- 开源许可
- 查看帮助
- 关于

工具栏
常用工具集合

标签栏
文件标签显示

工作区
编辑区

状态栏
显示文件状态

文本状态(新建/已修改)

  • 字符个数
  • 光标坐标
  • 功能实现

本程序有两个窗体,分别为Form1和AboutBox1

所有文件如下:

对于Form1:
需要引入的类库:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

窗体及空间声明代码:

private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打开ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 另存为ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 编辑ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem;
private System.Windows.Forms.TextBox editBox1;
private System.Windows.Forms.ToolStripMenuItem 撤销ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 剪切ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 复制ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 粘贴ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 全选AToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 日期DToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 字体ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查看VToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 状态栏ToolStripMenuItem;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem4;
private System.Windows.Forms.ToolStripMenuItem 页面设置UToolStripMenuItem;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Windows.Forms.ToolStripMenuItem 打印PToolStripMenuItem;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.ToolStripMenuItem 查看帮助HToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem5;
private System.Windows.Forms.ToolStripMenuItem 自动换行ToolStripMenuItem;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
public System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3;
private System.Windows.Forms.ToolStripMenuItem 全屏模式ToolStripMenuItem;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton newButton;
private System.Windows.Forms.ToolStripButton openButton;
private System.Windows.Forms.ToolStripButton saveButton;
private System.Windows.Forms.ToolStripButton saveAsButton;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripButton cutButton;
private System.Windows.Forms.ToolStripButton copyButton;
private System.Windows.Forms.ToolStripButton pasteButton;
private System.Windows.Forms.ToolStripButton deleteButton;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripButton timeButton;
private System.Windows.Forms.ToolStripButton fullButton;
private System.Windows.Forms.ToolStripButton textButton;
private System.Windows.Forms.ToolStripMenuItem 工具栏ToolStripMenuItem;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel4;
private System.Windows.Forms.ToolStripMenuItem 开源许可OToolStripMenuItem;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TextBox editBox2;1

窗体载入:

private void Form1_Load(object sender, EventArgs e)
  {
   this.editBox1.ScrollBars = ScrollBars.Both;
   this.editBox2.ScrollBars = ScrollBars.Both;
   this.Text = textFileName + " - " + programeName;//显示文件名
   this.timer1.Start();
   editBox1.Focus();

  }

首先进行一些固定变量声明:

private string textFileName = "无标题";
private string programeName = "Icey";
private string filePath = "";
private string asFilePath = "";
private string selecteText = "";
private string helpUrl = "https://blog.mayuko.cn/icey";
private string openSourceUrl = "https://github.com/mayuko2012/icey";
private string wrongMessage = "你好像遇到了错误...";
private string fileFormat = "文本文件(*.txt)|*.txt|Icey文件(*.ice)|*.ice|C++文件(*.cpp)|*.cpp|C文件(*.c)|*.c|所有文件(*.*)|(*.*)";
private string tabFileName1 = "无标题1 - Icey";
private string tabFileName2 = "无标题2 - Icey";
  Boolean saveFileStatus1 = false;
  Boolean textChanged1 = false;
  Boolean saveFileStatus2 = false;
  Boolean textChanged2 = false;

保存文件采用函数形式:

private void saveFile()//保存
  {
   if (!textFileName.Equals(""))
   {
    SaveFileDialog saveFile = new SaveFileDialog();
    saveFile.Filter = fileFormat;
    saveFile.FileName = "*.txt";
    if (saveFile.ShowDialog() == DialogResult.OK)
    {
     filePath = saveFile.FileName;
     StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default);
     sw.Write((editBox1.Focused) ? editBox1.Text : editBox2.Text);
     sw.Close();
     if (editBox1.Focused)
     {
      tabFileName1 = saveFile.FileName + " - " + programeName;
      saveFileStatus1 = true;
     }
     else if (editBox2.Focused)
     {
      tabFileName2 = saveFile.FileName + " - " + programeName;
      saveFileStatus2 = true;
     }

    }
   }
   toolStripStatusLabel4.Text = "已保存";
  }

另存为采用函数形式:

private void saveAsFile()//另存为
  {
   SaveFileDialog saveAsFile = new SaveFileDialog();
   saveAsFile.Filter = fileFormat;
   saveAsFile.FileName = "*.txt";
   if (saveAsFile.ShowDialog() == DialogResult.OK)
   {
    asFilePath = saveAsFile.FileName;
    StreamWriter sw = new StreamWriter(asFilePath, false, Encoding.Default);
    sw.WriteLine((editBox1.Focused) ? editBox1.Text : editBox2.Text);
    sw.Close();
    FileInfo fileInfo = new FileInfo(saveAsFile.FileName);
    textFileName = fileInfo.Name;
   }
   toolStripStatusLabel4.Text = "已保存";
}

新建函数:

 private void newFile()//新建
  {
   if (editBox1.Focused)
   {

    if (editBox1.Text != String.Empty && saveFileStatus1 == false && textChanged1 == true)//如果文本框不为空
    {
     DialogResult result = MessageBox.Show("是否将窗口1已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
     if (result == DialogResult.Yes)
     {
      saveFile();
      Application.Exit();
     }
     else if (result == DialogResult.No)
     {
      editBox1.Text = "";
     }
    }
    else
     editBox1.Text = "";
   }
   else if (editBox2.Focused)
   {
    if (editBox2.Text != String.Empty && saveFileStatus2 == false && textChanged2 == true)//如果文本框不为空
    {
     DialogResult result = MessageBox.Show("是否将窗口2已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
     if (result == DialogResult.Yes)
     {
      saveFile();
      Application.Exit();
     }
     else if (result == DialogResult.No)
     {
      editBox2.Text = "";
     }
    }
    else
     editBox2.Text = "";
   }
 }

打开文件函数:

private void openFile()//打开
   {
    OpenFileDialog openFile = new OpenFileDialog();
    openFile.Filter = fileFormat;
    if (openFile.ShowDialog() == DialogResult.OK)
    {
     StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default);
     if (editBox1.Focused)
     {
      editBox1.Text = sr.ReadToEnd();
     }
     else if(editBox2.Focused)
     {
      editBox2.Text = sr.ReadToEnd();
     }
     sr.Close();
     FileInfo fileInfo = new FileInfo(openFile.FileName);
     if (editBox1.Focused)
     {
      tabFileName1 = fileInfo.Name + " - " + programeName;
      saveFileStatus1 = true;
     }
     else if (editBox2.Focused)
     {
      tabFileName2 = fileInfo.Name + " - " + programeName;
      saveFileStatus2 = true;
     }
     textFileName = fileInfo.Name;
    }
}

全屏模式函数:

private void fullScreen()//全屏
  {
   if (全屏模式ToolStripMenuItem.Checked == false)
   {
    this.WindowState = FormWindowState.Maximized;
    全屏模式ToolStripMenuItem.Checked = true;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   }
   else
   {
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
    this.WindowState = FormWindowState.Normal;
    全屏模式ToolStripMenuItem.Checked = false;
   }
}

退出菜单命令:

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Text != String.Empty || saveFileStatus1 == false && textChanged1 == true)
   {
    this.tabPage1.Show();
    this.editBox1.Focus();
    DialogResult result = MessageBox.Show("是否将窗口1已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (result == DialogResult.Yes)
    {
     saveFile();
     Application.Exit();
    }
    else if (result == DialogResult.No)
    {
     Application.Exit();
    }
   }
   else if (editBox2.Text != String.Empty || saveFileStatus2 == false && textChanged2 == true)
   {
    this.tabPage2.Show();
    this.editBox2.Focus();
    DialogResult result = MessageBox.Show("是否将窗口2已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (result == DialogResult.Yes)
    {
     saveFile();
     Application.Exit();
    }
    else if (result == DialogResult.No)
    {
     Application.Exit();
    }
   }
   else
    Application.Exit();

}

Bool变量,用于判断TextBox是否发生变化:

private void textBox1_TextChanged(object sender, EventArgs e)
  {
   textChanged2 = true;
   toolStripStatusLabel4.Text = "已修改";
  }
  private void editBox2_TextChanged(object sender, EventArgs e)
  {
   textChanged2 = true;
   toolStripStatusLabel4.Text = "已修改";
  }

新建菜单命令:

private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   newFile();
  }

打开菜单命令:

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   openFile();
  }

字体菜单命令:

private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   FontDialog fontDialog = new FontDialog();
   if (fontDialog.ShowDialog() == DialogResult.OK)
   {
    if (editBox1.Focused)
    {
     editBox1.Font = fontDialog.Font;
    }
    else
     editBox2.Font = fontDialog.Font;
   }
  }

退出动作(当用户点击窗体右上角退出按钮时执行此操作):

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
   if (editBox1.Text != String.Empty && e.CloseReason == CloseReason.UserClosing || saveFileStatus1 == false && textChanged1 == true)//如果文本框不为空&&触发关闭按钮事件
   {
    this.tabPage1.Show();
    this.editBox1.Focus();
    DialogResult result = MessageBox.Show("是否将窗体1已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (result == DialogResult.Yes)
    {
     saveFile();
     e.Cancel = false;
    }
    else if (result == DialogResult.No)
    {
     e.Cancel = false;
    }
    else if (result == DialogResult.Cancel)
    {
     e.Cancel = true;
    }
   }
   else if (editBox2.Text != String.Empty && e.CloseReason == CloseReason.UserClosing || saveFileStatus2 == false && textChanged2 == true)//如果文本框不为空&&触发关闭按钮事件
   {
    this.tabPage2.Show();
    this.editBox2.Focus();
    DialogResult result = MessageBox.Show("是否将窗口2已编辑文件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (result == DialogResult.Yes)
    {
     saveFile();
     e.Cancel = false;
    }
    else if (result == DialogResult.No)
    {
     e.Cancel = false;
    }
    else if (result == DialogResult.Cancel)
    {
     e.Cancel = true;
    }
   }
   else
    Application.Exit();
  }

状态栏命令(状态栏是否显示):

private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   if (状态栏ToolStripMenuItem.Checked == true)
   {
    状态栏ToolStripMenuItem.Checked = false;
    statusStrip1.Visible = false;
   }
   else
   {
    状态栏ToolStripMenuItem.Checked = true;
    statusStrip1.Visible = true;
   }
}

编辑命令:

private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if ((editBox1.SelectedText.Equals("")))
   {
    剪切ToolStripMenuItem.Enabled = false;
    复制ToolStripMenuItem.Enabled = false;
    删除ToolStripMenuItem.Enabled = false;
   }
   else
   {
    剪切ToolStripMenuItem.Enabled = true;
    复制ToolStripMenuItem.Enabled = true;
    删除ToolStripMenuItem.Enabled = true;
   }

  }

全选命令:

private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.SelectAll();
   }
   else
    this.editBox2.SelectAll();
  }

剪切 复制 粘贴 删除命令:

private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    selecteText = editBox1.SelectedText;
    this.editBox1.Cut();
   }
   else
   {
    selecteText = editBox2.SelectedText;
    this.editBox2.Cut();
   }
  }

  private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Undo();
   }
   else
    this.editBox2.Undo();
  }

  private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Copy();
   }
   else
    this.editBox2.Copy();
  }

  private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Paste();
   }
   else
    this.editBox2.Paste();
  }

  private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.SelectedText = "";
   }
   else
    this.editBox2.SelectedText = "";
  }

保存命令:

private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   saveFile();
  }

另存为命令:

private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   saveAsFile();
  }

时间戳命令:

private void 日期DToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    editBox1.AppendText(System.DateTime.Now.ToString());
   }
   else
    editBox2.AppendText(System.DateTime.Now.ToString());
  }

页面设置命令:

private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
  {
   pageSetupDialog1.Document = printDocument1;
   this.pageSetupDialog1.AllowMargins = true;
   this.pageSetupDialog1.AllowOrientation = true;
   this.pageSetupDialog1.AllowPaper = true;
   this.pageSetupDialog1.AllowPrinter = true;
   this.pageSetupDialog1.Document = this.printDocument1;
   pageSetupDialog1.ShowDialog();
  }

打印命令:

private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  {
   this.printDialog1.Document = this.printDocument1;
   this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;
   if (this.printDialog1.ShowDialog() == DialogResult.OK)
   {
    try
    {
     this.printDocument1.Print();
    }
    catch (Exception ex)
    {
     MessageBox.Show(ex.Message, wrongMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
   }
  }

查看帮助 关于命令:

private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e)
  {
   System.Diagnostics.Process.Start(helpUrl);
  }

  private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   AboutBox1 about = new AboutBox1();
   about.StartPosition = FormStartPosition.CenterScreen;
   about.Show();
   about.Owner = this;
   //timer1.Stop();
  }

自动换行命令:

private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (自动换行ToolStripMenuItem.Checked == true)
   {
    editBox1.WordWrap = false;
    editBox2.WordWrap = false;
    自动换行ToolStripMenuItem.Checked = false;
   }
   else
   {
    editBox1.WordWrap = true;
    editBox2.WordWrap = true;
    自动换行ToolStripMenuItem.Checked = true;
   }
  }

计时器(100 ms刷新频率):

 private void timer1_Tick(object sender, EventArgs e)
  {
   toolStripStatusLabel1.Text = (editBox1.Focused) ? editBox1.Text.Length.ToString() + " 个字符" : editBox2.Text.Length.ToString() + " 个字符";
   int totalline = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(editBox1.Text.Length) + 1 : editBox2.GetLineFromCharIndex(editBox2.Text.Length) + 1;//得到总行数
   int index = (editBox1.Focused) ? editBox1.GetFirstCharIndexOfCurrentLine() : editBox2.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引
   int line = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(index) + 1 : editBox2.GetLineFromCharIndex(index) + 1;//得到当前行的行号
   int col = (editBox1.Focused) ? editBox1.SelectionStart - index + 1 : editBox2.SelectionStart - index + 1;//.SelectionStart得到光标所在位置的索引 - 当前行第一个字符的索引 = 光标所在的列数
   toolStripStatusLabel2.Text = "第" + line + "行,第" + col + "列";
   if (( (editBox1.Focused) ? editBox1.SelectedText.Equals(""):editBox2.SelectedText.Equals("")))
   {
    cutButton.Enabled = false;
    copyButton.Enabled = false;
    deleteButton.Enabled = false;
   }
   else
   {
    cutButton.Enabled = true;
    copyButton.Enabled = true;
    deleteButton.Enabled = true;
   }
   if (editBox1.Focused)
   {
    editBox1.Focus();
    this.Text = tabFileName1;
   }
   else
   {
    editBox2.Focus();
    this.Text = tabFileName2;
   }
   if (editBox2.Focused)
   {
    editBox2.Focus();
    this.Text = tabFileName2;
   }
   else
   {
    editBox1.Focus();
    this.Text = tabFileName1;
   }
  }

工具栏命令(工具栏是否显示):

private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (工具栏ToolStripMenuItem.Checked == false)
   {
    toolStrip1.Visible = true;
    工具栏ToolStripMenuItem.Checked = true;
   }
   else if(工具栏ToolStripMenuItem.Checked == true)
   {
    toolStrip1.Visible = false;
    工具栏ToolStripMenuItem.Checked = false;
   }
  }

开源许可命令:

private void 开源许可OToolStripMenuItem_Click(object sender, EventArgs e)
  {
   System.Diagnostics.Process.Start(openSourceUrl);
  }

工具栏的各个按钮:

private void newButton_Click(object sender, EventArgs e)
  {
   newFile();
  }

  private void openButton_Click(object sender, EventArgs e)
  {
   openFile();
  }

  private void saveButton_Click(object sender, EventArgs e)
  {
   saveFile();
  }

  private void saveAsButton_Click(object sender, EventArgs e)
  {
   saveAsFile();
  }

  private void cutButton_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    selecteText = editBox1.SelectedText;
    this.editBox1.Cut();
   }
   else
   {
    selecteText = editBox2.SelectedText;
    this.editBox2.Cut();
   }
  }

  private void copyButton_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Copy();
   }
   else
    this.editBox2.Copy();
  }

  private void pasteButton_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Paste();
   }
   else
    this.editBox2.Paste();
  }

  private void deleteButton_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.SelectedText = "";
   }
   else
    this.editBox2.SelectedText = "";
  }

  private void timeButton_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    editBox1.AppendText(System.DateTime.Now.ToString());
   }
   else
    editBox2.AppendText(System.DateTime.Now.ToString());
  }

  private void textButton_Click(object sender, EventArgs e)
  {
   FontDialog fontDialog = new FontDialog();
   if (fontDialog.ShowDialog() == DialogResult.OK)
   {
    if (editBox1.Focused)
    {
     editBox1.Font = fontDialog.Font;
    }
    else
     editBox2.Font = fontDialog.Font;
   }
  }

  private void fullButton_Click(object sender, EventArgs e)
  {
   fullScreen();
}

标签栏:

private void tabPage1_Click(object sender, EventArgs e)
  {
   editBox1.Focus();
   this.Text = tabPage1.Text;
   if (textChanged1 == false)
   {
    toolStripStatusLabel4.Text = "新建";
   }
  }

  private void tabPage2_Click(object sender, EventArgs e)
  {
   editBox2.Focus();
   this.Text = tabPage2.Text;
   if (textChanged2 == false)
   {
    toolStripStatusLabel4.Text = "新建";
   }
  }

对于AboutBox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
 partial class AboutBox1 : Form
 {
  public AboutBox1()
  {
   InitializeComponent();
   this.Text = String.Format("关于 {0}", AssemblyTitle);
   this.labelProductName.Text = AssemblyProduct;
   this.labelVersion.Text = String.Format("版本 {0}", AssemblyVersion);
   this.labelCopyright.Text = AssemblyCopyright;
   this.labelCompanyName.Text = AssemblyCompany;
   this.textBoxDescription.Text = AssemblyDescription;
  }

  #region 程序集特性访问器

  public string AssemblyTitle
  {
   get
   {
    object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
    if (attributes.Length > 0)
    {
     AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
     if (titleAttribute.Title != "")
     {
      return titleAttribute.Title;
     }
    }
    return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
   }
  }

  public string AssemblyVersion
  {
   get
   {
    return Assembly.GetExecutingAssembly().GetName().Version.ToString();
   }
  }

  public string AssemblyDescription
  {
   get
   {
    object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
    if (attributes.Length == 0)
    {
     return "";
    }
    return ((AssemblyDescriptionAttribute)attributes[0]).Description;
   }
  }

  public string AssemblyProduct
  {
   get
   {
    object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
    if (attributes.Length == 0)
    {
     return "";
    }
    return ((AssemblyProductAttribute)attributes[0]).Product;
   }
  }

  public string AssemblyCopyright
  {
   get
   {
    object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
    if (attributes.Length == 0)
    {
     return "";
    }
    return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
   }
  }

  public string AssemblyCompany
  {
   get
   {
    object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
    if (attributes.Length == 0)
    {
     return "";
    }
    return ((AssemblyCompanyAttribute)attributes[0]).Company;
   }
  }
  #endregion

  private void AboutBox1_Load(object sender, EventArgs e)
  {

  }

  private void okButton_Click(object sender, EventArgs e)
  {
   MessageBox.Show("已是最新版本!", "检查更新");
  }
  private void AboutBox1_FormClosing(object sender, FormClosingEventArgs e)
  {
   Form1 frm1 = (Form1)this.Owner;
   frm1.timer1.Start();
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#用记事本编写简单WinForm窗体程序

    平时我们编写WinForm程序经常使用VS进行拖控件的方式,这样做虽然简单,但是无法深入了解WinForm程序的本质.其实,用记事本也可以编写出VS编写的WinForm程序.还是直接看代码吧: 1.打开记事本,写入以下代码,另存为hello.cs文件 using System; using System.Windows.Forms; namespace Hello { public class Form1:Form { private System.Windows.Forms.Button bt

  • C#读取Excel到DataTable的方法示例

    前提 在Windows下进行数据处理的时候最常见的情况莫过于读取Microsoft的Excel文件了,Excel的普及率惊人,是事实上的标准.以前的开发中我采用调用第三方类库 NPOI的方式来处理Excel.这个方式有两个缺点: 需要依赖第三方类库NPOI NPOI支持几乎全功能的Office条件,缺点就是复杂度也高. 如果只是简单的导入数据,完全可以有更加简单的方案,方案的限制条件为: 只支持Windows平台 只读取Excel文件 支持xls和xlsx文件格式 依赖 还是有依赖的 2007

  • 详解C#开发Android应用程序的流程

    手把手教你用C#开发Android应用程序的方法和流程摘要:用C#能开发RFID-android吗?C#真的能开发android程序吗?C#开发android程序的工具是什么?开发步骤.方法以及流程是怎样的?出学android开发者一定会提出这些疑问,本文一一解答这些疑问,为安卓初学者提供入门指引. Android系统一下子铺天盖地而来,让人目不暇接.兴奋的同时也让部分开发人员犯难了!要知道从熟知的Wince.Mobile开发语言C#跨越到RFID-Android的Java.可不是一朝一夕就能完

  • C#/.NET使用git命令行来操作git仓库的方法示例

    我们可以在命令行中操作 git,但是作为一名程序员,如果在大量重复的时候还手动敲命令行,那就太笨了. 本文介绍使用 C# 编写一个 .NET 程序来自动化地使用 git 命令行来操作 git 仓库. 这是一篇很基础的入门文章. 最简单的运行 git 命令的代码 在 .NET 中,运行一个命令只需要使用 Process.Start 开启一个子进程就好了.于是要运行一个 git 命令,我们其实只需要这句足以: Process.Start("git", "status")

  • C# WebApi Get请求方式传递实体参数的方法示例

    前言 我又搞回笃NET啦!java之路真是命运多舛,好事多磨.不过,也许我已经进入无招胜有招,博取众家之长.融会贯通的地步了. 对于WebApi,今天又有了一些新的了解. 话说,Get请求方式,参数会附在Url后面,称为QueryString,传递给服务器:而POST方式,则将参数放在消息体内.采用QueryString的话,简单,方便,但只适合参数比较少的情况:但有的时候,需要传递比较多.比较复杂的参数,比如,组合条件查询. 组合条件查询,条件会很多,通常会用一个实体类来封装,传递给服务器.用

  • C#实现简单文本编辑器

    本文实例为大家分享了C#实现简单文本编辑器的具体代码,供大家参考,具体内容如下 建立一个窗体文件,实现对文件的编辑保存和对txt文件的打开 界面设计: 程序源代码: //form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

  • C#中#define后面只加一个参数的解释

    #define只加一个参数 的解释 <stdio.h> 里有: #ifndef __STDIO_H #define __STDIO_H 这个__STDIO_H代表什么?而define的用法不是后面加两个字符串吗,它这里却只加一个字符串,是什么意思? 还有很多头文件里都有如下语句 #if __STDC__ #define _Cdecl #else #define _Cdecl cdecl #endif __stdc__,cdecl代表什么? 比方说你#include进来一个stdio.h,再#i

  • C#实现记事本查找与替换功能

    看了网上关于记事本的查找替换很多,但都没有达到我想要的结果,然后自己学习总结了以下的方法: 统计字符串(汉字,字母,数字,字符) 先上效果图 定义全局变量 #region =★*★*★= [查找替换]的自定义变量 =★*★*★= /// <summary> 获取或设置查找起始位置 </summary> static int Index = 0; /// <summary> 查找到的字符串数量 </summary> static int count = 0;

  • 详解C#中的session用法

    Session具有以下特点: (1)Session中的数据保存在服务器端: (2)Session中可以保存任意类型的数据: (2)Session默认的生命周期是20分钟,可以手动设置更长或更短的时间. 需要注意的是在Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重 存入字符串: Session["userName"] = "aaa"; 这样取值: string str = Session["userName"

  • C# 填充Excel图表、图例背景色的实例代码

    填充背景色,一般可以选择多种不同样式来填充背景,包括填充为纯色背景.渐变背景.图片背景或者纹理背景等.下面的内容将分别介绍通过C#来设置Excel中图表背景色.以及图表中的图例背景色的方法. 使用工具:Spire.XLS for .NET dll引用:下载安装后,注意在程序中添加引用Spire.Xls.dll(dll文件在安装路径下的bin文件夹中获取) [示例1]填充图表背景色 测试文档如下: Step1:加载文档 //实例化Workbook类的对象 Workbook workbook = n

随机推荐