/*
 * (1)图像列表ImageList主要用于存储图像:
 *     为工具栏中的按钮提供图像;
 *     提供列表视图中使用的大小图标;
 *     提供树形视图中使用的图像。
 * (2)运行阶段向TreeView控件添加节点;
 * (3)设计阶段向TreeView控件添加节点;
 * (4)ListView控件应用;
 * (5)TabControl应用。
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //在ImageList属性中单击Images属性,完成添加图片的添加或删除。
            //然后将控件与其相连,改变其ImageList属性。
            button1.ImageList = imageList1;
            button1.ImageIndex = 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //为两个ImagList控件添加图标
            largeimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\b.png"));
            largeimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\h.png"));
            largeimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\s.png"));
            smallimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\5.jpg"));
            smallimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\6.jpg"));
            smallimageList.Images.Add(Image.FromFile(Application.StartupPath + @"\7.jpg"));
            //将两个ImagList控件分别关联listView1的大、小图标
            listView1.LargeImageList = largeimageList;
            listView1.SmallImageList = smallimageList;
            //设置listView的标题栏名称
            listView1.Columns.Add("File", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Size", 50, HorizontalAlignment.Left);
            listView1.Columns.Add("data", 80, HorizontalAlignment.Left);
            //建立项目列表中的项目数据
            ListViewItem item1=new ListViewItem(new string[]{"Per.doc","235K","2008-1-1"},0);
            ListViewItem item2=new ListViewItem("Text.exe" , 1);
            ListViewItem item3 = new ListViewItem("cmd.ppt", 2);
            listView1.Items.Add(item1);
            listView1.Items.Add(item2);
            listView1.Items.Add(item3);
            //指定listView初始状态为详细列表
            listView1.View = View.Details;
            //为控件添加节点
            treeView1.ImageList = imageList1;
            TreeNode tn = new TreeNode();
            tn.Text = "出版社";
            tn.Name = "root";
            tn.ImageIndex = 0;
            treeView1.Nodes.Add(tn);    //添加根节点
            tn = new TreeNode();
            tn.Text = "清华大学出版社";
            tn.Name = "K1";
            tn.ImageIndex = 1;
            treeView1.Nodes["root"].Nodes.Add(tn);   //在根节点下添加节点
            tn = new TreeNode();
            tn.Text = "高校出版社";
            tn.Name = "K2";
            tn.ImageIndex = 2;
            treeView1.Nodes["root"].Nodes.Add(tn);   //在根节点下添加节点
            tn = new TreeNode();
            tn.Text = "电力出版社";
            tn.Name = "K3";
            tn.ImageIndex = 3;
            treeView1.Nodes["root"].Nodes.Add(tn);   //在根节点下添加节点
            tn = new TreeNode();
            tn.Text = "编辑部";
            tn.Name = "K4";
            tn.ImageIndex = 4;
            treeView1.Nodes["root"].Nodes["K3"].Nodes.Add(tn);   
            tn = new TreeNode();
            tn.Text = "发行部";
            tn.Name = "K5";
            tn.ImageIndex = 5;
            treeView1.Nodes["root"].Nodes["K3"].Nodes.Add(tn);
            treeView1.ExpandAll();    //将节点展开
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show(e.Node.Text);     //显示节点内容
        }

        private void largeBT_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void smallBT_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }

        private void listBT_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void detailBT_Click(object sender, EventArgs e)
        {
            listView1.View = View.Details;
        }

        private void titleBT_Click(object sender, EventArgs e)
        {
            listView1.View = View.Tile;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
                MessageBox.Show(item.Text);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!tabControl1.SelectedTab.Text.Equals("媒体信息")) 
            {
                return;
            }
            //其他操作
        }
        
    }
}

设计:

TreeNode编辑器:

TabPage集合编辑器:




运行结果:



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