【C#】窗体和控件综合设计(多文本编辑器)


1.实验目的

综合应用C#的MDI窗体设计方法,掌握各控件设计方法,MenuStrip、Context MenuStrip、ToolStrip、StatusStrip等,以及文件管理操作等。

2.实验内容

设计一个多文档界面的Windows应用程序,能够实现对文档的简单处理,包括:打开、关闭、保存文件,复制、剪切、粘贴、撤销等文本处理功能,同时能实现打印。

3.实验方案

3.1 步骤:

  1. 首先完成对Form1的搭建,拖取控件MenuStrip、ToolStrip,并分别进行功能名字和图片的填充,编辑功能只需要添加语句richTextBox1.something(),新建和打开就在控件里进行操作,记得新建的时候把页面初始化为空,文件的存取就很常规了,分为打开、保存、另存为,在Form1中相应事件进行编码,对于修改字体,利用FontDialog控件,注意修改属性,使用非模式对话框在文本编辑器中实现查找和替换功能,需要添加一个窗体formFindReplace来实现交互;
  2. 随后就是利用跟踪控件RichTextBox中的文本内容是否修改;
  3. 之后是打印、页面布局;
  4. 最后是多文本文档那个功能的实现,需要增添一个父窗体FormMain来充当运行入口,放控件ToolStrip依次赋予属性,就可实现文档编辑器的制作。

3.2 功能:

点击运行之后是父窗体,它能够进行多文档的新建、逐一关闭、全部关闭、退出等功能,之后就可以进行子窗体的一系列操作,新建文档、打开、保存、另存为、历史文档记录、页面设置、打印、退出,编辑的撤销、复制、粘贴、删除、全选、恢复、打开时间日期等,还可以进行字体的调整,颜色的调整,查找替换功能、提示保存已被修改的文件、打印预览这些要求功能基本实现。

4.核心代码

①字体格式:

private void menuModelFont_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionFont = fontDialog1.Font;
        }

②字体颜色:

private void menuItemFontColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionColor = colorDialog1.Color;
        }

③保存:

private void menuItemFileSave_Click(object sender, EventArgs e)
        {
            if (s_FileName.Length != 0)
            {
                bSave = false;
                richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);
            }
            else
                menuItemFileSaveAs_Click(sender, e);
        }

④另存为:

private void menuItemFileSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "所有格式文件(*.rtf)|*.rtf|纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                s_FileName = saveFileDialog1.FileName;
                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                bSave = false;
            }
        }

⑤提示编辑后的保存:

public bool IfSaveOldFile()
        {
            bool ReturnValue = true;
            if (bSave)
            {
                System.Windows.Forms.DialogResult dr;
                dr = MessageBox.Show(this, "要保存当前更改吗?", "保存更改吗?",MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dr) //根据用户选择做相应处理
                {
                    case System.Windows.Forms.DialogResult.Yes:
                        bSave = false;//保存文件后,应使bSave为false
                        if (s_FileName.Length != 0)
                            richTextBox1.SaveFile(s_FileName, RichTextBoxStreamType.PlainText);
                        else
                        {
                            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                           
                            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                s_FileName = saveFileDialog1.FileName;
                                richTextBox1.SaveFile(s_FileName, RichTextBoxStreamType.PlainText);
                            }
                        } //返回true通知调用本方法的程序本次操作继续
                        ReturnValue = true;
                        break;
                    //单击了no按钮,不保存
                    case System.Windows.Forms.DialogResult.No:
                        bSave = false;
                        ReturnValue = true;
                        break;
                    //单击了Cancel按钮
                    case System.Windows.Forms.DialogResult.Cancel:
                        ReturnValue = false;
                        break;
                } //返回false,通知调用本方法的程序,本次操作取消
            }
            return ReturnValue;
        }

⑥页面布局:

private void menuItemPage_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 menuItemPrintDocment_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);
                }
            }
        }

⑧时间日期:

private void menuItemEditDate_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show(DateTime.Now.ToString());
        }

⑨调用网站链接:

private void menuItemSend_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.microsoft.com");
        }

5.运行界面

5.1 多文档文本的功能:

在这里插入图片描述

5.2 文本的保存:

在这里插入图片描述

5.3 查看文本的历史纪录、打开:

在这里插入图片描述

5.4 打印预览(页面布局):

在这里插入图片描述

