简易记事本

    技术2022-07-10  134

    简易记事本

    加载系统字体

    //加载系统字体 InstalledFontCollection myFont = new InstalledFontCollection(); //获取InstalledFontCollection对象的数组 FontFamily[] ff = myFont.Families; //声明一个ArrayList变量 ArrayList list = new ArrayList(); int count = ff.Length; for(int i = 0; i < count; i++) { toolStripComboBox1.Items.Add(ff[i].Name); }

    字体加粗

    //点击按钮加粗,加粗时再点击按钮取消加粗 if (textBox1.Font.Bold == true) { textBox1.Font = new Font(textBox1.Font, FontStyle.Regular); } else { textBox1.Font = new Font(textBox1.Font, FontStyle.Bold); }

    字体倾斜

    if (textBox1.Font.Italic == true) { textBox1.Font = new Font(textBox1.Font, FontStyle.Regular); } else { textBox1.Font = new Font(textBox1.Font, FontStyle.Italic); }

    修改字体类型

    string fontName = toolStripComboBox1.Text; float fontSize = float.Parse(toolStripComboBox2.Text); textBox1.Font = new Font(fontName, fontSize);

    修改字号

    private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e) { string fontName = toolStripComboBox1.Text; float fontSize = float.Parse(toolStripComboBox2.Text); textBox1.Font = new Font(fontName, fontSize); }

    保存

    //保存 private void toolStripButton2_Click(object sender, EventArgs e) { if (textBox1.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(textBox1.Text.Trim()); sw.Flush(); sw.Close(); } }else { //保存文件到用户指定的目录位置 //获取用户选择的文件及路径 string path = this.Text; //保存文件到指定位置 StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBox1.Text.Trim()); sw.Flush(); sw.Close(); } }else { MessageBox.Show("空文档不能保存", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Error); } }

    打开

    //打开 private void toolStripButton1_Click(object sender, EventArgs e) { //创建一个过滤器,只能打开txt文本文档 openFileDialog1.Filter = ("文本文档(*.txt)|*.txt"); //判断用户点击的是打开按钮还是取消按钮 if (openFileDialog1.ShowDialog() == DialogResult.OK) { //获取打开文档的路径 string path = openFileDialog1.FileName; //通用编码UTF8 StreamReader sr = new StreamReader(path, Encoding.UTF8); //读取文档中的数据流 string text = sr.ReadToEnd(); textBox1.Text = text; this.Text = path; this.toolStripLabel1.Text = ""; sr.Close(); } }

    退出

    private void frmChild_FormClosing(object sender, FormClosingEventArgs e) { if (this.toolStripLabel1.Text == "*") { DialogResult dr = MessageBox.Show("文件尚未保存,确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { this.Dispose(); }else { e.Cancel=true; } } }
    Processed: 0.010, SQL: 9