如何将button
放在设置标记的行前面?
button
应放在字符串之前。如果用户移动标记,则button
将移动到另一个字符串。如图所示。
目前,button
显示在发生click
的行的对面。
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
buttonaddmenu.Visible = true;
int index = richTextBox1.SelectionStart;
int line = richTextBox1.GetLineFromCharIndex(index);
buttonaddmenu.Visible = true;
int x = richTextBox1.Location.X - 10;
int y = 25;
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
buttonaddmenu.Location = new Point(3, Cursor.Position.Y - 170);
}
}
答案 0 :(得分:1)
你可以通过增加或减少x和y来改变基于按钮大小(我的按钮大小21,23)的新位置(例如:locationOnForm.X-20)试试这个:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
var pos = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
Point locationOnForm = richTextBox1.FindForm().PointToClient(richTextBox1.Parent.PointToScreen(richTextBox1.Location));
Point newLocation = new Point(locationOnForm.X-20,
pos.Y + locationOnForm.Y);
button2.Location = newLocation;
}
更新
Point locationOnForm = panel1.FindForm().PointToClient(panel1.Parent.PointToScreen(richTextBox1.Location));