5.5 打印:

在这里插入图片描述

5.6 查看时间日期:

在这里插入图片描述

5.7 Form1的文件菜单栏:

在这里插入图片描述

5.8 查找与替换:

在这里插入图片描述

6.源代码

1、Form1.cs:

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;

namespace notepad
{
    public partial class Form1 : Form
    {
        int FindPostion = 0;
        string s_FileName = "";
        bool bSave=false;
        private string WrongMessage;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(IfSaveOldFile())
            {
                e.Cancel = true;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            menuItemFileNew_Click(sender, e);
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            bSave = true;
        }

        private void menuItemFileNew_Click(object sender, EventArgs e)
        {
            if (!IfSaveOldFile())   //如果返回false本次操作取消
                return;
                richTextBox1.Text = "";
                s_FileName = ""; 
        }

        private void menuItemFileOpen_Click(object sender, EventArgs e)
        {
            if (!IfSaveOldFile())   //如果返回false本次操作取消
                return;
            else
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    s_FileName = openFileDialog1.FileName; richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                }
            }
        }

        private void menuItemFileSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "所有格式文件(*.rtf)|*.rtf|纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                s_FileName = saveFileDialog1.FileName;
                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                bSave = false;
            }
        }

        private void menuItemFileExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuItemFileSave_Click(object sender, EventArgs e)
        {
            if (s_FileName.Length != 0)
            {
                bSave = false;
                richTextBox1.SaveFile(s_FileName,RichTextBoxStreamType.PlainText);
            }
            else
                menuItemFileSaveAs_Click(sender, e);
        }

        private void menuModelFont_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionFont = fontDialog1.Font;
        }

        private void menuItemEditCut_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void menuItemEditCopy_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void menuItemEditPaste_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void menuItemEditUndo_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void menuItemEditRedo_Click(object sender, EventArgs e)
        {
            richTextBox1.Redo();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            formAbout AboutDialog = new formAbout();
            AboutDialog.ShowDialog(this);   //打开模式对话框
        }

        private void menuItemEditFind_Click(object sender, EventArgs e)
        {
            FindPostion = 0;
            formFindReplace FindReplaceDialog = new formFindReplace(this);
            FindReplaceDialog.Show();
        }
        public void FindRichTextBoxString(string FindString)
        {
            if (FindPostion >= richTextBox1.Text.Length)
            {
                MessageBox.Show("已到文本底部,再次查找将从文本开始处查找", "提示", MessageBoxButtons.OK);
                FindPostion = 0;
                return;
            }
            FindPostion = richTextBox1.Find(FindString, FindPostion, RichTextBoxFinds.MatchCase);
            if (FindPostion == -1)
            {
                MessageBox.Show("未找到匹配字符串,再次查找将从文本开始处查找", "提示", MessageBoxButtons.OK);
                FindPostion = 0;
            }
            else
            {
                richTextBox1.Focus();
                FindPostion += FindString.Length;
            }
        }

        private void menuItemEditRe_Click(object sender, EventArgs e)
        {
            FindPostion = 0;
            formFindReplace FindReplaceDialog = new formFindReplace(this);
            FindReplaceDialog.Show();
        }
        public void ReplaceRichTextBoxString(string ReplaceString)
        {
            if (richTextBox1.SelectedText.Length != 0)
                richTextBox1.SelectedText = ReplaceString;

        }
        public bool IfSaveOldFile()
        {
            bool ReturnValue = true;
            if (bSave)
            {
                System.Windows.Forms.DialogResult dr;
                dr = MessageBox.Show(this, "要保存当前更改吗?", "保存更改吗?",MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dr) //根据用户选择做相应处理
                {
                    case System.Windows.Forms.DialogResult.Yes:
                        bSave = false;//保存文件后,应使bSave为false
                        if (s_FileName.Length != 0)
                            richTextBox1.SaveFile(s_FileName, RichTextBoxStreamType.PlainText);
                        else
                        {
                            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                           
                            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                s_FileName = saveFileDialog1.FileName;
                                richTextBox1.SaveFile(s_FileName, RichTextBoxStreamType.PlainText);
                            }
                        } //返回true通知调用本方法的程序本次操作继续
                        ReturnValue = true;
                        break;
                    //单击了no按钮,不保存
                    case System.Windows.Forms.DialogResult.No:
                        bSave = false;
                        ReturnValue = true;
                        break;
                    //单击了Cancel按钮
                    case System.Windows.Forms.DialogResult.Cancel:
                        ReturnValue = false;
                        break;
                } //返回false,通知调用本方法的程序,本次操作取消
            }
            return ReturnValue;
        }

        private void menuModelChangeLine_Click(object sender, EventArgs e)
        {
            if (this.menuModelChangeLine.Checked)
            {
                richTextBox1.WordWrap = true;
            }
            else
            {
                richTextBox1.WordWrap = false;
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            menuItemFileSave_Click(sender, e);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void menuItemEditAll_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void menuItemEditDate_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show(DateTime.Now.ToString());
        }

        private void menuItemSend_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.microsoft.com");
        }

        private void menuItemRefresh_Click(object sender, EventArgs e)
        {
            MessageBox.Show("该系统已为最新版本", "提示");
        }
        
        private void menuItemTxtList_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Explorer.exe", "E:\\Vs2019Item");
        }

        private void menuItemPage_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 menuItemSQL_Click(object sender, EventArgs e)
        {
            MessageBox.Show("尚未建立数据库,请耐心等待!", "提示");
        }

        private void menuItemHistory_Click(object sender, EventArgs e)
        {
            menuItemTxtList_Click(sender, e);
        }

        private void menuItemFontColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionColor = colorDialog1.Color;
        }

        private void menuItemPrintDocment_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);
                }
            }
        }
    }
}

