C#学习日记05———常用WinForm控件及类and简单记事本实战

1.TextBox控件:该控件可以输入信息
WordWrap:指示控件是否可以换行
PassWrodChar:使输入框显示一个单一文本(常用于密码栏)
事件:TextChanged当文本框内容改变时触发这一事件

2.Timer控件
每隔设定的时间就会触发事件(比如幻灯片)

3.focus:设置焦点

4.单选和多选控件:
多选控件:checkBox
单选控件:RadioButton
checked属性:指示这个控件是否被选中状态
默认状态下,在一个窗体中,所有的单选按钮只允许选中一个,可以使用groupbox分组来选择

5.webrowse:浏览器控件
uri:指向的网址 属性值中自动加https://,用代码赋值时,要手动加

6.ComboBox下拉框控件:
[外链图片转存失败,源站可能有防盗链机制,建议将在这里插描述]入!图片上https://传(imbQog.csdnimg.cn/2021022506099h.c//img-blog.csdnimg.cn/20210225092620995.png)]
DropDownStyle:设置下拉框格式

7.pictureBox控件
sizemode:设置图片格式:比如填充等
Image:图片指向的地址

8.listBox控件:
SelectedIndex:当前使用项的索引
在这里插入图片描述
9.MDI窗体设计:
首先确定一个父窗体 将IsMdiContaner设置为true
创建子窗体并将子窗体 fm2.MdiParent=this;

10.菜单控件MenuStrip

常用的类汇总:
1.Directory可以操作文件夹(创建对象去使用)
CreateDiretory 创建文件夹
Delete 删除文件夹
Move 剪切文件夹
Exist 判断是否存在
GetFiles 获得指定的目录下所有文件的全路径
GetDirectory 获得指定目录下所有文件夹的全路径

2.OpenfileDialog类(打开文件对话框)

            //点击弹出对话框
            OpenFileDialog ofd = new OpenFileDialog();
            //设置对话框的标题
            ofd.Title = "请选择要打开的文件";
            //设置多选
            ofd.Multiselect = true;
            //设置初始目录
            ofd.InitialDirectory = @"C:\Users\14505\Desktop";
            //设置打开文件框的文件类型
            ofd.Filter = "文本文件|*.txt|音乐文件|*.wav|所有文件|*.*";
            //展示对话框
            ofd.ShowDialog();
            //获得打开的文件的路径
            string str =ofd.FileName;
            using (FileStream fsRead = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                //实际读取到的字节数
                int r = fsRead.Read(buffer, 0, buffer.Length);
                textBox1.Text=  Encoding.Default.GetString(buffer, 0, r);
            }

3.SaveFileDialog类(保存对话框)
和上边使用代码基本一样

4.FondDialog字体对话框
ColorDialog 颜色对话框
直接FondDialog flg = new FondDiaLog();
flg.Show();
textbox1.Text.Fond=flg.Fond;就可以了

实战之简单记事本小程序:
首先添加菜单,将Form设计成这个样子
在这里插入图片描述
左侧是listBox 最下面是TextBox ListBox和一个按钮处于Panel容器中
文件菜单中有打开和保存
格式中有:自动换行
样式中有:字体、颜色
当我们点击《按钮时,要让listBox消失,这时候可以把panel的Visable设置成false
全代码:

List<string> list = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //加载程序时,隐藏panel
            panel1.Visible = false;
            //自动换行关闭
            textBox1.WordWrap = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //点击按钮时,同样要隐藏
            panel1.Visible = false;
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要打开的文本文件";
            ofd.InitialDirectory = @"C:\Users\14505\Desktop";
            ofd.Multiselect = true;
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            ofd.ShowDialog();

            //获得用户选择的文件的路径
            string input = ofd.FileName;
            //将文件的全路径存储到泛型集合当中
            list.Add(input);

            if (input == "")
            {
                return;
            }

            using (FileStream fsRead = new FileStream(input, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r = fsRead.Read(buffer, 0, buffer.Length);
                textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
            }



        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FileStream fswrite = new FileStream(@"C:\Users\14505\Desktop\new1.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer = Encoding.Default.GetBytes(textBox1.Text.Trim());
                fswrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功!");
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行ToolStripMenuItem.Text == "☆自动换行")
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Text = "★取消自动换行";
            }
            else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
            {
                textBox1.WordWrap = false;
                自动换行ToolStripMenuItem.Text = "☆自动换行";
            }

        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            textBox1.Font = fd.Font;
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            textBox1.ForeColor = cd.Color;

        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //要获得双击文件对应的全路径
            string path = list[listBox1.SelectedIndex];
            using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r = fsRead.Read(buffer, 0, buffer.Length);
                textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值