在RichTextBox中选择单词时如何显示面板?

时间:2017-06-01 17:23:58

标签: c# winforms richtextbox

当我选择或双击RichTextBox中的某个字词时,面板应显示在该字词上方(此panel最初会被隐藏并显示在该字词上突出显示)。删除选择后,panel应该会消失。

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    if (richTextBox1.SelectedText.Length > 0)
       panel1.Visible = true;
    else
       panel1.Visible = false;
}

1 个答案:

答案 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.