社区首页 >问答首页 >如何防止ContextMenu被限制在其容器中(Win Forms c#)

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

Stack Overflow用户
提问于 2010-04-26 14:14:00
回答 1查看 934关注 0票数 0

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

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

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

任何帮助都将得到极大的重视,也请随意窃取控制:)

图1。

代码语言:javascript
代码运行次数:0
复制
/// <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();
                }
            }
        }
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-04-26 15:18:54

这是因为您将其TopLevel属性设置为false并将其添加到父控件的control集合中,从而将其转换为子控件。替换为:

代码语言:javascript
代码运行次数:0
复制
                SuggestionList.TopLevel = false;
                SuggestionList.Visible = true;
                SuggestionList.Top = (this.Top + this.Height);
                SuggestionList.Left = this.Left;
                this.Parent.Controls.Add(SuggestionList);
                SuggestionList.BringToFront();

有了这个:

代码语言:javascript
代码运行次数:0
复制
      SuggestionList.Show(this.Parent.PointToScreen(new Point(this.Left, this.Bottom)));

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2714124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档