博客
关于我
C#学习日记05———常用WinForm控件及类and简单记事本实战
阅读量:278 次
发布时间:2019-03-01

本文共 5359 字,大约阅读时间需要 17 分钟。

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
list = new List
(); 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); } }

转载地址:http://muvo.baihongyu.com/

你可能感兴趣的文章
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>