日期:2014-05-17 浏览次数:21051 次
public class ComboBoxListBox : ComboBox
{
ToolStripControlHost ControlListBoxHost;
ToolStripDropDown dropDown;
ListBox listBox = new ListBox();
public ComboBoxListBox()
{
DrawListBox();
}
private void DrawListBox()
{
listBox.BorderStyle = BorderStyle.None;
listBox.Click += new EventHandler(listBox_Click);
base.TextChanged += new EventHandler(ComboBoxListBox_TextChanged);
Form frmDataSource = new Form();
frmDataSource.Controls.Add(listBox);
frmDataSource.SuspendLayout();
ControlListBoxHost = new ToolStripControlHost(listBox);
ControlListBoxHost.AutoSize = false;
// 重新调整一下位置
ControlListBoxHost.Margin = new Padding(0, -1, 0, -1);
dropDown = new ToolStripDropDown();
dropDown.Width = this.Width;
dropDown.Items.Add(ControlListBoxHost);
}
void ComboBoxListBox_TextChanged(object sender, EventArgs e)
{
this.ShowDropDown();
this.Focus();
}
void listBox_Click(object sender, EventArgs e)
{
PopupGridView(e);
}
/// <summary>
/// 弹出下拉表格并触发选择后事件
/// </summary>
/// <param name="e"></param>
private void PopupGridView(EventArgs e)
{
if (ListBoxSpell.SelectedItems.Count > 0)
{
base.Text = ListBoxSpell.SelectedItems[0].ToString();
}