c#-RichTextBox中的显示按钮

如何在设置指针的字符串前面显示按钮?

此刻,按钮出现在单击发生的字符串的对面.

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
    button2.Visible = true;

    int index = richTextBox1.SelectionStart;
    int line = richTextBox1.GetLineFromCharIndex(index);
    button2.Visible = true;
    int x = richTextBox1.Location.X - 10;
    int y = 25;

    for (int i = 0; i < richTextBox1.Lines.Length; i++)
    {
        button2.Location = new Point(280, Cursor.Position.Y - 170);
    }
}

解决方法:

如果要显示richTextBox1的按钮“内部”

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

        if (pos.X > button2.Width + 4)
        {
            if (button2.Parent != richTextBox1)
            {
                button2.Parent.Controls.Remove(button2);
                richTextBox1.Controls.Add(button2);
            }
            button2.Location = new Point(pos.X - button2.Width - 2, pos.Y);
        }
        else
        {
            if (button2.Parent == richTextBox1)
            {
                button2.Parent.Controls.Remove(button2);
                richTextBox1.Parent.Controls.Add(button2);
            }
            button2.Location = new Point(richTextBox1.Left - button2.Width - 2, pos.Y + richTextBox1.Top);
        }
    }

如果要在行开头显示按钮:

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        var pos = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
        button2.Location = new Point(richTextBox1.Left - button2.Width - 2, pos.Y + richTextBox1.Top);
    }
上一篇:分布式事物


下一篇:c#wpf将RichTextBox格式导出为xml文档