2017-06-13 67 views
0

如何將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

這不是很清楚w ^你的意思是「在...之前」和「在...之後」。你能解釋一下嗎? –

+0

預期的位置在哪裏? 「在線前面」在哪裏? – Sweeper

+0

我也不明白爲什麼你有一個for循環的內部指令總是相同的 –

回答

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)); 
+0

但如何將按鈕放在字符串的對面?該按鈕應該只能垂直移動。 – Kaya

+0

回答更新請檢查 –

+0

對不起,還有一個問題。如何在面板上顯示此按鈕,而不是在窗體上顯示? – Kaya

相關問題