基本功能描述:可以实现文本的编辑(改字体字号)以及保存和打开文件。
1.界面设计
2.代码
【Myform.cs】 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Text; using System.Collections; using System.IO; namespace 记事本 { public partial class Myform : Form { public Myform() { InitializeComponent(); } //窗体加载 private void Myform_Load(object sender, EventArgs e) { //加载系统字体 InstalledFontCollection myFonts = new InstalledFontCollection(); //获取数组 FontFamily[] ff = myFonts.Families; //声明 ArrayList list = new ArrayList(); //获取数组列表长度 int count = ff.Length; //使用for循环把字体名称 for (int i = 0; i < count; i++) { string FontName = ff[i].Name; toolStripComboBoxFont.Items.Add(FontName); } } //加粗 private void toolStripButtonBold_Click(object sender, EventArgs e) { //点击按钮加粗 if (textBoxNote.Font.Bold == false) { textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Bold); } else//恢复 { textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular); } } private void toolStripButtonItalic_Click(object sender, EventArgs e) { //点击倾斜 if (textBoxNote.Font.Italic == false) { textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Italic); } else//恢复 { textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular); } } //改变字体 private void toolStripComboBoxFont_SelectedIndexChanged(object sender, EventArgs e) { string fontName = toolStripComboBoxFont.Text; float fontSize = float.Parse(toolStripComboBoxSize.Text); textBoxNote.Font = new Font(fontName, fontSize); } //改变字号 private void toolStripComboBoxSize_TextChanged(object sender, EventArgs e) { string fontName = toolStripComboBoxFont.Text; float fontSize = float.Parse(toolStripComboBoxSize.Text); textBoxNote.Font = new Font(fontName, fontSize); } private void textBoxNote_TextChanged(object sender, EventArgs e) { } //保存文档 private void toolStripButtonSave_Click(object sender, EventArgs e) { if (textBoxNote.Text.Trim() != "") { if (this.Text == "") { //新建一个保存文件的对话框 //创建一个筛选器 saveFileDialog1.Filter = ("文本文档(*.txt)|*.txt"); //判断 if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //保存文件到用户指定位置 //获取用户选择的文件及路径 string path = saveFileDialog1.FileName; //保存文件到指定路径 StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBoxNote.Text.Trim()); sw.Flush(); sw.Close(); } else { //保存文件到用户指定位置 //获取用户选择的文件及路径 string path = this.Text; //保存文件到指定路径 StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBoxNote.Text.Trim()); sw.Flush(); sw.Close(); } } } else { MessageBox.Show("空文档不能保存", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //打开文档 /*private void toolScripButtonOpen_Click(object sender, EventArgs e) { }*/ private void toolStripButtonOpen_Click(object sender, EventArgs e) { openFileDialog1.Filter = ("文本文档(*.txt)|*.txt"); //判断打开还是取消 if (openFileDialog1.ShowDialog() == DialogResult.OK) { string path = openFileDialog1.FileName; //通用编码 StreamReader sr = new StreamReader(path, Encoding.UTF8); //读取数据流 string text = sr.ReadToEnd(); textBoxNote.Text = text; //将打开的路径写入当前窗体的text属性中 this.Text = path; sr.Close(); } } private void toolStripComboBoxSize_Click(object sender, EventArgs e) { string fontName = toolStripComboBoxFont.Text; float fontSize = float.Parse(toolStripComboBoxSize.Text); textBoxNote.Font = new Font(fontName, fontSize); } } }
PS:我是跟着B站一个up主敲得代码,应付实验课的要求所以并没有实现进一步的功能,详情可以看 https://www.bilibili.com/video/av27671190(本人水平比较低,见谅啦)