当我选择或双击RichTextBox
中的某个字词时,面板应显示在该字词上方(此panel
最初会被隐藏并显示在该字词上突出显示)。删除选择后,panel
应该会消失。
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
panel1.Visible = true;
else
panel1.Visible = false;
}
答案 0 :(得分:0)
根据您的更改,您需要一个自定义控件来实现此目的。
为此设置自定义位置,另一个问题可能是z-index
的{{1}}(优先级)(此处为:Control
)。使用buttonOverlay
,您可以将其设置在前面。
buttonOverlay.BringToFront()
要添加自定义private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
{
Point relativePoint = rTxtBxSelectionTester.GetPositionFromCharIndex(rTxtBxSelectionTester.SelectionStart);
int txtBsPosX = relativePoint.X + rTxtBxSelectionTester.Location.X;
int txtBxPosY = relativePoint.Y + rTxtBxSelectionTester.Location.Y - this.buttonOverlay.Size.Height;
relativePoint = new Point(txtBsPosX, txtBxPosY);
this.buttonOverlay.Location = relativePoint;
this.buttonOverlay.Visible = true;
this.buttonOverlay.BringToFront();
}
else
{
this.buttonOverlay.Visible = false;
}
}
,请将以下代码添加到Control
构造函数中:
Form
this.buttonOverlay = new FormattingOverlay(this);
this.Controls.Add(this.buttonOverlay);
this.buttonOverlay.Visible = false;`
是一个继承自FormattingOverlay
:
UserControl
可以找到整个示例项目via this link.