繁体   English   中英

如何防止ContextMenu限制在其容器中(Win Forms c#)

[英]How do I prevent my ContextMenu from being constrained to its container (Win Forms c#)

我写了一个自定义的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();
                }
            }
        }
    }

}

这是因为您通过将其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太高,它将与文本框重叠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM