如何在 RichTextBox 上显示面板?

How can I show a Panel on a RichTextBox?

有一个 Button 会根据标记安装在 RichTextBox 中的哪一行垂直改变其位置。当我点击 Button 时,Panel(或 UserControl,也许更好)出现在 Button 对面到 [=12= 的整个宽度是必要的].

Panel 应该位于 RichTextBox 上。也就是说,如果窗体的初始尺寸(500, 500)而这个Panel是从边缘到边缘显示的RichTextBox,当用户改变框架尺寸为(500; 1000) 并再次按下 Panel 显示,这个 Panel 应该会拉伸并且所有内容也会显示在 RichTextBox 上。下面是垂直显示 Button 的代码和我需要的动画 GIF 示例。

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    var pos = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);

    Point locationOnForm = panel1.FindForm()
            .PointToClient(panel1.Parent.PointToScreen(panel1.Location));

    Point newLocation = new Point(locationOnForm.X - 10, pos.Y + locationOnForm.Y - 13);

    button1.Location = newLocation;
}

在 onClick 方法中,您可以将面板放在它旁边吗?

    private void MyButton_Click(object sender, EventArgs e)
    {
        MyPanel.Location = new Point(MyButton.Location.X + MyButton.Width, MyButton.Location.Y);
        MyPanel.Visible = true;
    }

如果您希望它与按钮的高度相同,请在方法中添加此行。

MyPanel.Size = new System.Drawing.Size(MyPanel.Width, MyButton.Height);

要拉伸它,您可以使用表单 SizeChnged 事件

    private void MyForm_SizeChanged(object sender, EventArgs e)
        {
           MyPanel.Width - MyButton.width + 5;
        }