【问题标题】:How can I show a Panel on a RichTextBox?如何在 RichTextBox 上显示面板?
【发布时间】:2017-11-19 13:53:01
【问题描述】:

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

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;
}

【问题讨论】:

  • 也许你可以试试control layering
  • 您可以覆盖 uc,将其定位与放置按钮类似。问题究竟出在哪里? (请注意,它将覆盖 RTB,即它将覆盖部分 Text..

标签: c# winforms button panel richtextbox


【解决方案1】:

在 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;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2011-05-24
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多