如何防止我的ContextMenu被约束到其容器(Win Forms c#)

时间:2010-04-26 14:14:00

标签: c# winforms controls autocomplete contextmenu

我写了一个自定义Control(自动完成TextBox(下方)),其中ContextMenuStrip以编程方式添加到表单中。

我的问题是,当控件生成的列表长度超过其父容器的高度(PanelGroupBox等)时,隐藏ContextMenuStrip的底部。

我试过调用.BringToFront(),但找不到任何方法来克服这种行为。

任何帮助都会非常受欢迎,也可以随意窃取控制权:)

图1。

/// <summary>
/// TextBox which can auto complete words found in a table column
/// Just set DataSource and DataListField and start typing - WD
/// </summary>
public class AutoComplete : TextBox
{
    public DataTable DataSource { get; set; }
    public string DataListField { get; set; }
    private ContextMenuStrip SuggestionList = new ContextMenuStrip();

    public AutoComplete()
    {
        this.LostFocus += new EventHandler(AutoComplete_LostFocus);
        KeyUp += new KeyEventHandler(AutoComplete_KeyUp);
        SuggestionList.ItemClicked += new ToolStripItemClickedEventHandler(SuggestionList_ItemClicked);
    }

    void AutoComplete_LostFocus(object sender, EventArgs e)
    {
        if (!SuggestionList.Focused)
        {
            SuggestionList.Visible = false;
        }
    }

    void SuggestionList_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        this.Text = e.ClickedItem.Text;
        SuggestionList.Visible = false; 
        this.Focus();
        SuggestionList.Visible = false;
    }

    void AutoComplete_KeyUp(object sender, KeyEventArgs e)
    {
        if (null != DataSource && DataSource.Rows.Count > 0 && null != DataListField)
        {
            if (e.KeyCode != Keys.Enter)
            {
                if (SuggestionList.Items.Count > 0 && e.KeyCode == Keys.Down)
                {
                    SuggestionList.Focus();
                    SuggestionList.Items[0].Select();
                    SuggestionList.BringToFront();
                }
                else if (this.Text.Length > 0)
                {
                    SuggestionList.Items.Clear();

                    DataRow[] drSuggestionList = DataSource.Select("[" + DataListField + "] LIKE '" + this.Text + "%'");

                    foreach (DataRow dr in drSuggestionList)
                    {
                        SuggestionList.Items.Add(dr[DataListField].ToString());
                    }

                    SuggestionList.TopLevel = false;
                    SuggestionList.Visible = true;
                    SuggestionList.Top = (this.Top + this.Height);
                    SuggestionList.Left = this.Left;
                    this.Parent.Controls.Add(SuggestionList);
                    SuggestionList.BringToFront();
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

这是因为您通过将其TopLevel属性设置为false并将其添加到父级的Control集合中,将其转换为子控件。替换这个:

                SuggestionList.TopLevel = false;
                SuggestionList.Visible = true;
                SuggestionList.Top = (this.Top + this.Height);
                SuggestionList.Left = this.Left;
                this.Parent.Controls.Add(SuggestionList);
                SuggestionList.BringToFront();

用这个:

      SuggestionList.Show(this.Parent.PointToScreen(new Point(this.Left, this.Bottom)));

请注意,如果文本框太高,CMS将与文本框重叠。