C#WinForm开发笔记——基本控件(二)

本文详细介绍了C# WinForm中WebBrowser、ComboBox、ListBox、Panel和DataGridView等基本控件的属性和事件,并探讨了如何使用OpenFileDialog、SaveFileDialog等对话框。此外,还提供了一系列的应用案例,包括浏览器、日期选择器、图片查看器和简单的音乐播放器等。
摘要由CSDN通过智能技术生成

一、控件

1、WebBrowser

  • 浏览器控件
    在这里插入图片描述

1>属性

  • Url:导航的网址(在属性栏输入时会自动补全http://)
    在这里插入图片描述

2、ComboBox

  • 下拉框控件:建议以cbo…格式命名
    在这里插入图片描述

1>属性

  • Items:下拉框中的显示内容(可在属性栏中的编辑项、Items以及点击控件上的小三角进行输入)
    在这里插入图片描述
  • DropDownStyle:控制下拉框的外观格式
    在这里插入图片描述
    在这里插入图片描述

2>事件

  • SelectedIndexChanged:当前下拉框中的值被选中的时候发生事件
    在这里插入图片描述

3、ListBox

  • 集合:类似于音乐播放的菜单栏,属性与事件与ComboBox类似
    在这里插入图片描述

1>属性

  • Items:下拉框中的显示内容(可在属性栏中的编辑项、Items以及点击控件上的小三角进行输入)
    在这里插入图片描述

2>事件

  • DoubleClick:双击时触发事件
    在这里插入图片描述

4、Panel

  • 容器:与GroupBox类似
    在这里插入图片描述

5、DataGridView

  • 用于显示表格数据
    在这里插入图片描述
  • 更改中文表名并绑定相关数据列的操作
    在这里插入图片描述
    在这里插入图片描述

二、对话框

  • OpenFileDialog:打开文件对话框
  • SaveFileDialog:保存文件对话框
  • FontDialog:字体对话框
  • ColorDialog:颜色对话框

1、打开文件对话框

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 文本框
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   
            //点击弹出对话框
            OpenFileDialog ofd = new OpenFileDialog();
            //设置对话框标题
            ofd.Title = "请选择需要打开的文档";
            //设置对话框可以多选
            ofd.Multiselect = true;
            //设置对话框的初始目录
            ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
            //设置对话框的文件类型
            ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|图片文件|*.jpg|所有文件|*.*";
            
            //展示对话框
            ofd.ShowDialog();


            //获得 在打开对话框中  被选中文件的全路径
            string path = ofd.FileName;
            if (path == "")
                return;
            using (FileStream fsRead = new FileStream(path,FileMode.Open,FileAccess.Read))
            {
   
                //创建缓冲区
                byte[] buffer =new byte[1024 * 1024 * 5];
                int r = fsRead.Read(buffer, 0, buffer.Length);

                string str = Encoding.Default.GetString(buffer, 0, r);

                textBox1.WordWrap = true;
                textBox1.Text = str;
            }

        }
    }
}

2、保存文件对话框

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace 保存文件对话框
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择要保存的路径";
            sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";

            sfd.ShowDialog();

            string path = sfd.FileName;
            if (path == "")
                return;
            using(FileStream fsWrite = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
            {
   
                byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }

            MessageBox.Show("保存成功");
        }
    }
}

3、字体和颜色对话框

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 字体和颜色对话框
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private void bntFont_Click(object sender, EventArgs e)
        {
   
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            //将字体对话框选中的字体给到文本框中
            textBox1.Font = fd.Font;
        }

        private void bntColor_Click(object sender, EventArgs e)
        {
   
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            //将颜色对话框选中的字体给到文本框中
            textBox1.ForeColor = cd.Color;
        }
    }
}

三、案例代码

1、浏览器

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 浏览器控件
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   
            textBox1.Focus();
            string text = textBox1.Text;
            webBrowser1.Url = new Uri("http://"+text);
        }
    }
}

2、日期选择器

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 日期选择器
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
   
            //程序加载的时候,将年份添加到下拉框中
            //获得当前的年份
            int year = DateTime.Now.Year;

            for (int i = year; i >= 1949; i--)
            {
   
                cboYear.Items.Add(i +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聪 ~smart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值