C#——Windows应用程序高级控件

1.ImageList控件(存储图像控件)

ImageList控件用于存储图像资源,然后在控件上显示出来,这样就简化了对图像的管理。ImageList控件的主要属性是Images,它包含关联控件将要使用的图片。

  • 在ImageList控件中添加图像

使用ImageList控件的Images属性的Add()方法,可以以编程的方式向ImageList控件中添加图像。

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置要加载的第一张图片的路径
            //取当前运行的exe所在目录的上两级目录的位置,比如说当前的程序的exe在c:\temp\abc\123\sample.exe
            //用下面的就会取到c:\temp
            //下面的语句相当于string Path = Application.StartupPath + @"\..\..";
            string Path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            //string Path = Application.StartupPath + @"\..\..";
            //@的意思是忽略字符中的转义符,比如有路径是c:\temp\123.txt,就不能直接写成string str = "c:\temp\123.txt";
            //这样编译时就会报错,因为它以为你是要转义,而不是要\这个字符,所以要写成string str = "c:\\temp\\123.txt";或是string str = @"c:\\temp\\123.txt";
            Path += @"\01.jpg";
            string Path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            Path2 += @"\02.jpg";
            Image One = Image.FromFile(Path, true);
            imageList1.Images.Add(One);
            Image Two = Image.FromFile(Path2, true);
            imageList1.Images.Add(Two);
            imageList1.ImageSize = new Size(250, 200);
            pictureBox1.Width = 250;
            pictureBox1.Height = 200;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //设置pictureBox1的图像索引是imageList1、控件索引为0的图片
            pictureBox1.Image = imageList1.Images[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //设置pictureBox1的图像索引是imageList1、控件索引为1的图片
            pictureBox1.Image = imageList1.Images[1];
        }
    }
}

  • 在ImageList控件中移除图像

可以使用RemoveAt()方法移除单个图像或可以使用Clear()方法清除图像列表中的所有图像。

RemoveAt()方法用于从列表中移除图像

public void RemoveAt(int index)

Clear()方法用于从ImageList中移除所有图像

2.ListView控件(列表视图控件)

ListView控件显示带图标的项的列表,可以显示大图标、小图标和数据。使用ListView控件可以创建类似Windows资源管理器右窗口的用户界面。

①添加项Add()

namespace ListView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "")

            {

                MessageBox.Show("项目不能为空");

            }

            else

            {

                //使用ListView控件的Items属性的Add()方法向控件中添加项

                listView1.Items.Add(textBox1.Text.Trim());

                textBox1.Clear();

            }

        }



        private void textBox1_TextChanged(object sender, EventArgs e)

        {



        }

    }

}

②移除项

RemoveAt()方法移除指定项,Clear()方法移除列表中的所有项。

namespace ListView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "")

            {

                MessageBox.Show("项目不能为空");

            }

            else

            {

                //使用ListView控件的Items属性的Add()方法向控件中添加项

                listView1.Items.Add(textBox1.Text.Trim());

                textBox1.Clear();

            }

        }



        private void textBox
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值