C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序-創(chuàng)新互聯(lián)

這篇文章主要介紹了C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為福田等服務(wù)建站,福田等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為福田企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

具體內(nèi)容如下

編程語(yǔ)言: C#
編程環(huán)境: Visual Studio 2013
運(yùn)行環(huán)境: .NET Framework 4.5

預(yù)覽:

C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序

功能:

標(biāo)題欄
顯示文件標(biāo)題

菜單欄
各類(lèi)菜單命令

文件- 新建
- 打開(kāi)
- 保存
- 另存為
- 頁(yè)面設(shè)置
- 打印
- 退出

編輯
- 撤銷(xiāo)
- 剪切
- 復(fù)制
- 粘貼
- 全選
- 時(shí)間/日期

格式
- 自動(dòng)換行
- 字體

視圖
- 狀態(tài)欄
- 工具欄
- 全屏模式

幫助
- 開(kāi)源許可
- 查看幫助
- 關(guān)于

工具欄
常用工具集合

標(biāo)簽欄
文件標(biāo)簽顯示

工作區(qū)
編輯區(qū)

狀態(tài)欄
顯示文件狀態(tài)

文本狀態(tài)(新建/已修改)

  • 字符個(gè)數(shù)

  • 光標(biāo)坐標(biāo)

  • 功能實(shí)現(xiàn)

本程序有兩個(gè)窗體,分別為Form1和AboutBox1

所有文件如下:

C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序

對(duì)于Form1:
需要引入的類(lèi)庫(kù):

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 打開(kāi)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 另存為T(mén)oolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 編輯ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 關(guān)于ToolStripMenuItem;
private System.Windows.Forms.TextBox editBox1;
private System.Windows.Forms.ToolStripMenuItem 撤銷(xiāo)ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 剪切ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 復(fù)制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 狀態(tài)欄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 頁(yè)面設(shè)置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 自動(dòng)換行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 開(kāi)源許可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();

  }

首先進(jìn)行一些固定變量聲明:

private string textFileName = "無(wú)標(biāo)題";
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 = "你好像遇到了錯(cuò)誤...";
private string fileFormat = "文本文件(*.txt)|*.txt|Icey文件(*.ice)|*.ice|C++文件(*.cpp)|*.cpp|C文件(*.c)|*.c|所有文件(*.*)|(*.*)";
private string tabFileName1 = "無(wú)標(biāo)題1 - Icey";
private string tabFileName2 = "無(wú)標(biāo)題2 - Icey";
  Boolean saveFileStatus1 = false;
  Boolean textChanged1 = false;
  Boolean saveFileStatus2 = false;
  Boolean textChanged2 = false;

保存文件采用函數(shù)形式:

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 = "已保存";
  }

另存為采用函數(shù)形式:

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 = "已保存";
}

新建函數(shù):

 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 = "";
   }
 }

打開(kāi)文件函數(shù):

private void openFile()//打開(kāi)
   {
    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;
    }
}

全屏模式函數(shù):

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是否發(fā)生變化:

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();
  }

打開(kāi)菜單命令:

private void 打開(kāi)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;
   }
  }

退出動(dòng)作(當(dāng)用戶(hù)點(diǎn)擊窗體右上角退出按鈕時(shí)執(zhí)行此操作):

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
   if (editBox1.Text != String.Empty && e.CloseReason == CloseReason.UserClosing || saveFileStatus1 == false && textChanged1 == true)//如果文本框不為空&&觸發(fā)關(guān)閉按鈕事件
   {
    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)//如果文本框不為空&&觸發(fā)關(guān)閉按鈕事件
   {
    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();
  }

狀態(tài)欄命令(狀態(tài)欄是否顯示):

private void 狀態(tài)欄ToolStripMenuItem_Click(object sender, EventArgs e)
 {
   if (狀態(tài)欄ToolStripMenuItem.Checked == true)
   {
    狀態(tài)欄ToolStripMenuItem.Checked = false;
    statusStrip1.Visible = false;
   }
   else
   {
    狀態(tài)欄ToolStripMenuItem.Checked = true;
    statusStrip1.Visible = true;
   }
}

編輯命令:

private void 編輯ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if ((editBox1.SelectedText.Equals("")))
   {
    剪切ToolStripMenuItem.Enabled = false;
    復(fù)制ToolStripMenuItem.Enabled = false;
    刪除ToolStripMenuItem.Enabled = false;
   }
   else
   {
    剪切ToolStripMenuItem.Enabled = true;
    復(fù)制ToolStripMenuItem.Enabled = true;
    刪除ToolStripMenuItem.Enabled = true;
   }

  }

全選命令:

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

剪切 復(fù)制 粘貼 刪除命令:

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 撤銷(xiāo)ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (editBox1.Focused)
   {
    this.editBox1.Undo();
   }
   else
    this.editBox2.Undo();
  }

  private void 復(fù)制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 另存為T(mén)oolStripMenuItem_Click(object sender, EventArgs e)
 {
   saveAsFile();
  }

時(shí)間戳命令:

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

頁(yè)面設(shè)置命令:

private void 頁(yè)面設(shè)置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);
    }
   }
  }

查看幫助 關(guān)于命令:

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

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

自動(dòng)換行命令:

private void 自動(dòng)換行ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   if (自動(dòng)換行ToolStripMenuItem.Checked == true)
   {
    editBox1.WordWrap = false;
    editBox2.WordWrap = false;
    自動(dòng)換行ToolStripMenuItem.Checked = false;
   }
   else
   {
    editBox1.WordWrap = true;
    editBox2.WordWrap = true;
    自動(dòng)換行ToolStripMenuItem.Checked = true;
   }
  }

計(jì)時(shí)器(100 ms刷新頻率):

 private void timer1_Tick(object sender, EventArgs e)
  {
   toolStripStatusLabel1.Text = (editBox1.Focused) ? editBox1.Text.Length.ToString() + " 個(gè)字符" : editBox2.Text.Length.ToString() + " 個(gè)字符";
   int totalline = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(editBox1.Text.Length) + 1 : editBox2.GetLineFromCharIndex(editBox2.Text.Length) + 1;//得到總行數(shù)
   int index = (editBox1.Focused) ? editBox1.GetFirstCharIndexOfCurrentLine() : editBox2.GetFirstCharIndexOfCurrentLine();//得到當(dāng)前行第一個(gè)字符的索引
   int line = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(index) + 1 : editBox2.GetLineFromCharIndex(index) + 1;//得到當(dāng)前行的行號(hào)
   int col = (editBox1.Focused) ? editBox1.SelectionStart - index + 1 : editBox2.SelectionStart - index + 1;//.SelectionStart得到光標(biāo)所在位置的索引 - 當(dāng)前行第一個(gè)字符的索引 = 光標(biāo)所在的列數(shù)
   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;
   }
  }

開(kāi)源許可命令:

private void 開(kāi)源許可OToolStripMenuItem_Click(object sender, EventArgs e)
  {
   System.Diagnostics.Process.Start(openSourceUrl); 
  }

工具欄的各個(gè)按鈕:

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(); 
}

標(biāo)簽欄:

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 = "新建";
   }
  }

對(duì)于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("關(guān)于 {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 程序集特性訪問(wèn)器

  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();
  }
 }
}

C#是什么

C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開(kāi)發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開(kāi)發(fā)的選語(yǔ)言,但它不適用于編寫(xiě)時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司,關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前文章:C#如何實(shí)現(xiàn)簡(jiǎn)單記事本程序-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article18/hipdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作服務(wù)器托管、自適應(yīng)網(wǎng)站、商城網(wǎng)站建站公司、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)