实例介绍
【实例简介】
C#记事本程序
【实例截图】
【核心代码】
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Text; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; namespace Notepad { public partial class MainForm : DevComponents.DotNetBar.Office2007RibbonForm { //定义文件操作类 private string fileURL = ""; private bool change; private string Filter = "文本文件(*.txt)|*.txt"; private Font font; private FileInfo fileInfo; private string[] fontStyle = new string[4] {"","","",""}; private Color color; public MainForm() { InitializeComponent(); } //加载处理 private void MainForm_Load(object sender, EventArgs e) { LineColAndSum(); this.richTextBox1.AllowDrop = true; this.richTextBox1.DragEnter = new DragEventHandler(RichTextBox1_DragEnter); //this.richTextBox1.DragDrop = new DragEventHandler(RichTextBox1_DragDrop); font = richTextBox1.Font; color = richTextBox1.ForeColor; boldButton.Checked = false; ltalicButton.Checked = false; unLineButton.Checked = false; stLineButton.Checked = false; fontLeftButton_Click(sender, e); ControlChanged(); fontRightButton.Image.RotateFlip(RotateFlipType.Rotate180FlipY); GetFontNames(); GetFontSize(); showFile_Message(); change = false; } //获取系统字体和样式 private void GetFontNames() { FontFamily[] fontFamilies; fontNames.Items.Clear(); InstalledFontCollection installedFontCollection = new InstalledFontCollection(); fontFamilies = installedFontCollection.Families; int count = fontFamilies.Length; for (int j = 0; j < count; j) { fontNames.Items.Add(fontFamilies[j].Name); } fontNames.SelectedItem = richTextBox1.Font.Name; } //字号初始化 private void GetFontSize() { for (float i = 8; i < 48; i ) { fontSize.Items.Add(i); } fontSize.SelectedItem = richTextBox1.Font.Size; } //文本框格式的显示 private void showFont() { float size = float .Parse((font.Size * this.sliderItem1.Value / 100).ToString()); this.richTextBox1.Font = new Font(font.Name, size,font.Style); this.richTextBox1.ForeColor = color; } //统计字数和行列定位 private void LineColAndSum() { int index = richTextBox1.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引 int line = richTextBox1.GetLineFromCharIndex(index) 1;//得到当前行的行号,从0开始,习惯是从1开始,所以 1. int col = richTextBox1.SelectionStart - index 1;//.SelectionStart得到光标所在位置的索引 减去 当前行第一个字符的索引 = 光标所在的列数(从0开始) int sum = richTextBox1.Text.Length; labelItemLnCh.Text = "行:" line "列:" col "总字数:" sum; } //控件检测 private void ControlChanged() { //backButton检测 if (richTextBox1.CanUndo) backButton.Enabled = true; else backButton.Enabled = false; //redoButton检测 if (richTextBox1.CanRedo) redoButton.Enabled = true; else redoButton.Enabled = false; } //文本发生变化 private void TextChanged(object sender, EventArgs e) { change = true; LineColAndSum(); ControlChanged(); } //菜单的展开与收缩 private void switchButtonItem1_ValueChanged(object sender, EventArgs e) { if (switchButtonItem1.Value == true) this.ribbonControl1.Expanded = false; else this.ribbonControl1.Expanded = true; } //打开文件 private void openFile_Click(object sender, EventArgs e) { string mess = closeFile(sender,e); if(mess.Equals("Cancel")) return; try { OpenFileDialog open = new OpenFileDialog(); open.Title = "打开文本文件"; open.AddExtension = true; open.CheckFileExists = true; open.CheckPathExists = true; open.Filter = this.Filter; if (open.ShowDialog() == DialogResult.OK) { // this.Text = open.SafeFileName "-记事本"; fileURL = open.FileName; fileInfo = new FileInfo(fileURL); StreamReader Sr = new StreamReader(fileURL, System.Text.Encoding.Default); this.richTextBox1.Text = Sr.ReadToEnd(); MainForm_Load(sender, e); Sr.Close(); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); } } //显示文件的信息 private void showFile_Message() { if (fileInfo!=null) { lablelFileName.Text = fileInfo.Name; labelFileSize.Text = (fileInfo.Length) " 字节"; labelFileDate.Text = fileInfo.CreationTimeUtc.ToString(); } else { lablelFileName.Text = "未命名"; labelFileSize.Text = "0 字节"; labelFileDate.Text = "未创建文件"; } } //字体放大或缩小 private void sliderItem1_ValueChanged(object sender, EventArgs e) { if (sliderItem1.Value < 100) { sliderItem1.Minimum = 25; sliderItem1.Maximum = 175; sliderItem1.Step = 3; if (sliderItem1.Value==99||sliderItem1.Value==80) sliderItem1.Value = 97; } else if (sliderItem1.Value > 100) { sliderItem1.Minimum = -200; sliderItem1.Maximum = 400; sliderItem1.Step = 20; if (sliderItem1.Value==101||sliderItem1.Value==103) sliderItem1.Value = 120; } this.sliderItem1.Text = sliderItem1.Value.ToString() "%"; showFont(); } //文件存储 private void saveFile() { try { FileStream Fs = File.Open(fileURL, FileMode.Create, FileAccess.Write); StreamWriter Sw = new StreamWriter(Fs,Encoding.Default); Sw.Write(richTextBox1.Text); change = false; Sw.Close(); Fs.Close(); fileInfo = new FileInfo(fileURL); showFile_Message(); } catch (Exception Ex) { MessageBox.Show(Ex.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } //创建新文件 private void newFile_Click(object sender, EventArgs e) { string mess = closeFile(sender,e); if(mess.Equals("Cancel")) return; else { fileURL = null; fileInfo = null; richTextBox1.Clear(); MainForm_Load(sender, e); } } //另存为对话框 private void saveFileAsButton() { SaveFileDialog save = new SaveFileDialog(); save.Title = "另存为"; save.AddExtension = true; save.AutoUpgradeEnabled = true; save.Filter = this.Filter; if (save.ShowDialog() == DialogResult.OK) fileURL = save.FileName; else return; } //撤销按钮 private void backButton_Click(object sender, EventArgs e) { if (richTextBox1.CanUndo) { richTextBox1.Undo(); // fontNames.SelectedItem = richTextBox1.Font.Name; // fontSize.SelectedItem = richTextBox1.Font.Size * 100 / sliderItem1.Value; LineColAndSum(); ControlChanged(); } else return; } //撤销-撤销按钮 private void redoButton_Click(object sender, EventArgs e) { if (richTextBox1.CanRedo) { richTextBox1.Redo(); LineColAndSum(); ControlChanged(); } else return; } //光标位置的变化---鼠标 private void CursorChanged(object sender, EventArgs e) { LineColAndSum(); } //光标位置的变化---键盘 private void richTextBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) LineColAndSum(); } //粘贴 private void pasteButton_Click(object sender, EventArgs e) { richTextBox1.Paste(); LineColAndSum(); } //剪切 private void cutButton_Click(object sender, EventArgs e) { richTextBox1.Cut(); LineColAndSum(); } //复制 private void copyButton_Click(object sender, EventArgs e) { richTextBox1.Copy(); } //删除所选择的文字 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { richTextBox1.SelectedText = ""; } //选中全部文字 private void 全选ToolStripMenuItem_Click(object sender, EventArgs e) { richTextBox1.SelectAll(); } //字体名称发生变化 private void fontNames_SelectedIndexChanged(object sender, EventArgs e) { string Name = fontNames.SelectedItem.ToString(); font = new Font(Name, font.Size); showFont(); } //字体字号发生变化 private void fontSize_SelectedIndexChanged(object sender, EventArgs e) { float size = float.Parse(fontSize.SelectedItem.ToString()); size = size*sliderItem1.Value/100; font = new Font(font.Name, size); showFont(); } //字体样式统计 private void fontStyle_Changed() { font = new Font(font.Name, font.Size, FontStyle.Regular); foreach (string i in fontStyle) { //string i = fontStyle[0]; if (i.Equals("bold")) font = new Font(font.Name, font.Size, font.Style | FontStyle.Bold); if(i.Equals("ltalic")) font = new Font(font.Name, font.Size, font.Style | FontStyle.Italic); if (i.Equals("underline")) font = new Font(font.Name, font.Size, font.Style | FontStyle.Underline); if(i.Equals("strikeout")) font = new Font(font.Name, font.Size, font.Style | FontStyle.Strikeout); } showFont(); } //字体粗体设置 private void boldButton_Click(object sender, EventArgs e) { if (boldButton.Checked == false) { boldButton.Checked = true; fontStyle[0] = "bold"; } else { boldButton.Checked = false; fontStyle[0] = ""; } fontStyle_Changed(); } //倾斜字体设置 private void ltalicButton_Click(object sender, EventArgs e) { if (ltalicButton.Checked == false) { ltalicButton.Checked = true; fontStyle[1] = "ltalic"; } else { ltalicButton.Checked = false; fontStyle[1] = ""; } fontStyle_Changed(); } //下划线字体设置 private void unLineButton_Click(object sender, EventArgs e) { if (unLineButton.Checked == false) { unLineButton.Checked = true; fontStyle[2] = "underline"; } else { unLineButton.Checked = false; fontStyle[2] = ""; } fontStyle_Changed(); } //删除线设置 private void stLineButton_Click(object sender, EventArgs e) { if (stLineButton.Checked == false) { stLineButton.Checked = true; fontStyle[3] = "strikeout"; } else { stLineButton.Checked = false; fontStyle[3] = ""; } fontStyle_Changed(); } //字体颜色发生变化 private void colorButton_SelectedColorChanged(object sender, EventArgs e) { color = colorButton.SelectedColor; showFont(); } //文本左对齐 private void fontLeftButton_Click(object sender, EventArgs e) { if (fontLeftButton.Checked == true) return; else { fontLeftButton.Checked = true; fontRightButton.Checked = false; fontCenterButton.Checked = false; richTextBox1.SelectionAlignment = HorizontalAlignment.Left; } } //右对齐 private void fontRightButton_Click(object sender, EventArgs e) { if (fontRightButton.Checked == true) return; else { fontRightButton.Checked = true; fontCenterButton.Checked = false; fontLeftButton.Checked = false; richTextBox1.SelectionAlignment = HorizontalAlignment.Right; } } //居中对齐 private void fontCenterButton_Click(object sender, EventArgs e) { if (fontCenterButton.Checked == true) return; else { fontLeftButton.Checked = false; fontRightButton.Checked = false; fontCenterButton.Checked = true; richTextBox1.SelectionAlignment = HorizontalAlignment.Center; } } //点击保存按钮 private void saveFileButton_Click(object sender, EventArgs e) { if (fileURL.Equals("")) saveFileAsButton(); if (!fileURL.Equals("")) saveFile(); else return; } //点击另存为按钮 private void saveAsFileButton_Click(object sender, EventArgs e) { saveFileAsButton(); if (!fileURL.Equals("")) saveFile(); else return; } //当用户聚焦到textKey上时 private void textKey_GotFocus(object sender, EventArgs e) { if (textKey.Text.Equals(" 请输入关键字")) textKey.Text = ""; else return; } //当用户对textKey失去聚焦时 private void textKey_LostFocus(object sender, EventArgs e) { if (textKey.Text.Equals("")) textKey.Text = " 请输入关键字"; else return; } //查找关键字 private void findKey_Click(object sender, EventArgs e) { string key = textKey.Text; if (richTextBox1.Find(key, richTextBox1.SelectionStart richTextBox1.SelectionLength, richTextBox1.TextLength, RichTextBoxFinds.None) < 0) // 为-1表示没有找到 { richTextBox1.SelectionStart = 0; // 光标设置为0位置 richTextBox1.SelectionLength = 0; } richTextBox1.Focus(); } //打印 private void buttonItem6_Click(object sender, EventArgs e) { MessageBox.Show("o(︶︿︶)o 唉~\n开发中..."); } //关闭文档 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { string mess = closeFile(sender, e); if (mess.Equals("Cancel")) e.Cancel = true; } //点击退出按钮 private void exitbutton_Click(object sender, EventArgs e) { String mess = closeFile(sender, e); if (mess.Equals("Cancel")) Application.Exit(); } //经典蓝 private void StyleBlue_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Blue; } //水晶银 private void StyleSilver_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Silver; } //酷炫黑 private void StyleBlack_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Black; } //VistaGlass private void StyleVistaGlass_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007VistaGlass; } //洁白银 private void StyleSilver1_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Silver; } //浅色蓝 private void StyleBlue1_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Blue; } //咖啡黑 private void StyleBlack1_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Black; } //Win7蓝 private void StyleBlue2_Click(object sender, EventArgs e) { styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Windows7Blue; } //拖入文件操作 private void RichTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { string mess = closeFile(sender, e); if (mess.Equals("Cancel")) return; else { fileURL = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); fileInfo = new FileInfo(fileURL); StreamReader Sr = new StreamReader(fileURL, System.Text.Encoding.Default); this.richTextBox1.Text = Sr.ReadToEnd(); MainForm_Load(sender, e); Sr.Close(); } } //关闭当前文件 private String closeFile(Object sender, EventArgs e) { if (change) { DialogResult DResut = MessageBox.Show("文件发生了改动,是否保存?", "信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (DResut == DialogResult.Yes) { saveFileButton_Click(sender, e); return "Yes"; } else if (DResut == DialogResult.No) { return "No"; } else return "Cancel"; } else return "No"; } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论