cursor position

S

Steve

does any one know a way to determine the position of the
cursor in a text box. I would like to position a list next
to the current position for context commands. I am using
VS.NET/C#/System.Windows.Forms.
 
M

Marco Martin

Steve,

I think textbox.Bounds is what your looking for. It holds everything you
might need about the location, size ect.

regards,

Marco
 
S

Steve.

Thanks for the reponse Marco. This gives me a lot of
information, however I still can't seem to get the
position of the code insertion point in form coordinates.
I am starting to wonder whether I have to compare the size
of the text against the client rectangle and calculate
this every time ... or is there an easier way?
 
M

Marco Martin

Steve,

I still not quite clear on what you are trying to do.
If you are trying to "float" a list next to the cursor position inside the
textbox, I think you are going to need to calculate the size of the string
inside the textbox and establish your location from there. I cant remeber
exacly where, but I have seen a class on the net that uses the
graphics.drawstring method to establish the length of a string in pixels.

Hope this helps.
I'm gone for the Holidays!

regards,

Marco
 
G

Gustavo Franco

Try Something like this,

I didn't test it, but I think this is what u are looking for.

TextBox textBox = new TextBox();
Graphics graphics = Graphics.FromHwnd(textBox.Handle);
SizeF size = graphics.MeasureString(textBox.Text.Substring(0,
textBox.SelectionStart), textBox.Font);
Point coord = textBox.Location.Offset((int) size.Width, (int) size.Height);

Regards,
Gustavo.

P.S.: this works if the textbox border is 0, to get the exactly position you
have to get the border size and add to the coordinate Point, I don't
remember right now what is the function or method, but I know it exist.
 
A

Andrew \(Infragistics\)

To get the exact position, you're probably better off using api's and
sending an EM_POSFROMCHAR message to the textbox.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top