2、formAbout.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;

namespace notepad
{
    public partial class formAbout : Form
    {
        public formAbout()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

3、formFindReplace.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;

namespace notepad
{
    public partial class formFindReplace : Form
    {
        Form1 MainForm1;
        public formFindReplace(Form1 form1)
        {
            
            InitializeComponent();
            MainForm1 = form1;
        }

        private void formFindReplace_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length != 0)
            {
                MainForm1.FindRichTextBoxString(textBox1.Text);
            }
            else
                MessageBox.Show("查找字符串不能为空", "提示",MessageBoxButtons.OK);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length != 0)
            {
                MainForm1.ReplaceRichTextBoxString(textBox2.Text);
            }
            else
                MessageBox.Show("替换字符串不能为空", "提示",MessageBoxButtons.OK);
        }
    }
}

4、FormMain.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;

namespace notepad
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form1 child = new Form1();
            child.MdiParent = this;
            child.Show();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Form form1 = this.ActiveMdiChild;
            form1.Close();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            foreach(Form form in this.MdiChildren)
            {
                Form form1 = this.ActiveMdiChild;
                form1.Close();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

5、Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace notepad
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());
        }
    }
}

注:点击此处下载项目工程:源工程压缩包

  • 8
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
【可视化编程】实验4: c 实验4:c是一种新型的可视化编程语言,它与其他编程语言不同之处在于它使用图表来表示代码逻辑。c的主要目标是通过可视化编程的方式让初学者更容易理解和编写代码。 在实验4:c中,我们将学习如何使用c语言来解决实际问题。例如,我们可以使用c语言编写一个计算应用程序,它可以进行基本的数学运算,如加法、减法、乘法和除法。通过使用c语言的图表表示方法,我们可以更直观地看到代码的逻辑。 在编写c程序时,我们首先需要定义输入和输出。输入是指我们程序中需要获取的数据,而输出是指我们程序中将要产生的结果。在实验4:c中,我们可以使用图表的输入输出节点来表示输入和输出。比如,在计算应用程序中,我们可以使用一个输入节点来接收用户输入的数字,然后使用一个输出节点来显示计算结果。 另一个关键的概念是使用图表的各种节点来表示代码的逻辑。在实验4:c中,我们可以使用图表中的条件节点来表示条件语句,使用循环节点来表示循环语句,使用函数节点来表示函数调用等等。通过使用这些节点,我们可以更清楚地看到代码的执行流程。 实验4:c还包括一些高级特性,如使用变量和数组来存储数据,使用自定义函数来组织代码等等。这些特性使得c语言更强大和灵活,可以应用于更复杂的问题。 总而言之,实验4:c是一种新颖的可视化编程语言,它以图表的形式展现代码逻辑,使初学者更易理解和编写代码。通过实验4:c,我们可以学习到编程的基本概念和技巧,为今后更深入地学习编程打下基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值