Bootstrap

如何使用Visual Studio 2022做出简单的记事本?

一.使用工具、创建环境、语言 

使用工具:Visual Studio 2022(社区免费版)

创建环境:Win 11系统

语言:C#

二.创建步骤

第一步:创建一个新的项目,具体步骤如下图所示

第二步:项目创建好后如下界面

在工具箱中拖动需要的控件到窗体上,若页面中没有工具箱,可以在视图中点击。页面右下角是控件的相关属性和事件。(若一个解决方案中有多个窗体,可以在Program.cs文件中更改启动窗体)

 

 第三步:文本编辑器所需控件及其相关属性如下表:
控件类型控件名称(Name属性)控件文本内容(Text属性)
Form窗体FrmMain_Edit文本编辑器
MenuStripmenuStrip1menuStrip1
ToolStriptoolStrip1toolStrip1
RichTextBoxrtxtText哈利波特骑着扫帚飞...(可自行修改)
StatusStripstatusStrip1statusStrip1
ToolStripMenuItemtsmiFile文件(&F)
ToolStripMenuItemtsmiEdit编辑(&E)
ToolStripMenuItemtsmiNew新建<Ctrl+N>
ToolStripMenuItemtsmiOpen打开<Ctrl+O>
ToolStripMenuItemtsmiSave保存<Ctrl+S>
ToolStripMenuItemtsmiPrint打印<Ctrl+P>
ToolStripMenuItemtsmiExit退出<Ctrl+E>
ToolStripMenuItemtsmiCut剪切<Ctrl+X>
ToolStripMenuItemtsmiCopy复制<Ctrl+C>
ToolStripMenuItemtsmiPaste粘贴<Ctrl+V>
ToolStripButtontbtnCut剪切
ToolStripButtontbtnCopy复制
ToolStripButtontbtnPaste粘贴
ToolStripComboBoxcboFont定义字体类型
ToolStripComboBoxcboSize定义字体大小
ToolStripStatusLabeltsslMousePositiontoolStripStatusLabel1
ContextMenuStripcontextMenuStrip1------
ToolStripMenuItemcmiCut剪切<Ctrl+X>
ToolStripMenuItemcmiCopy复制<Ctrl+C>
ToolStripMenuItemcmiPaste粘贴<Ctrl+V>

搭建好的界面如下图所示:

第四步:编写相关控件的代码: 双击页面上的控件进入代码编辑界面
public partial class Frm65_Edit : Form
{
    public Frm65_Edit()
    {
        InitializeComponent();
    }

    private void tsmiCut_Click(object sender, EventArgs e)
    {
        //将所选内容放到剪切板上,清除所选内容
        Clipboard.SetDataObject(rtxtText.SelectedText);
        rtxtText.SelectedText = String.Empty;
    }

    private void tsmiCopy_Click(object sender, EventArgs e)
    {
        Clipboard.SetDataObject(rtxtText.SelectedText);
    }

    private void Paste()
    {
        //将剪切板上的内容放到指定位置
        IDataObject iData = Clipboard.GetDataObject();
        rtxtText.SelectedText = (String)iData.GetData(DataFormats.Text);
    }

    private void tsmiPaste_Click(object sender, EventArgs e)
    {
        Paste();
    }

    private void cmiCut_Click(object sender, EventArgs e)
    {
        //将所选内容放到剪切板上,清除所选内容
        Clipboard.SetDataObject(rtxtText.SelectedText);
        rtxtText.SelectedText = String.Empty;
    }

    private void cmiPaste_Click(object sender, EventArgs e)
    {
        Paste();
    }

    private void cmiCopy_Click(object sender, EventArgs e)
    {
        Clipboard.SetDataObject(rtxtText.SelectedText);
    }

    private void tbtnCut_Click(object sender, EventArgs e)
    {
        Clipboard.SetDataObject(rtxtText.SelectedText);
        rtxtText.SelectedText = String.Empty;
    }

    private void tbtnCopy_Click(object sender, EventArgs e)
    {
        Clipboard.SetDataObject(rtxtText.SelectedText);
    }

    private void tbtnPaste_Click(object sender, EventArgs e)
    {
        Paste();
    }

    private void cboFont_SelectedIndexChanged(object sender, EventArgs e)
    {
        rtxtText.SelectionFont = new Font(cboFont.Text, rtxtText.SelectionFont.Size);
    }

    private void cboSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        float dSize = 0;
        dSize = Convert.ToSingle(cboSize.Text);
        rtxtText.SelectionFont = new Font(rtxtText.SelectionFont.Name, dSize);
    }

    private void tsmiOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlgOpen = new OpenFileDialog();
        dlgOpen.InitialDirectory = "c:\\";
        dlgOpen.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt|" + "RTF格式(*.RTF)|*.rtf";
        dlgOpen.FilterIndex = 1;
        if (dlgOpen.ShowDialog() == DialogResult.OK)
        {
            rtxtText.LoadFile(dlgOpen.FileName, RichTextBoxStreamType.PlainText);
        }
    }

    private void tsmiSave_Click(object sender, EventArgs e)
    {
        SaveFileDialog dlgSave = new SaveFileDialog();
        dlgSave.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt|" + "RTF格式(*.RTF)|*.rtf";
        dlgSave.FilterIndex = 2;
        if (dlgSave.ShowDialog() == DialogResult.OK)
        {
            rtxtText.SaveFile(dlgSave.FileName, RichTextBoxStreamType.PlainText);
        }
    }

    private void rtxtText_MouseMove(object sender, MouseEventArgs e)
    {
        tsslMousePosition.Text = "位置 X:" + e.X.ToString() + "Y:" + e.Y.ToString();
    }
}
第五步:注意要在主窗体FrmMain_Edit的MainMenuStrip属性中关联上menuStrip1控件;在RichTextBox控件的ContextMenuStrip属性中关联上contextMenuStrip1控件。

三.注意点

1.RichTextBox控件中文本编辑方法如下图所示:

2.下拉选项的实现方法
3.最终运行效果
 
;