【ImageList控件】(存储图像控件)
用于存储图像资源。
1.在ImageList控件中添加图像
使用ImageList控件的Images属性的Add方法。
private void Form1_Load(object sender, EventArgs e)
{
string Path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
Path += @"\01.jpg";
string Path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
Path2 += @"\02.jpg";
Image Mimg=Image.FromFile(Path,true);
imageList1.Images.Add(Mimg);
Image Mimg2 = Image.FromFile(Path2, true);
imageList1.Images.Add(Mimg2);
imageList1.ImageSize = new Size(200,165);
pictureBox1.Width = 200;
pictureBox1.Height = 165;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[0];
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[1];
}
2.在ImageList控件中移除图像
Images属性的RemoveAt方法移除指定图像,Clear方法清空所有图像。
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Width = 200;
pictureBox1.Height = 165;
string Path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
Path += @"\01.jpg";
Image img = Image.FromFile(Path, true);
imageList1.Images.Add(img);
imageList1.ImageSize = new Size(200,165);
}
private void button1_Click(object sender, EventArgs e)
{
if (imageList1.Images.Count == 0)
{
MessageBox.Show("没有图像");
}
else
{
pictureBox1.Image = imageList1.Images[0];
}
}
private void button2_Click(object sender, EventArgs e)
{
imageList1.Images.RemoveAt(0);
pictureBox1.Image = null;
}
【ListView控件】(列表视图控件)
ListView控件显示带图标的项的列表。通过View属性设置项在控件中显示的方式。
1.在ListView控件中添加和移除项
1)添加项
使用ListView控件的Items属性的Add方法。
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("项目不能为空");
}
else
{
listView1.Items.Add(textBox1.Text.Trim());
}
}
2)移除项
Items属性的RemoveAt方法移除指定项,Clear方法清空所有项。
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("项目不能为空");
}
else
{
listView1.Items.Add(textBox1.Text.Trim());
textBox1.Text = "";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (listView1.Items.Count == 0)
{
MessageBox.Show("项目中已经没有项目");
}
else
{
listView1.Items.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择要删除的项");
}
else
{
listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);
listView1.SelectedItems.Clear();
}
}
2.选择ListView控件中的项
通过控件的Selected属性。
private void Form1_Load(object sender, EventArgs e)
{
listView1.Items.Add("用一生下载你");
listView1.Items.Add("芸烨湘枫");
listView1.Items.Add("一生所爱");
listView1.Items[2].Selected = true;//选中第3项
}
3.为ListView控件中的项添加图标
使用ImageList控件设置ListView控件中项的图标。
1)设置ListView控件属性(SmallImageList、LargeImageList、StateImageList)
2)设置ImageIndex或者StateImageIndex属性。
private void Form1_Load(object sender, EventArgs e)
{
listView1.LargeImageList = imageList1;//设置属性
imageList1.ImageSize = new Size(37,36);
imageList1.Images.Add(Image.FromFile("01.png"));
imageList1.Images.Add(Image.FromFile("02.png"));
listView1.SmallImageList = imageList1;//设置属性
listView1.Items.Add("用一生下载你");
listView1.Items.Add("芸烨湘枫");
listView1.Items[0].ImageIndex = 0;
listView1.Items[1].ImageIndex = 1;
}
4.在ListView控件中启用平铺视图
将View属性设置为Tile。可以通过TileSize属性调整大小。
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Tile;
listView1.LargeImageList = imageList1;
imageList1.Images.Add(Image.FromFile("1.bmp"));
imageList1.Images.Add(Image.FromFile("2.bmp"));
listView1.Items.Add("用一生下载你");
listView1.Items.Add("一生所爱");
listView1.Items.Add("用一生下载你");
listView1.Items.Add("一生所爱");
listView1.Items.Add("用一生下载你");
listView1.Items[0].ImageIndex = 0;
listView1.Items[1].ImageIndex = 1;
listView1.Items[2].ImageIndex = 0;
listView1.Items[3].ImageIndex = 1;
listView1.Items[4].ImageIndex = 0;
listView1.TileSize = new Size(100, 50);
}
5.为ListView控件中的项分组
1)添加组
使用Group集合的Add方法将指定的ListViewGroup添加到集合。
2)移除组
使用Group集合的RemoveAt方法移除指定组,Clear方法清空所有组。
listView1.Groups.RemoveAt(1);//移除索引为1的组
listView1.Groups.Clear();
3)向组分配项或在组之间移动项
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.SmallIcon;//设置样式
//建立两个组
listView1.Groups.Add(new ListViewGroup("名称",HorizontalAlignment.Left));
listView1.Groups.Add(new ListViewGroup("年龄", HorizontalAlignment.Left));
listView1.Items.Add("用一生下载你");
listView1.Items.Add("芸烨湘枫");
listView1.Items.Add("一生所爱");
listView1.Items.Add("28");
listView1.Items.Add("27");
listView1.Items.Add("26");
//将1,2,3项添加到1组
listView1.Items[0].Group = listView1.Groups[0];
listView1.Items[1].Group = listView1.Groups[0];
listView1.Items[2].Group = listView1.Groups[0];
//将4,5,6项添加到2组
listView1.Items[3].Group = listView1.Groups[1];
listView1.Items[4].Group = listView1.Groups[1];
listView1.Items[5].Group = listView1.Groups[1];
}
【TreeView控件】(树控件)
1.添加和删除树节点
1)添加节点
使用TreeView控件的Nodes属性的Add方法
private void Form1_Load(object sender, EventArgs e)
{
//建立3个父节点
TreeNode tn1 = treeView1.Nodes.Add("名称");
TreeNode tn2 = treeView1.Nodes.Add("性别");
TreeNode tn3 = treeView1.Nodes.Add("年龄");
//建立3个子节点
TreeNode Ntn1 = new TreeNode("用一生下载你");
TreeNode Ntn2 = new TreeNode("芸烨湘枫");
TreeNode Ntn3 = new TreeNode("一生所爱");
//将3个子节点添加到第1个父节点
tn1.Nodes.Add(Ntn1);
tn1.Nodes.Add(Ntn2);
tn1.Nodes.Add(Ntn3);
TreeNode Stn1 = new TreeNode("男");
TreeNode Stn2 = new TreeNode("女");
TreeNode Stn3 = new TreeNode("男");
tn2.Nodes.Add(Stn1);
tn2.Nodes.Add(Stn2);
tn2.Nodes.Add(Stn3);
TreeNode Atn1 = new TreeNode("28");
TreeNode Atn2 = new TreeNode("27");
TreeNode Atn3 = new TreeNode("26");
tn3.Nodes.Add(Atn1);
tn3.Nodes.Add(Atn2);
tn3.Nodes.Add(Atn3);
}
2)移除节点
使用TreeView控件的Nodes属性的Remove方法。
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn1 = treeView1.Nodes.Add("名称");
TreeNode Ntn1 = new TreeNode("用一生下载你");
TreeNode Ntn2 = new TreeNode("芸烨湘枫");
TreeNode Ntn3 = new TreeNode("一生所爱");
tn1.Nodes.Add(Ntn1);
tn1.Nodes.Add(Ntn2);
tn1.Nodes.Add(Ntn3);
}
private void button1_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode.Text == "名称")
{
MessageBox.Show("请选择要删除的子节点");
}
else
{
treeView1.Nodes.Remove(treeView1.SelectedNode);
}
}
2.获取树控件中选中的节点
在控件的AfterSelect事件中,使用EventArgs对象返回对已单击对象的引用。
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn1 = treeView1.Nodes.Add("名称");
TreeNode Ntn1 = new TreeNode("用一生下载你");
TreeNode Ntn2 = new TreeNode("芸烨湘枫");
TreeNode Ntn3 = new TreeNode("一生所爱");
tn1.Nodes.Add(Ntn1);
tn1.Nodes.Add(Ntn2);
tn1.Nodes.Add(Ntn3);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
label1.Text = "当前选中的节点:" + e.Node.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn1 = treeView1.Nodes.Add("组织结构");
TreeNode Ntn1 = new TreeNode("C#部门");
TreeNode Ntn2 = new TreeNode("ASP.NET部门");
TreeNode Ntn3 = new TreeNode("VB部门");
tn1.Nodes.Add(Ntn1);
tn1.Nodes.Add(Ntn2);
tn1.Nodes.Add(Ntn3);
imageList1.Images.Add(Image.FromFile("1.png"));
imageList1.Images.Add(Image.FromFile("2.png"));
//设置treeView1的ImageList属性为imageList1
treeView1.ImageList = imageList1;
imageList1.ImageSize = new Size(16,16);
//设置节点的图标
treeView1.ImageIndex = 0;
//设置选中节点的图标
treeView1.SelectedImageIndex = 1;
}
【DateTimePicker控件】(日期控件)
1.使用DateTimePicker控件显示时间
通过将控件的Format属性设置为Time,实现控件只显示时间。
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Time;
textBox1.Text = dateTimePicker1.Text;
}
2.使用DateTimePicker控件以自定义格式显示日期
通过控件的CustomFormat属性
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
label1.Text = dateTimePicker1.Text;
}
3.返回DateTimePicker控件中选择的日期
Value属性
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = dateTimePicker1.Text;
textBox2.Text = dateTimePicker1.Value.Year.ToString();
textBox3.Text = dateTimePicker1.Value.Month.ToString();
textBox4.Text = dateTimePicker1.Value.Day.ToString();
}
【MonthCalendar控件】(月历控件)
1.更改MonthCalendar控件的外观
1)更改月历的配色方案
private void Form1_Load(object sender, EventArgs e)
{
//设置标题背景为蓝色
monthCalendar1.TitleBackColor = System.Drawing.Color.Blue;
//设置控件其他日期的颜色为红色
monthCalendar1.TrailingForeColor = System.Drawing.Color.Red;
//标题上的文字为黄色
monthCalendar1.TitleForeColor = System.Drawing.Color.Yellow;
}
2)显示周数
将ShowWeekNumbers属性设置为true就可以了。
private void Form1_Load(object sender, EventArgs e)
{
monthCalendar1.ShowWeekNumbers = true;
}
private void Form1_Load(object sender, EventArgs e)
{
//设置CalendarDimensions属性,在水平和垂直方向都显示2个月份
monthCalendar1.CalendarDimensions = new Size(2, 2);
}
private void Form1_Load(object sender, EventArgs e)
{
DateTime myVacation1 = new DateTime(2008, 3, 2);
monthCalendar1.AddBoldedDate(myVacation1);
monthCalendar1.UpdateBoldedDates();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = monthCalendar1.TodayDate.ToString();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
textBox2.Text = monthCalendar1.SelectionStart.ToString();
textBox3.Text = monthCalendar1.SelectionEnd.ToString();
}
【其他高级控件】
1.使用ErrorProvider控件验证文本框输入
private int a,b,c;
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox1, "不能为空");
}
else
{
errorProvider1.SetError(textBox1,"");
a = 1;
}
}
private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox2.Text == "")
{
errorProvider2.SetError(textBox2, "不能为空");
}
else
{
try
{
int x = Int32.Parse(textBox2.Text);
errorProvider2.SetError(textBox2,"");
b = 1;
}
catch
{
errorProvider2.SetError(textBox2, "请输入一个数");
}
}
}
private void textBox3_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox3.Text == "")
{
errorProvider3.SetError(textBox3, "不能为空");
}
else
{
errorProvider3.SetError(textBox3, "");
c = 1;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (a + b + c == 3)
{
MessageBox.Show("数据录入成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
2.使用HelpProvider控件调用帮助文件
通过控件的Helpamespace属性和SetShowHelp方法。
private void Form1_Load(object sender, EventArgs e)
{
//设置文件位置
string strPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
strPath += @"\helpPage.htm";
helpProvider1.HelpNamespace = strPath;
MessageBox.Show(strPath);
helpProvider1.SetShowHelp(this,true);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;//设置Interval属性1000毫秒
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "开始")
{
timer1.Enabled = true;//启动timer控件
button1.Text ="停止";
}
else
{
timer1.Enabled = false;//停止timer控件
button1.Text = "开始";
}
}
4.使用ProgressBar控件显示程序运行进度条
控件比较重要的属性:Value:表示已完成的进度;Minimum:进度条的最小值;Maximum:进度条的最大值。
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
progressBar1.Step = 1;//设置增值为1
for (int i = 0; i <5000; i++)
{
progressBar1.PerformStep();//使用PerformStep方法按step的值递增
textBox1.Text = "进度值:" + progressBar1.Value.ToString();
}
}