Knowing the point location of the caret in a Winforms TextBox?

前端 未结 7 1755
粉色の甜心
粉色の甜心 2021-01-12 08:14

I need to know the exact point location in my window where the caret currently resides so that I can pop up a small window under the text (similar to intellisense or a spell

7条回答
  •  [愿得一人]
    2021-01-12 09:09

     TextBox lbl = new TextBox(); 
                     lbl.Text = Build("20000", i);
                     lbl.Location = new Point(30, 25 * i);
                     lbl.Width = 350;
                     lbl.MouseHover += new EventHandler(lbl_MouseHover);
    
       void lbl_MouseHover(object sender,
     EventArgs e)
             {
                 TextBox t = (TextBox)sender;
                 ListBox lb = new ListBox();
                 for (int i = 0; i < 10; i++)
                 {
                     lb.Items.Add("Hej");
                 }
                 int x = t.Location.X; 
                 int y = t.Location.Y + t.Height;
                 lb.Location = new Point(x, y);
                 panel1.Controls.Add(lb);
                 lb.BringToFront(); 
             }
    

    Note this particular piece of code: int y = t.Location.Y + t.Height; You add the height of the textbox to the y-axis.

提交回复
热议问题