zoukankan      html  css  js  c++  java
  • 记事本制作

    记事本

    对话框控件:  

      不显示需要在双击菜单栏设置显示   

        ColorDialog :颜色对话框  

        FontDialog:字体对话框

        FloderBrowserDialog:文件夹选择对话框  

        OpenFileDialog:打开对话框  

         SabeFileDialog:保存对话框

    文件流

      using System.IO;

    打印控件:  

      PageSetupDialog:更改有页面相关的设置  

      PrintDialog:选择打印机并选择其他打印方式

      PrintDocument:想打印机输出打印对象

      PrintPreviewControl:只表示打印预览中显示正在预览的文档部分,不包含任何对话框和按钮  

      PrintPreviewDialog:向用户显示关联文档打印时的样子

    Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBox1.ScrollBars = ScrollBars.Vertical;
            }
    
            private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                DialogResult dr = colorDialog1.ShowDialog();//点击字体颜色按钮显示颜色对话框并返回给DialogResult dr
    
                if (dr == DialogResult.OK)//如果用户字体颜色对话框的确定
                {
                    textBox1.ForeColor = colorDialog1.Color;//更改textBox1的字体颜色改变全部颜色不能改一部分
                    textBox1.BackColor = colorDialog1.Color;//更改背景颜色
                }
    
    
            }
    
            private void 文件夹选择ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //现在底部弄一个 StatusStrip然后StatusLabel清空掉Text
                DialogResult dr = folderBrowserDialog1.ShowDialog();
                //看不见任何一个文件,只能看见文件夹
                if (dr == DialogResult.OK)
                {
                    la1.Text = folderBrowserDialog1.SelectedPath;//在底部的StatusStrip中StatusLabel显示出选择的文件的绝对路径
                }
    
            }
    
            private void 字体选择ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                DialogResult dr = fontDialog1.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    textBox1.Font = fontDialog1.Font;
                    textBox1.ForeColor = fontDialog1.Color;//字体对话框中没有颜色需要先右键点击fontDialog1选择属性中的ShowColor点true需要不能用需要设置因为他是字体对话框里的颜色
                }
    
    
            }
    
            private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "文本文件|*.txt";//Filter查看文件的格式只能看到txt的文件格式也只能选择txt文件
                DialogResult dr = openFileDialog1.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    string path = openFileDialog1.FileName;//获取绝对路径
                    StreamReader sr = new StreamReader(path, Encoding.Default);//这就话之前需要引用流using System.IO。Encoding.Default(获取当前操做系统默认的编码(系统用什么编码你用什么编码) )读取汉字
                    //创建流吧获取的绝对路径给sr。选定的流就会针对于你选择的路径
                    textBox1.Text = sr.ReadToEnd();//读取sr的路径从头读到尾然后将读到的内容返回到text.Box1中显示出来。只能读取英文读取汉字在 StreamReader sr = new StreamReader(path, Encoding.Default)
                }
            }
    
            bool hasave = false;
            string savepath = "";
            private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (savepath == "")
                {
    
                    saveFileDialog1.Filter = "文本文件|*.txt";
                    saveFileDialog1.FileName = "新建文本文档";//没保存默认标题
                    DialogResult dr = saveFileDialog1.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        savepath = saveFileDialog1.FileName;//绝对路径
                        la1.Text = savepath;
    
                        string[] sss = savepath.Split('\');
                        this.Text = sss[sss.Length - 1] + " - 超级记事本";//截取路径最后
    
                        StreamWriter sw = new StreamWriter(savepath);//StreamWriter输出  对他进行保存
                        sw.Write(textBox1.Text);//将text.Box1写道流里
                        sw.Close();//关掉流
                        hasave = true;
                    }
                }
                else//没有记录直接写
                {
                    StreamWriter sw = new StreamWriter(savepath);
                    sw.Write(textBox1.Text);
                    sw.Close();
                    hasave = true;//保存完成
                }
    
            }
    
            private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (hasave)
                {
                    textBox1.Text = "";
                    savepath = "";
                    this.Text = "超级记事本";
                }
                else
                {
                    DialogResult dr = MessageBox.Show("当前文件未保存,新建将~~~~", "警告!", MessageBoxButtons.OKCancel);
                    if (dr == DialogResult.OK)
                    {
                        textBox1.Text = "";
                        savepath = "";
                        this.Text = "超级记事本";
    
                    }
    
                }
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                la2.Text = textBox1.Text.Length.ToString();
                hasave = false;//保存好了后更改内容提示是否保存
            }
    
            private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //不自动换行显示滚动条
                if (textBox1.WordWrap)
                {
                    textBox1.WordWrap = false;
                    自动换行ToolStripMenuItem.Checked = false;
                    textBox1.ScrollBars = ScrollBars.Both;
                }
                else
                {
                    textBox1.WordWrap = true;
                    自动换行ToolStripMenuItem.Checked = true;
                    textBox1.ScrollBars = ScrollBars.Vertical;
                }
            }
    
            private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                pageSetupDialog1.Document = printDocument1;
                pageSetupDialog1.ShowDialog();
            }
            //下面必用打印对象
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Brush b = new SolidBrush(textBox1.ForeColor);//实线画刷(需要颜色对象来自于textBox1.ForeColor颜色)
                PointF p = new PointF(30,10);//距离x轴30y轴10
    
                e.Graphics.DrawString(textBox1.Text, textBox1.Font, b, p);//绘制一些字符串(要绘制的东西,绘制成什么样,画刷,位置)
            }
    
            private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
            {
              //PrintPreviewControl1.Document = printDocument1;
                printPreviewDialog1.Document = printDocument1;//独立的
                printPreviewDialog1.ShowDialog();
            }
    
            private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                printDialog1.Document = printDocument1;
                DialogResult dr = printDialog1.ShowDialog();//接受一下返回值
    
                if (dr == DialogResult.OK)
                {
                    printDocument1.Print();//执行打印
                
                }
    
            }
    
            private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Undo();//撤销
            }
    
            private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Cut();//剪贴
            }
    
            private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Copy();//复制
            }
    
            private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Paste();//粘贴
            }
    
            private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.SelectAll();//全选
            }
    
            private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2(this);//this传给Form2
                f2.Owner = this;//拥有者form1拥有form2
                f2.Show();
            }
        }
    }

    Form2查找的窗口

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            Form1 F1;//提升作用域
            public Form2(Form1 f1)
            {
                InitializeComponent();
                F1 = f1;
                //将文本框的privat改成public
            }
    
            int a = -1;//加载一次使用
            private void button1_Click(object sender, EventArgs e)
            {
                string s1 = textBox1.Text;//获取要查找的数据
                string s2 = F1.textBox1.Text;//要查的地方
    
                a = s2.IndexOf(s1, (a + s1.Length));//多少a开始往后找
    
                if (a < 0)
                {
                    MessageBox.Show("找不到"" + s1 + ""!!");//   ""转义字符
                }
                else
                {
                    F1.textBox1.Select(a, s1.Length);//设置选定的内容(从哪个索引开始选,选中多长)  
                    F1.textBox1.Focus();//获得焦点
                }
    
    
            }
        }
    }

     

  • 相关阅读:
    @Value注解无法为static 变量赋值
    CloseableHttpClient方式配置代理服务器访问外网
    微信小程序上传图片
    centos7 无界面静默安装 oracle
    Linux安装配置JDK
    java导出Excel定义导出模板
    ECharts柱状图
    Java获取客户端真实IP
    Eclipse控制台输出log日志中文乱码
    $_SRRVER常用用法
  • 原文地址:https://www.cnblogs.com/skyhorseyk/p/7192287.html
Copyright © 2011-2022 走看看