我有一個RichTextBox和兩個按鈕的用戶控件。我試圖在ToolStripButton上點擊ToolStripDropDown。我使用ToolStripControlHost將我的控件放在ToolStripDrowDown上。當我在窗體工具欄上單擊ToolStripButton時,我會在某個位置顯示下拉菜單,並將焦點放在ToolStripControlHost控件上。鼠標指針保持在ToolStripButton之上,遊標位於RichTextBox上。但是當我開始編輯RichTextBox鼠標指針消失時,只有當它不在矩形中時才能看到它。我該如何解決它?在ToolStripDropDown上編輯文本框時,鼠標光標消失
這裏是我的代碼:
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;
}
感謝您的要求。是的,它可以恢復,但僅限於窗體顯示矩形。所以,當鼠標在我的表單上時,光標消失。我需要點擊我的表格來顯示光標。奇怪的是,當我從ContextMenuStrip項目點擊顯示相同的toolstripdropdown(相同的代碼)時,一切都正常,並且當我移動鼠標時,光標在表單上結束。只有當我從ToolBar按鈕調用toolstripdropdown時纔會出現問題。看起來焦點停留在ToolStripButton。 – mao