ToolStripDropDown 编辑文本框时鼠标光标消失
我有一个带有 RichTextBox 的用户控件和两个按钮。我试图在 ToolStripDropDown 上的 ToolStripButton 单击时显示这一点。我正在使用 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;
}
I have a user control with RichTextBox and two buttons on it. I'm trying to show that at ToolStripDropDown on ToolStripButton click. I'm using ToolStripControlHost to put my control at ToolStripDrowDown. When I click ToolStripButton at form toolbar, I show dropdown at some position and make focus at ToolStripControlHost control. Mouse pointer stays above ToolStripButton and cursor is at RichTextBox. But when I start editing RichTextBox mouse pointer disappears and I can see it only when it's out of form rectangle. How can I fix it?
Here is my code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您开始编辑 TextBox 或 RichTextBox 时,鼠标光标隐藏是正常现象。当您移动鼠标时它应该会恢复。您还应该注意,调用 Cursor.Show 将不会产生任何效果。
It is normal behavior for the mouse cursor to hide when you start to edit a TextBox, or RichTextBox. It should be restored when you move the mouse. You should also note that calling Cursor.Show will not have an effect if you try to call it in TextChanged, or something similar.
我找到了解决方案。不是最好的,但它有效。我只是用 WinApi 模拟单击显示的 RichTextBox,如下所示:
I found the solution. Not best but it works. I just simulate click at shown RichTextBox with WinApi like that: