C# 第六章『交互式图形界面』◆第3节:Form窗体—简单例子(3)WebBrowser+ComboBox+Listbox

         一、公共控件——WebBrowser(浏览网页)

        二、公共控件——ComboBox(一个可编辑的文本框,下拉列表)

         SelectedIndexChanged:属性值更改时发生的事件。

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 _20220727_2
{
    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 >= 2000; i--)
            {
                comboBox1.Items.Add(i + "年");
            }
        }
        //当年份发生改变的时候,加载月份
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();//这里必须要先清空,否则月份会重复添加
            for (int i = 1; i <= 12; i++)
            {
                comboBox2.Items.Add(i + "月");
            }

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            comboBox3.Items.Clear();
            int day = 0;
            string strMonth = comboBox2.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
            string strYear = comboBox1.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
            //string[] strMonth = comboBox2.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries);
            int year = Convert.ToInt32(strYear);
            int month = Convert.ToInt32(strMonth);

            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: day = 31;
                    break;

                case 2:
                    if (year % 400 == 0)
                    //if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                    {
                        day = 29;
                    }
                    else
                    {
                        day = 28;
                    }
                    break;

                default: day = 30;
                    break;
            }
            for (int i = 1; i <= day; i++)
            {
                comboBox3.Items.Add(i + "日");
            }
        }
    }
}
 

        三、Listbox(可选择的列表)

        案例一:

        DoubleClick:双击组件时发生。

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

namespace _20220827_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] path = Directory.GetFiles(@"D:\Documents\Pictures\Wallpaper\3440x1440", "*.jpg");
            for (int i = 0; i < path.Length; i++)
            {
                string fileName = Path.GetFileName(path[i]);

                listBox1.Items.Add(fileName);

            }
        }
        string[] path = Directory.GetFiles(@"D:\Documents\Pictures\Wallpaper\3440x1440", "*.jpg");

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]);
        }
    }
}

        案例二:

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

namespace _20220727_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //存储音乐文件的全路径
        List<string> listSongs = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] path = Directory.GetFiles(@"d:/01", "*.wav");
            for(int i= 0; i<path.Length;i++)
            {
                string fileName = Path.GetFileName(path[i]);
                listBox1.Items.Add(fileName);
                //将音乐文件的全路径存到泛型集合中
                listSongs.Add(path[i]);
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation = listSongs[listBox1.SelectedIndex];
            sp.Play();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值