日期:2014-05-17  浏览次数:21022 次

关于继承 ComboBox,添加 ListBox控件,使其支持拼音自动提示功能,所遇到的问题!
如题:关于继承 ComboBox,添加 ListBox控件,使其支持拼音自动提示功能,所遇到的问题!

我开始用的是TextBox+ListBox来做的,功能已经完成正常使用了,但后来根据要求
要把拼音自动提示功能加到ComboBox中,所以我就继承了系统的 ComboBox 控件,
用 ToolStripDropDown 来加载 ListBox,然后显示的时候把 ToolStripDropDown 
显示到ComboBox的下方。
现在碰到的问题是:在ComboBox的文本框中输入文本时,输入一个字符后ComboBox的文本框
就自动失去焦点了,我用 .Focus() 再次获取焦点时,也能获取到焦点,能看到光标在闪动
但就是无法输入文本,ComboBox 的_TextChanged 事件也触发不了,不知什么原因。

以下是代码,请各位帮帮帮,分不够再加。


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();
        }