当前位置: 首页 > news >正文

C#实现多选下拉框

1、创建多选下拉框控件

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace DFT_FFTApp.userCtrl
{/// <summary>/// 多选下拉控件/// </summary>public class MultiComboBox:UserControl{public ComboBox ComboBox { get; set; }public CheckedListBox CheckedListBox { get; set; }public ComboBox.ObjectCollection Items{get{return ComboBox?.Items;}}public MultiComboBox(){//面板this.VerticalScroll.Enabled = true;this.AutoSize = true;//多项列表CheckedListBox = new CheckedListBox();CheckedListBox.CheckOnClick = true;CheckedListBox.BorderStyle = BorderStyle.Fixed3D;CheckedListBox.Visible = false;CheckedListBox.Margin=new Padding(0);CheckedListBox.MouseUp += (ss, se) =>{//更新ComboBox显示文本var list = new List<string>();foreach(var item in CheckedListBox.CheckedItems){list.Add(item.ToString());}ComboBox.Text = string.Join(",", list);ComboBox.Tag = list;};CheckedListBox.MouseLeave += (ss, se) =>{//隐藏多选框CheckedListBox.Hide();};//下拉框ComboBox=new ComboBox();ComboBox.Width = 150;ComboBox.DrawMode = DrawMode.OwnerDrawFixed;ComboBox.IntegralHeight = false;ComboBox.DroppedDown = false;ComboBox.DropDownHeight = 1;ComboBox.Margin=new Padding(0);ComboBox.Location=new System.Drawing.Point(0,0);ComboBox.DropDownStyle = ComboBoxStyle.DropDown;ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;ComboBox.MouseDown += (ss, se) =>{ComboBox.DroppedDown = false;};ComboBox.MouseLeave += (ss, se) =>{//不在下拉区时隐藏多项列表var curMousePos = this.PointToClient(Control.MousePosition);var downArea = CheckedListBox.Location;if (curMousePos.X < downArea.X || curMousePos.X>(downArea.X+CheckedListBox.Width)|| curMousePos.Y<downArea.Y || curMousePos.Y>(downArea.Y+CheckedListBox.Height)){CheckedListBox.Hide();}};ComboBox.DropDown += (ss, se) =>{//显示下拉多选框CheckedListBox.Items.Clear();//添加并设置选中项var lastChecked = ComboBox.Tag as List<string>;ComboBox.BeginUpdate();foreach(var item in this.Items){var ck = false;if(lastChecked!=null && lastChecked.Contains(item.ToString())){ck = true;}CheckedListBox.Items.Add(item, ck);}//显示下拉框CheckedListBox.Width=ComboBox.Width;CheckedListBox.ItemHeight = ComboBox.ItemHeight;CheckedListBox.Size=new Size(ComboBox.DropDownWidth,this.Items.Count*18);CheckedListBox.Location = new Point(ComboBox.Left, ComboBox.Height);this.Controls.Add( CheckedListBox );CheckedListBox.Visible = true;ComboBox.EndUpdate();};//添加控件this.Controls.Add(ComboBox);}}
}

2、程序调用

using DFT_FFTApp.userCtrl;
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 DFT_FFTApp
{public partial class MainForm : Form{MultiComboBox multiComboBox;public MainForm(){InitializeComponent();multiComboBox = new MultiComboBox();multiComboBox.Items.AddRange(new string[] { "AIN1", "AIN2", "AIN3", "AIN4" });multiComboBox.Location = new Point(175, 15);this.Controls.Add(multiComboBox);}private void Btn_ComboBoxTest_Click(object sender, EventArgs e){//显示选择项CheckedListBox CheckedListBox =multiComboBox.CheckedListBox;foreach(var item in CheckedListBox.CheckedItems){Console.WriteLine(item);}//显示选项列表//foreach (string item in multiComboBox.Items)//{//    Console.WriteLine(item);//}}}
}

3、运行结果
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 关于一些搜索的longchain实践
  • 20240820飞凌的OK3588-C的核心板在Linux R4下使用poweroff关机
  • usb无线网卡转typec口
  • [数据集][目标检测]夜间老鼠检测数据集VOC+YOLO格式316张1类别+视频文件1个
  • 省市区三级联动数据+PHP接口
  • 【C++备忘录】
  • 贪心+构造,CF 1592F1 - Alice and Recoloring 1
  • 每日一题-贪心算法
  • Vue状态管理工具:Pinia
  • 接口自动化-代码实现
  • SpringBoot 设置传入参数非必要
  • leetcode每日一题49
  • 微信小程序的四种弹窗使用
  • 【计算机操作系统】段页式管理方式
  • 【网络安全】IDOR之邮箱银行报价
  • 08.Android之View事件问题
  • 2017 年终总结 —— 在路上
  • javascript 哈希表
  • Javascript设计模式学习之Observer(观察者)模式
  • js递归,无限分级树形折叠菜单
  • Spring Boot MyBatis配置多种数据库
  • supervisor 永不挂掉的进程 安装以及使用
  • Vue 重置组件到初始状态
  • Vue小说阅读器(仿追书神器)
  • Zepto.js源码学习之二
  • 仿天猫超市收藏抛物线动画工具库
  • 每天10道Java面试题,跟我走,offer有!
  • 如何合理的规划jvm性能调优
  • 如何设计一个微型分布式架构?
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 使用common-codec进行md5加密
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • - 转 Ext2.0 form使用实例
  • 最简单的无缝轮播
  • Mac 上flink的安装与启动
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • $.ajax()
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (1)Nginx简介和安装教程
  • (C++)八皇后问题
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (简单) HDU 2612 Find a way,BFS。
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (区间dp) (经典例题) 石子合并
  • (三)c52学习之旅-点亮LED灯
  • (五)MySQL的备份及恢复
  • (一)UDP基本编程步骤
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法