C# 记事本功能实现

一、文件

1.打开

        private void fOpen_Click(object sender, EventArgs e)

        {

            

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);

            }

            path = openFileDialog1.FileName;

            //该path保存打开文件的路径,方便后续操作

        }

2.保存/另存为

另存为:调出saveFileDialog1来进行该操作。

保存:若保存路径不为空(比如在目录下打开该文件path = openFileDialog1.FileName;),则默认保存在该路径下;若路径为空(新建文件),则执行另存为操作。

        private void mOpen_Click(object sender, EventArgs e)

        {

            if (path != null)

            {

                richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);

                MessageBox.Show("已保存");

            }

            else

            {

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                {

                    richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);

                }

                MessageBox.Show("已保存");

                path=saveFileDialog1.FileName;

            }

        }


3.页面设置

        private void Pagesetting_Click(object sender, EventArgs e)

        {

            pageSetupDialog1.Document = printDocument1;

            try

            {

                pageSetupDialog1.ShowDialog();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "打印出错");

            }

        }

4.打印

        private void fPrint_Click(object sender, EventArgs e)

        {

            if (printDialog1.ShowDialog() == DialogResult.OK)

            {

                try

                {

                    printDocument1.Print();

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }



            }

        }


 

二、编辑

1.richTextBox功能调用

        richTextBox1.Copy();

        richTextBox1.Cut();

        richTextBox1.Paste();

        richTextBox1.SetAll();

        richTextBox1.Undo();    

2.查找、查找下一个、替换、转到

 

 

 

 

三、格式

1.字体

        private void Font_Click(object sender, EventArgs e)

        {

            FontDialog fontDialog = new FontDialog();

            fontDialog.Color = richTextBox1.ForeColor;

            fontDialog.AllowScriptChange = true;

            fontDialog.ShowColor = true;

            if (fontDialog.ShowDialog() != DialogResult.Cancel)

            {

                richTextBox1.SelectionFont = fontDialog.Font;

                richTextBox1.SelectionColor = fontDialog.Color;

            }  

        }

2.combobox改变字体大小

        private void toolStripComboBox1_TextChanged(object sender, EventArgs e)

        {

            richTextBox1.SelectionFont = new Font(toolStripComboBox1.Text, Convert.ToSingle(toolStripComboBox1.Text));



        }


3.对齐格式

        richTextBox1.SelectionAlignment = HorizontalAlignment.Left; 

        richTextBox1.SelectionAlignment = HorizontalAlignment.Right;

        richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

 

 四、工具

1.字数统计

private void richTextBox1_TextChanged(object sender, EventArgs e)

        {

            char[] ch=richTextBox1.Text.Substring(0,richTextBox1.TextLength).ToCharArray();

            if (richTextBox1.Text != "")

            {

                for (int i = 0; i < richTextBox1.TextLength; i++)

                {

                    if (ch[i] >= 0 && ch[i] <= 127)

                    {

                        if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')

                        {

                            letters.Text = "字母:" + (++letter_sum);

                        }

                        else if (ch[i] == ' ') space.Text = "空格:" + (++space_sum);

                    }

                    else if (ch[i] >= 0x4e00 && ch[i] <= 0x9fbb) chinese.Text = "汉字:" + (++chinese_sum);

                }



                letter_sum = 0;

                space_sum = 0;

                chinese_sum = 0;

                total.Text = "总字符:" + richTextBox1.Text.Length.ToString();

                lines_sum = richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length) + 1;

                lines.Text = "行数:" + (lines_sum);

            }

            else

            {

                letters.Text = "0";

                space.Text = "0";

                chinese.Text = "0";

                total.Text = "0";

            }





               

        }

 

 

你可能感兴趣的:(记事本)