[添加文本框时C#WinForms contextmenu焦点问题

问题描述 投票:0回答:1

在C#WinForms应用程序中,我需要使用下拉列表和文本框创建ContextMenuStrip

private System.Windows.Forms.ContextMenuStrip ct1;

private void button_Click(object sender, EventArgs e) 
{
    var header = new ToolStripMenuItem("Header");
    header.Enabled = false;

    var options = new ToolStripMenuItem("Options");

    for (int i = 0; i < 5; i++)
    {
        var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
        checkoption.CheckOnClick = true;
        options.DropDownItems.Add(checkoption);
    }

    var txt = new ToolStripTextBox();
    txt.Text = "changeme";


    options.DropDownItems.Add(txt);

    options.DropDown.Closing += DropDown_Closing;

    ct1.Items.Clear();
    ct1.Items.Add(header);
    ct1.Items.Add(options);

    ct1.Show(this, button.Left, button.Top);
}

private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
    e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}

现在,如果原因为e.CancelItemClicked将阻止关闭下拉菜单,因此我可以选择更多项目,而无需再次打开菜单:

enter image description here

请注意,“ changeme”是ToolStripTextBox

一旦聚焦(单击它),我就可以在其中编辑文本:

enter image description here

完成文本框的编辑后,我仍然可以更改复选框的项目,但是有无焦点指示器

enter image description here

如何像第一张照片一样取回聚焦指示器?

注:如果将鼠标移到“页眉”上,下拉列表将关闭,然后再将其移回“选项”,将重新打开下拉列表,然后焦点指示符将再次恢复正常:]]

enter image description here

如何在不关闭并重新打开下拉菜单的情况下执行此操作?

我为Select()项目尝试了options,但没有帮助,Invalidate()上的ct1也没有帮助。

在C#WinForms应用程序中,我需要使用下拉列表和文本框创建ContextMenuStrip:private System.Windows.Forms.ContextMenuStrip ct1;私人void button_Click(object sender,EventArgs e){...

c# drop-down-menu contextmenu
1个回答
0
投票

刚刚找到它:

© www.soinside.com 2019 - 2024. All rights reserved.