How to display the button in front of the string where the pointer is set?

At the moment, the button appears opposite the string where the click occurred.
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);
}
}
If you wan to display button "inside" of 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);
}
}
If you wan to display button at the line start:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With