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

C# 在RichTextBox中选择单词时如何显示面板?,c#,winforms,richtextbox,C#,Winforms,Richtextbox,当我在RichTextBox中选择或双击某个单词时,该单词上方会出现一个面板(该面板最初是隐藏的,当该单词高亮显示时会出现)。当我删除选择时,面板应该消失 private void richTextBox1_SelectionChanged(object sender, EventArgs e) { if (richTextBox1.SelectedText.Length > 0) panel1.Visible = true; else pane

当我在
RichTextBox
中选择或双击某个单词时,该单词上方会出现一个面板(该
面板最初是隐藏的,当该单词高亮显示时会出现)。当我删除选择时,
面板
应该消失

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

根据您的更改,您需要一个自定义控件来实现这一点。 为此设置自定义位置,另一个问题可能是
控件的
z-index
(优先级)(此处:
buttonOverlay
)。使用
按钮noverlay.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;
    }
}
要添加自定义
控件
请将以下代码添加到
窗体
构造函数中:

this.buttonOverlay = new FormattingOverlay(this);
this.Controls.Add(this.buttonOverlay);
this.buttonOverlay.Visible = false;`
FormattingOverlay
是从
UserControl
继承的类:

public partial class FormattingOverlay : UserControl
{

    public FormattingOverlay(Form1 mainForm)
    {
        this.mainForm = mainForm;
        InitializeComponent();
    }

    private void btnBold_Click(object sender, EventArgs e)
    {
        RichTextBox rTxtBx = mainForm.rTxtBxSelectionTester;
        rTxtBx.SelectionFont = new Font(rTxtBx.Font, FontStyle.Bold);
        rTxtBx.SelectionStart = rTxtBx.SelectionStart + rTxtBx.SelectionLength;
        rTxtBx.SelectionLength = 0;
        rTxtBx.SelectionFont = rTxtBx.Font;
    }
}

整个示例项目都可以找到

根据您的更改,您需要一个自定义控件来实现这一点。 为此设置自定义位置,另一个问题可能是
控件的
z-index
(优先级)(此处:
buttonOverlay
)。使用
按钮noverlay.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;
    }
}
要添加自定义
控件
请将以下代码添加到
窗体
构造函数中:

this.buttonOverlay = new FormattingOverlay(this);
this.Controls.Add(this.buttonOverlay);
this.buttonOverlay.Visible = false;`
FormattingOverlay
是从
UserControl
继承的类:

public partial class FormattingOverlay : UserControl
{

    public FormattingOverlay(Form1 mainForm)
    {
        this.mainForm = mainForm;
        InitializeComponent();
    }

    private void btnBold_Click(object sender, EventArgs e)
    {
        RichTextBox rTxtBx = mainForm.rTxtBxSelectionTester;
        rTxtBx.SelectionFont = new Font(rTxtBx.Font, FontStyle.Bold);
        rTxtBx.SelectionStart = rTxtBx.SelectionStart + rTxtBx.SelectionLength;
        rTxtBx.SelectionLength = 0;
        rTxtBx.SelectionFont = rTxtBx.Font;
    }
}

可以找到整个示例项目

那么问题是什么?如何使面板显示在所选单词的上方?您可能需要检查如何获取所选单词的位置信息以定位面板。那么问题是什么?如何使面板显示在所选单词的上方?您可能需要检查想法关于如何获取选定单词的位置信息以定位面板。此面板上不应有单词。我需要在此面板上有按钮。在此面板中,文本格式按钮应该位于。如果用户选择了一个单词,那么这个面板将显示所选单词或行的编辑按钮。我对代码做了一些更改,查看了一下,还创建了一个新的示例项目。它看起来很棒,但是如何将面板提升到上面呢?面板应位于所选单词上方。
int txtBsPosX=relativePoint.X+rTxtBxSelectionTester.Location.X;int txtBxPosY=relativePoint.Y+rTxtBxSelectionTester.Location.Y-buttonOverlay.Size.Height;相对点=新点(txtBsPosX,txtBxPosY)此面板上不应有文字。我需要在此面板上有按钮。在此面板中,文本格式按钮应该位于。如果用户选择了一个单词,那么这个面板将显示所选单词或行的编辑按钮。我对代码做了一些更改,查看了一下,还创建了一个新的示例项目。它看起来很棒,但是如何将面板提升到上面呢?面板应位于所选单词上方。
int txtBsPosX=relativePoint.X+rTxtBxSelectionTester.Location.X;int txtBxPosY=relativePoint.Y+rTxtBxSelectionTester.Location.Y-buttonOverlay.Size.Height;相对点=新点(txtBsPosX,txtBxPosY)