C#(二十九)之C#listBox checkedlistbox imagelist

简介: 本篇内容记录了listBox 、checkedlistbox、 imagelist的基本信息。

QQ图片20220426151230.jpg

listBox属性:


Items:所有选项

 

SelectionMode:是否支持多选


(1):one 只能选择一个

(2):Multisimple 可选择多个

(3):MultExtended 按组合键可多选

(4):none 不可选择

 

Multicolnum:

true:支持水平排列

false:不支持水平排列

 

SelectedIndex

listbox选项的索引是从0开始排列的。

 

Checklistbox


Items:所有选项

 

CheckOnClick

False:需要双击才能选中选项

True:单击便可选中选项

 

Imagelist 组件


Images:


组件中图片集合  使用数组表示


QQ图片20220426151232.jpg


测试使用全部代码


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 _0416Day
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void putButton_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if(index == 0)
            {
                result.Text = listBox1.SelectedItem.ToString();
            }
            else if(index == 1)
            {
                result.Text = listBox1.SelectedItem.ToString();
            }
            else if (index == 2)
            {
                result.Text = listBox1.SelectedItem.ToString();
            }
            else
            {
                MessageBox.Show("请选择选项");
            }
        }
        /**
         * 添加按钮
         */
        private void Add_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(Input.Text);
            Input.Clear();
        }
        /**
         * 移除按钮
         */
        private void Remove_Click(object sender, EventArgs e)
        {
            int sel = listBox1.SelectedIndex;
            if (sel != -1)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
            else
            {
                MessageBox.Show("请选择需要移除的选项");
            }
        }
        /**
         * 清空按钮
         */
        private void Clear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
        /**
         * 复选框默认事件
         * 选中文字打勾才会执行(在不修改CheckOnClick属性时需要双击才会打勾)
         */
        private void fubox_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        /**
         * 复选框默认事件
         * 选中文字(不打勾)就会执行
         */
        private void fubox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            // e是参数对象,等于被选中的那个值
            if (e.NewValue == CheckState.Checked)
            {
                // 选中的主键(选中之后显示图片)
                switch (e.Index)
                {
                    case 0: pictureBox1.Image = imageList1.Images[0]; break;
                    case 1: pictureBox2.Image = imageList1.Images[1]; break;
                    case 2: pictureBox3.Image = imageList1.Images[2]; break;
                    case 3: pictureBox4.Image = imageList1.Images[3]; break;
                }
            }
            else
            {
                // 未选中的主键(去掉选中之后图片消失)
                switch (e.Index)
                {
                    case 0: pictureBox1.Image = null; break;
                    case 1: pictureBox2.Image = null; break;
                    case 2: pictureBox3.Image = null; break;
                    case 3: pictureBox4.Image = null; break;
                }
            }
        }
    }
}


目录
相关文章
|
2月前
|
C# 数据库 虚拟化
43.c#:listbox控件
43.c#:listbox控件
16 1
|
存储 C# 图形学
C#编程-53:ImageList复习笔记
C#编程-53:ImageList复习笔记
117 0
|
数据库 C++ 索引
c#listbox使用详解和常见问题解决
c#listbox使用详解和常见问题解决
290 0
c#listbox使用详解和常见问题解决
|
C#
C# ListBox实现显示插入最新的数据的方法
原文:C# ListBox实现显示插入最新的数据的方法 在我们使用ListBox控件时,如果我们在里面不断的添加一条条数据,但是在我们添加的数据过多超过了ListBox显示的窗口时(此时会产生滑动条), 发现我们无法看到最新添加的数据。
1683 0