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);
}
标签:richtextbox,c,winforms 来源: https://codeday.me/bug/20191111/2018809.html