将按钮放在字符串前面

时间:2017-06-13 06:07:40

标签: c# winforms richtextbox

如何将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);
        }
    }

enter image description here

1 个答案:

答案 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));