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

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

C# Windows Forms 常用控件与实战应用

在C# Windows Forms开发中,控件是构建用户界面的核心元素。本文将详细介绍常用控件的使用方法,并结合实战案例说明如何高效完成日常开发任务。

1. 常用控件概述

1.1 TextBox控件

TextBox允许用户输入信息,具有灵活性和丰富的属性。以下是其关键属性:

  • WordWrap:指示是否支持文本换行。
  • PassWordChar:用于密码输入,隐藏字符。
  • TextChanged事件:当内容发生变化时触发。

1.2 Timer控件

Timer用于定时操作,如幻灯片展示。其主要属性包括Interval(定时间隔)和Enabled(是否启用)。

1.3 Focus控件

Focus用于管理窗口焦点,支持设置默认焦点和焦点遍历。

1.4 单选和多选控件

  • CheckBox:多选按钮,适合复杂选择。
  • RadioButton:单选按钮,默认状态下不互斥。
  • Checked属性:判断控件是否选中。
  • GroupBox:用于分组单选按钮,确保单选。

1.5 WebBrowse控件

用于嵌入浏览功能,支持加载URL。其关键属性包括Uri(目标URL)和NavigationStyle(导航模式)。

1.6 ComboBox控件

下拉框控件,支持自定义样式。属性包括DropDownStyle(下拉框风格)。

1.7 PictureBox控件

用于显示图片,支持设置图片格式和源。属性包括SizeMode(图片模式)和Image(图片路径)。

1.8 ListBox控件

支持多选和单选模式,属性包括SelectedIndex(选中项索引)。

2. 窗口设计与菜单

2.1 MDI窗体设计

  • IsMdiContainer属性:设置父窗体为MDI容器。
  • 子窗体管理:使用MdiParent属性将子窗体添加到父窗体。

2.2 MenuStrip控件

创建菜单,支持上下文菜单。常用方法包括添加菜单项和处理点击事件。

3. 文件对话框

3.1 OpenFileDialog类

  • 使用场景:打开文件对话框。
  • 方法示例:
    OpenFileDialog ofd = new OpenFileDialog();ofd.Title = "请选择要打开的文件";ofd.InitialDirectory = @"C:\Users\14505\Desktop";ofd.Multiselect = true;ofd.Filter = "文本文件|*.txt|所有文件|*.*";ofd.ShowDialog();string str = ofd.FileName;
  • 读取文件内容:使用FileStream读取文件内容并显示。

3.2 SaveFileDialog类

与OpenFileDialog类似,用于保存文件。

3.3 ColorDialog类

用于选择颜色,直接调用并获取颜色值。

4. 实战之记事本小程序

4.1 设计思路

  • 窗口布局:Panel容器嵌入TextBox、ListBox和按钮。
  • 菜单功能:新增、打开、保存、格式设置。
  • 控制事件:按钮点击隐藏或显示Panel。

4.2 核心代码

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

5. 开发总结

通过以上内容可以看出,C# Windows Forms开发需要对各类控件的理解和灵活运用。通过合理布局和事件处理,可以快速完成实用的小程序开发。

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

你可能感兴趣的文章
python+flask计算机毕业设计高校选课系统(程序+开题+论文)
查看>>
Python+Jenkins+Allure Report接口自动化测试持续集成
查看>>
python+locust电商全流程性能测试
查看>>
Python函数运行的可执行文件的终端输出如何以一般方式静音?
查看>>
Python+Pytest+Allure+Git+Jenkins接口自动化框架
查看>>
python+pytest接口自动化 —— 参数关联
查看>>
python+pytest接口自动化 —— 参数关联
查看>>
Python+pytest接口自动化 —— 接口测试基础
查看>>
python+pytest接口自动化 —— 自动化用例编写思路 (使用pytest编写一个测试脚本)
查看>>
Python+pytest接口自动化之cookie绕过登录(保持登录状态)
查看>>
python+pytest接口自动化:接口测试
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
python+requests+unittest执行自动化接口测试!
查看>>
Python+Requests编码识别Bug
查看>>
python函数编写_[零基础学python]传说中的函数编写条规
查看>>
Python+selenium —— UI自动化之鼠标操作
查看>>
python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
查看>>
Python+Selenium+Threading进行兼容性测试
查看>>
Python+selenium+unittest的GUI自动化框架实现
查看>>