一、公共控件——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();
}
}
}