C# 当ToolStripDropDown编辑文本框时,鼠标光标消失

C# 当ToolStripDropDown编辑文本框时,鼠标光标消失,c#,toolstripdropdown,mouse-pointer,C#,Toolstripdropdown,Mouse Pointer,我有一个带有RichTextBox和两个按钮的用户控件。我试图在ToolStripButton点击上的ToolStripDropDown上显示这一点。我正在使用ToolStripControlHost将我的控件置于ToolStripDrowDown。当我单击表单工具栏上的ToolStripButton时,我会在某个位置显示下拉列表,并在ToolStripControlHost控件上聚焦。鼠标指针停留在ToolStripButton上方,光标位于RichTextBox。但当我开始编辑RichTex

我有一个带有RichTextBox和两个按钮的用户控件。我试图在ToolStripButton点击上的ToolStripDropDown上显示这一点。我正在使用ToolStripControlHost将我的控件置于ToolStripDrowDown。当我单击表单工具栏上的ToolStripButton时,我会在某个位置显示下拉列表,并在ToolStripControlHost控件上聚焦。鼠标指针停留在ToolStripButton上方,光标位于RichTextBox。但当我开始编辑RichTextBox时,鼠标指针消失了,只有当它不在矩形中时,我才能看到它。我怎样才能修好它

这是我的密码:

private void toolBtnNote_Click(object sender, EventArgs e)
{

   dropDownClosed = false;
   noteChanged = false;

   tsdd = new ToolStripDropDown();
   this.tsdd.Opened += new EventHandler(tsdd_Opened);
   this.tsdd.AutoSize = true;

   NoteEdit ne = new NoteEdit();
   ne.NoteText = note ?? "";
   // appears when user clicks first button at my control
   ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick);
   // appears when user clicks second button at my control
   ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick);

   this.tbh = new ToolStripControlHost(ne, "noteEdit");
   this.tbh.Padding = new Padding(0);
   this.tbh.AutoSize = false;
   this.tbh.Size = ne.Size;

   this.tsdd.Items.Add(tbh);
   this.tsdd.Padding = new Padding(0);
   this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing);
   // show toolstripdrowdown at specific position at DataGridView         
   this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height));

   while (!this.dropDownClosed)
   {
       Application.DoEvents();
   }

   if(noteChanged) {...}
}

void ne_CancelClick()
{
    tsdd.Close();
}

void ne_OkClick()
{
    noteChanged = true;
    tsdd.Close();
}

void tsdd_Opened(object sender, EventArgs e)
{
    tbh.Focus();
}

void tsdd_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
    dropDownClosed = true;
}

当您开始编辑文本框或RichTextBox时,鼠标光标隐藏是正常行为。当你移动鼠标时,它应该被恢复。您还应该注意,如果您尝试用TextChanged或类似的方式调用它,调用将不会产生效果。

我找到了解决方案。不是最好的,但它很有效。我只是用WinApi模拟单击显示的RichTextBox,如下所示:

public class WinApiHelper
{
    private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

    [DllImport("user32.dll")]
    private static extern void mouse_event(
        UInt32 dwFlags, // motion and click options
        UInt32 dx, // horizontal position or change
        UInt32 dy, // vertical position or change
        UInt32 dwData, // wheel movement
        IntPtr dwExtraInfo // application-defined information
        );

    public static void SendClick(Point location)
    {
        Cursor.Position = location;
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
    }     
}

private void toolBtnNote_Click(object sender, EventArgs e)
{
        dropDownClosed = false;
        noteChanged = false;

        dgvMarks.Focus();
        tsdd = new ToolStripDropDown();
        this.tsdd.Opened += new EventHandler(tsdd_Opened);
        this.tsdd.AutoSize = true;

        NoteEdit ne = new NoteEdit();
        ne.NoteText = note ?? "";
        ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick);
        ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick);

        this.tbh = new ToolStripControlHost(ne, "noteEdit");
        this.tbh.Padding = new Padding(0);
        this.tbh.AutoSize = false;
        this.tbh.Size = ne.Size;

        this.tsdd.Items.Add(tbh);
        this.tsdd.Padding = new Padding(0);
        this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing);

        this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height));
        // emulate click on richtextbox at dropdown 
        WinApiHelper.SendClick(tsdd.Location + new Size(ne.Rtb.Location) + new Size(5, 5));

        while (!this.dropDownClosed)
        {
            Application.DoEvents();
        }

        if (noteChanged)
        {...}
 }

谢谢你的请求。是的,它将恢复,但仅恢复外形外的显示矩形。所以,当鼠标移到我的窗体上时,光标消失了。我需要单击我的表单以显示光标。奇怪的是,当我从ContextMenuStrip项单击中显示相同的toolstripdropdown(相同的代码)时,一切正常,当我移动鼠标时,光标移到窗体上。仅当我从工具栏按钮调用toolstripdropdown时出现问题。看起来焦点停留在ToolStripButton上。