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

前端 未结 7 1703
粉色の甜心
粉色の甜心 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 08:57
    MessageBox.Show(textBox1.SelectionStart.ToString());
    
    0 讨论(0)
  • 2021-01-12 09:03

    Here is my solution:

    private Point GetCaretPosition()
    {
        Point result = TextBox.GetPositionFromCharIndex(TextBox.SelectionStart);
    
    
        if (result.X == 0 && TextBox.Text.Length > 0)
        {
            result = TextBox.GetPositionFromCharIndex(TextBox.Text.Length - 1);
            int s = result.X / TextBox.Text.Length;
            result.X = (int)(result.X + (s * 1.3));
        }
    
    
        return result;
    }
    
    0 讨论(0)
  • 2021-01-12 09:04

    Try this.

    Under `Control.KeyPress` event,

    I use Control.GetPositionFromCharIndex(Control.SelectionStart) to get the caret position which relate to the control. Then translate this position relate to the screen for the form's new location.

    private void aTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        SuperComboForm supcomboF = new SuperComboForm();
        supcomboF.StartPosition = FormStartPosition.Manual;
        var caret_pos = aTextBox.GetPositionFromCharIndex(aTextBox.SelectionStart);
        supcomboF.Location = PointToScreen(new Point(caret_pos.X, caret_pos.Y));
        supcomboF.ShowDialog();
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 09:10

    If you don't mind using Win32-API use GetCaretPos to get the exact position.

    0 讨论(0)
  • 2021-01-12 09:18

    I have discoverd some peculiar behavior of the textbox control. When you type text at the end of the textbox, GetPositionFromCharIndex remains 0,0. However, if you insert text before the last char in the textbox, GetPositionFromCharIndex IS updated.

    Don't know if you can use that to your advantage, but I think you might wanted to know that.

    Edit: Ok, LOL, this seems to work, how ugly it may be...:

    Point pt = textBox1.GetPositionFromCharIndex(textBox1.SelectionStart > 0 ? textBox1.SelectionStart - 1 : 0);
    

    Edit 2: In retrospect, I think the behavior isn't so odd, since the SelectionStart at the end of a textbox returns the index where the next character will be typed, not where the last character actually is. Since GetPositionFromCharIndex will return an character position, the index of a not yet existing char returns apparently 0,0.

    Edit 3: With mine and MikeJ's code combined and slightly reworked, you can get the exact position of the caret:

    SizeF size = g.MeasureString(textBox1.Text.Substring(textBox1.SelectionStart - 1, 1), textBox1.Font);
    pt.X += (int)size.Width;
    

    I admit, It's damn-ugly the way I typed it, but with some editing you should be able to get some nice code out of this.

    0 讨论(0)
提交回复
热议问题