使用Windows窗体应用开发一个记事本

tech2025-12-08  5

开发语言:C# 开发工具:VS2017 类型:Windows窗体应用 效果图–父窗体 主窗体 功能描述 记事本: 这些事子窗体的功能

在工具栏中添加保存 打开 加粗 倾斜四个带图片文字的按钮,两个下拉框选择字体和字号,文本输入框设置边框 滚动条 铺满整个容器。窗体加载时,将系统字体加载到字体控件中,点击加粗按钮加粗文本框,再次点击取消。点击倾斜按钮,再次点击取消。(未解决问题:不能让同时设置加粗和倾斜属性,字体设置重载方法中,fontStyle.Bold 和fontStyle.Italic不能同时存在,以后解决)。保存文件,为空不能保存,设置用户保存的路径streamwrite。打开文件。 6.新建窗体 注意事项: 如果文件是新建的那么保存的时候应该选择路径,如果不是就直接保存。用户修改了文档如果直接关闭窗体应该提示用户保存。新建按钮。 修改字体 字号,可以通过下拉框修改,也可以直接输入一个值修改。 DisplaStyle:指定是否同时呈现图像和文本。 代码区 父窗体代码 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; namespace TextProj { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //新建一个窗口 private void MenuItemExitNewCre_Click(object sender, EventArgs e) { FormChild fc = new FormChild(); fc.MdiParent = this; fc.Show(); } private void MenuItemExitClose_Click(object sender, EventArgs e) { Form frm = this.ActiveMdiChild; frm.Close(); } private void MenuItemExitCloseAll_Click(object sender, EventArgs e) { foreach (Form frm in this.MdiChildren) { Form f = this.ActiveMdiChild; f.Close(); } } private void MenuItemExit_Click(object sender, EventArgs e) { this.Close(); } } }

子窗体代码

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.IO; using System.Collections; namespace TextProj { public partial class FormChild : Form { public FormChild() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { labFlag.Text = "*"; } //窗体加载事件 private void FormChild_Load(object sender, EventArgs e) { //在窗体加载的时候,将字体加载到combobox中 InstalledFontCollection ifc = new InstalledFontCollection(); FontFamily[] ff = ifc.Families; //获取字体数组 for(int i = 0; i < ff.Length; i++) { toolStripComboFont.Items.Add(ff[i].Name); } } //点击按钮文本内容加粗或取消加粗 private void toolStripBtnBold_Click(object sender, EventArgs e) { //先获取文本内容的加粗属性,只有为false的时候才加粗 //加粗不能修改已经设置的属性,字体和字号 //获取当前的字体 string fontName = toolStripComboFont.Text; string fontSizes = toolStripComboFontSize.Text; float fontSize = 0; if (fontSizes == "字号") { fontSize = 8; } else { fontSize = float.Parse(fontSizes); } if(textBox1.Font.Bold == false) { this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Bold); } else { this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Regular); } } private void toolStripBtnIval_Click(object sender, EventArgs e) { string fontName = toolStripComboFont.Text; string fontSizes = toolStripComboFontSize.Text; float fontSize = 0; if (fontSizes == "字号") //显示当前combobox的Text属性,字号只能是数字,不能是字符串 { fontSize = 8; } else { fontSize = float.Parse(fontSizes); } if (textBox1.Font.Italic == false) { this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Italic); } else { this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Regular); } } private void toolStripComboFont_SelectedIndexChanged(object sender, EventArgs e) { string fontName = toolStripComboFont.Text; string fontSizes = toolStripComboFontSize.Text; float fontSize = 0; if (fontSizes == "字号") { fontSize = 8; } else { fontSize = float.Parse(fontSizes); } this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Italic); } private void toolStripComboFontSize_SelectedIndexChanged(object sender, EventArgs e) { string fontName = toolStripComboFont.Text; string fontSizes = toolStripComboFontSize.Text; float fontSize = 0; if (fontSizes == "字号") { fontSize = 8; } else { fontSize = float.Parse(fontSizes); } this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Italic); } private void toolStripComboFontSize_TextChanged(object sender, EventArgs e) { string fontName = toolStripComboFont.Text; string fontSizes = toolStripComboFontSize.Text; float fontSize = 0; if (fontSizes == "字号") { fontSize = 8; } else { if(fontSize == 0) { fontSize = 1; } fontSize = float.Parse(fontSizes); } this.textBox1.Font = new Font(fontName, fontSize, FontStyle.Italic); } //打开文件按钮 private void toolStripBtnOpen_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 str = sr.ReadToEnd(); textBox1.Text = str; sr.Close(); } } //保存文件按钮 private void toolStripBtnSave_Click(object sender, EventArgs e) { //文件不为空才可以保存 if(this.textBox1.Text.Trim() != "") { saveFileDialog1.Filter = "文本文件(*.txt)|*.txt"; //用户点击OK保存文件 if(saveFileDialog1.ShowDialog() == DialogResult.OK) { //设置用户的保存目录 string path = saveFileDialog1.FileName; StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBox1.Text); sw.Flush(); sw.Close(); } } else { MessageBox.Show("文件内容不能为空,请输入...","提示信息",MessageBoxButtons.OK, MessageBoxIcon.Error); } labFlag.Text = ""; } private void toolStripBtnNewCreate_Click(object sender, EventArgs e) { this.textBox1.Text = ""; this.Text = ""; } private void labFlag_TextChanged(object sender, EventArgs e) { } //如果用户没有保存就关闭窗口,就提醒用户保存之后再关闭 private void FormChild_FormClosed(object sender, FormClosedEventArgs e) { //未保存 if(labFlag.Text == "*") { DialogResult dr = MessageBox.Show("未保存,确认退出吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if(dr == DialogResult.Yes) { this.Dispose(); //释放所有资源 } else { //e.Cancel = true; } } } } }
最新回复(0)