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 FormRadioButton : Form
{
public FormRadioButton()
{
InitializeComponent();
}
private void FormRadioButton_Load(object sender, EventArgs e)
{
radioButton2.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
MessageBox.Show("微型", "型号选择");
}
else if (radioButton2.Checked)
{
MessageBox.Show("小型", "型号选择");
}
else if (radioButton3.Checked)
{
MessageBox.Show("中型", "型号选择");
}
else if (radioButton4.Checked) {
MessageBox.Show("大型", "型号选择");
}
}
}
}
图片框用于显示图像,常用属性及方法:
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 FormPictureBox : Form
{
public FormPictureBox()
{
InitializeComponent();
}
public void showpicture()
{
//
pictureBox1.Image = Image.FromFile(System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal) + @"\1.png");
// 指定图片显示属性为StretchImage类型
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
else {
showpicture();
}
}
}
}
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 FormTabControl : Form
{
public FormTabControl()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = "当前选项卡为:第"+tabControl1.SelectedIndex.ToString() + "页,选项卡页为:"+
tabControl1.SelectedTab.Text+"选项卡总为:"+tabControl1.TabCount.ToString();
}
}
}
进度条控件是显示用户当前进程的控件:
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 FormProgressbar : Form
{
public FormProgressbar()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
progressBar1.Value++;
}
private void progressBar2_Click(object sender, EventArgs e)
{
progressBar1.Value++;
}
}
}
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 FormTrackBar : Form
{
public FormTrackBar()
{
InitializeComponent();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
label2.Text = trackBar2.Value.ToString();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
label1.Text = trackBar1.Value.ToString();
}
private void FormTrackBar_Load(object sender, EventArgs e)
{
trackBar1.Minimum = 0;
trackBar1.Maximum = 100;
trackBar1.SmallChange = 5;
trackBar1.TickFrequency = 10;
trackBar2.Minimum = 0;
trackBar2.Maximum = 100;
trackBar2.SmallChange = 1;
trackBar2.TickFrequency = 5;
}
}
}
位于Systems.Windows.Forms命名空间内的ImageList控件,主要用于缓存用户预定义好的图片列表信息,该控件不可以单独使用显示图片内容,必须附着在其他控件联合使用才可以显示图片内容。
ToolBar控件主要用于窗体的顶部工具栏的实现。
添加ToolBar来联合使用ImageList图像列表:
BindingSource=》选择项
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 FormImageList : Form
{
public FormImageList()
{
InitializeComponent();
}
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
}
private void FormImageList_Load(object sender, EventArgs e)
{
// 定义ToolBarButton对象
ToolBarButton btn1 = new ToolBarButton();
ToolBarButton btn2 = new ToolBarButton();
ToolBarButton btn3 = new ToolBarButton();
// 向当前的ToolBar控件添加工具栏按钮
toolBar1.Buttons.Add(btn1);
toolBar1.Buttons.Add(btn2);
toolBar1.Buttons.Add(btn3);
// 指定ToolBar控件的ImageList控件对象
toolBar1.ImageList = imageList1;
// 设置显示工具提示
toolBar1.ShowToolTips = true;
// 设置图片索引
btn1.ImageIndex = 0;
btn2.ImageIndex = 1;
btn3.ImageIndex = 2;
// 设置按钮的标题和提示信息
btn1.Text = "按钮1";
btn1.ToolTipText = "按钮提示1";
btn2.Text = "按钮2";
btn2.ToolTipText = "按钮提示2";
btn3.Text = "按钮3";
btn3.ToolTipText = "按钮提示3";
}
}
}
StatusStrip控件主要出现在窗体的底部,一般用于显示程序的当前状态信息。
StatusStrip控件允许添加包括:StatusLabel(标签控件)、ProgressBar(进度条)、DropDownButton(下拉按钮)和SplitButton(分割控件)等。
定时执行设定方法。
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 FormTimer : Form
{
public FormTimer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value += 1;
if (progressBar1.Value > 100) {
progressBar1.Value = 100;
}
}
}
}
ListView是以列表的方式显示数据内容,可以通过编辑列、组和项,添加行列数据信息。
ListView控件的编辑列、组和项的一般流程:
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 FormListView : Form
{
public FormListView()
{
InitializeComponent();
listView1.View = View.Details;
// 设置ListView对象的View属性值为Details
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem lst in listView1.SelectedItems)
{
MessageBox.Show(lst.Text);
}
}
private void FormListView_Load(object sender, EventArgs e)
{
ColumnHeader header1 = new ColumnHeader();
header1.Text = "职业";
header1.TextAlign = HorizontalAlignment.Center;
header1.Width = 50;
listView1.Columns.Add(header1);
ColumnHeader header2 = new ColumnHeader();
header2.Text = "兴趣";
header2.TextAlign = HorizontalAlignment.Center;
header2.Width = 50;
listView1.Columns.Add(header2);
listView1.Columns.Add("身高", 100, HorizontalAlignment.Center);
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Columns.Add("年龄", 100, HorizontalAlignment.Center);
listView1.Columns.Add("班级", 40, HorizontalAlignment.Center);
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Columns.Remove(listView1.Columns[0]);
}
private void button3_Click(object sender, EventArgs e)
{
listView1.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
}
private void button5_Click(object sender, EventArgs e)
{
listView1.BeginUpdate();
listView1.Items.Add("row1", "小李", 0);
listView1.Items["row1"].SubItems.Add("21");
listView1.Items["row1"].SubItems.Add("98524");
listView1.Items["row1"].SubItems.Add("男");
listView1.EndUpdate();
for (int i = 0; i < listView1.Items.Count; i++) {
if (i % 2 == 0) {
listView1.Items[i].BackColor = Color.Gray;
}
}
}
}
}
TreeView是以树型视图样式排列的对象。有两个重要的知识概念节点集和节点对象。TreeView控件的Nodes属性表示为TreeView控件指定的树节点集,而树节点集中的每个树节点对象可以包括它本身的树节点集,在树节点集中Add(),Remove()和RemoveAt()方法使开发人员可以添加和移动集中的单个树节点。
using System;
using System.CodeDom.Compiler;
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 FormTreeView : Form
{
public FormTreeView()
{
InitializeComponent();
}
private void FormTreeView_Load(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
TreeNode tem = new TreeNode("根节点");
treeView1.Nodes.Add(tem);
}
///
/// 添加子节点方法
///
private void AddChildNode()
{
// 首先判断是否选定组件中的位置
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox1.Text != "")
{
// 创建一个节点对象并初始化
TreeNode tmp;
tmp = new TreeNode(textBox1.Text);
// 在TreeView组件中加入子节点
treeView1.SelectedNode.Nodes.Add(tmp);
treeView1.SelectedNode = tmp;
treeView1.ExpandAll();
}
else
{
MessageBox.Show("请填写节点名称!", "信息提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
///
/// 添加兄弟节点方法
///
private void AddParent()
{
try
{
if (treeView1.SelectedNode == null)
{
MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (textBox1.Text != "")
{
// 创建一个节点对象并初始化
TreeNode tmp;
tmp = new TreeNode(textBox1.Text);
treeView1.SelectedNode.Parent.Nodes.Add(tmp);
treeView1.ExpandAll();
}
else
{
MessageBox.Show("请选择一个节点!", "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch
{
TreeNode tem = new TreeNode("根节点");
treeView1.Nodes.Add(tem);
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(this, new Point(e.X, e.Y));
}
}
private void button1_Click(object sender, EventArgs e)
{
treeView1.SelectedNode.Expand();
}
private void button2_Click(object sender, EventArgs e)
{
treeView1.SelectedNode = treeView1.Nodes[0];
treeView1.SelectedNode.ExpandAll();
}
private void button3_Click(object sender, EventArgs e)
{
treeView1.SelectedNode = treeView1.Nodes[0];
treeView1.SelectedNode.Collapse();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
AddChildNode();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
AddParent();
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode.Nodes.Count == 0)
{
treeView1.SelectedNode.Remove();
}
else
{
MessageBox.Show("请先删除此节点的所有子节点!", "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}