combobox实现下拉多选的终结解决方案
2009-07-05 15:22
337 查看
前段时间自己在公司负责将web下的一些自己的控件转换为 winform模式下。其实对于C/S的开发自己不是很熟悉,但是新员工嘛,没办法,硬着头皮扛下了。接下来就是不断去学习和研究,今天在做多选下拉菜单时,遇到了一点难度,网上搜索了下,解决方案很多,大多在combobox里面家checkbox,但这样对用户的体验不是很好,因为用户可能需要选择连续的很多选项,这时就需要一个一个的点,不够人性化。思考之后,我觉得ListBox里面的MultiExtended蛮适合的。于是利用午后时间,做了demo,结合textbox一起使用,效果不错。废话先不多说了,来张截图先吧。
主要功能,用户在使用控件的时候只能看到一个textbox,点击输入框后出现选项菜单,然后选中值后,输入框会自动添加好值,点击旁边的按钮,选项框消失。这边有个注意点,就是要让控件的背景随选择菜单的出现变大和变小。
代码部分:
总结:
1.使用Item实现了对ListBox的动态赋值。
2.在设计模式下,拉动修改自定义控件的大小,自动修改自定义空间内部控件的大小。
3.自定义控件的大小随多选菜单展开、放下自动变化大小,从而不影响别的控件使用。
这只是一个demo,真的要使用还有很多方法可以添加,比如控件赋值、获取值等等。
**************************************************************************************
write by Awen.Q ,转载请表明出处:http://www.52aspx.cn
**************************************************************************************
主要功能,用户在使用控件的时候只能看到一个textbox,点击输入框后出现选项菜单,然后选中值后,输入框会自动添加好值,点击旁边的按钮,选项框消失。这边有个注意点,就是要让控件的背景随选择菜单的出现变大和变小。
代码部分:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsControls { public partial class SkyBoxList : UserControl { /// <summary> /// ListItem用户boxlist的赋值 /// </summary> public class ListItem { private string _name; public string Name { get { return _name; } set { _name = value; } } private string _value; public string Value { get { return _value; } set { _value = value; } } public override string ToString() { return Name; } } public SkyBoxList() { InitializeComponent(); for (int i = 0; i < 10; i++) { ListItem lt = new ListItem(); lt.Name = i.ToString() + "name"; lt.Value = i.ToString() + "value"; listBox1.Items.Add(lt); listBox1.DisplayMember = "Name"; listBox1.ValueMember = "Value"; } listBox1.Visible = false; button1.Visible = false; this.Height = this.textBox1.Height; } /// <summary> /// 选择时获取其值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listBox1_SelectedValueChanged(object sender, EventArgs e) { string textstr = ""; int count = listBox1.SelectedItems.Count; for (int i = 0; i < count; i++) { ListItem lt = (ListItem)listBox1.SelectedItems[i]; textstr += lt.Value + ","; } textBox1.Text = textstr; } /// <summary> /// 点击放下多选菜单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_Click(object sender, EventArgs e) { this.Height = this.textBox1.Height + this.listBox1.Height; listBox1.Visible = true; button1.Visible = true; } /// <summary> /// 收起和放下选择菜单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (listBox1.Visible == false) { button1.Text = "∧"; listBox1.Visible = true; this.Height = this.textBox1.Height + this.listBox1.Height; } else { button1.Text = "∨"; listBox1.Visible = false; this.Height = this.textBox1.Height; } } /// <summary> /// 用于设计时,自定义控件内部的控件大小随用户的拉动而变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SkyBoxList_Resize(object sender, EventArgs e) { this.textBox1.Width = this.Width * 9 / 10; this.button1.Width = this.Width-this.textBox1.Width; } } }
总结:
1.使用Item实现了对ListBox的动态赋值。
2.在设计模式下,拉动修改自定义控件的大小,自动修改自定义空间内部控件的大小。
3.自定义控件的大小随多选菜单展开、放下自动变化大小,从而不影响别的控件使用。
这只是一个demo,真的要使用还有很多方法可以添加,比如控件赋值、获取值等等。
**************************************************************************************
write by Awen.Q ,转载请表明出处:http://www.52aspx.cn
**************************************************************************************
相关文章推荐
- 用easyUI的combobox实现多选下拉框
- C#中comboBox下拉框中实现多选
- 扩展Ext的Combobox实现多选下拉列表
- jquery的combobox实现下拉多选
- 使用jquery-combobox实现select下拉框多选之后,如何将下拉框的值传给input隐藏域
- 扩展Ext的Combobox实现多选下拉列表
- 在ExtJS的ComboBox组件中实现下拉树效果
- silverlight自定义用户控件,实现下拉多选功能
- C# WPF 多选ComboBox实现
- jquery自定义风格独特的下拉列表控件(可实现单选和多选)
- 关于easyUI中combobox多选并附加checkbox的实现
- android 简单易用的ListView 实现多选的解决方案
- 【Bootstrap】Bootstrap-select多选下拉框实现
- 一个简单的实现下拉框多选的插件可移植性比较好
- 文本框(Textbox)和下拉框(Combobox)自动联想功能的实现
- jquery实现下拉框多选
- c#多选下拉框(ComboBox)
- JS实现支持多选的遍历下拉列表代码
- JS实现支持多选的遍历下拉列表代码
- TFS下拉列表实现多选