您的位置:首页 > 其它

combobox实现下拉多选的终结解决方案

2009-07-05 15:22 337 查看
前段时间自己在公司负责将web下的一些自己的控件转换为 winform模式下。其实对于C/S的开发自己不是很熟悉,但是新员工嘛,没办法,硬着头皮扛下了。接下来就是不断去学习和研究,今天在做多选下拉菜单时,遇到了一点难度,网上搜索了下,解决方案很多,大多在combobox里面家checkbox,但这样对用户的体验不是很好,因为用户可能需要选择连续的很多选项,这时就需要一个一个的点,不够人性化。思考之后,我觉得ListBox里面的MultiExtended蛮适合的。于是利用午后时间,做了demo,结合textbox一起使用,效果不错。废话先不多说了,来张截图先吧。



主要功能,用户在使用控件的时候只能看到一个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
**************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: