TextBox GetPositionFromCharIndex

  • Thread starter Thread starter john doe
  • Start date Start date
J

john doe

I'm trying to implement the GetPositionFromCharIndex for a TextBox. As
all the RichTextBox uses is the windows api EM_POSFROMCHAR SendMessage
call, it looks like quite an easy thing to do, however my call never
populates my Point object. Here's the code:

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, ref POINTL
p, int lParam);
private const int EM_POSFROMCHAR = 0xD6;

public struct POINTL
{
public int X,Y;
}

public Point GetPositionFromCharIndex(int index)
{
POINTL point = new POINTL();
int n = SendMessage(this.Handle,EM_POSFROMCHAR,ref point,index );
System.Diagnostics.Debug.WriteLine(string.Format("index:{0}
point.X:{1}",index,point.X));

return new Point(point.X,point.Y);
}


I've seen a post where Nicholas Paldino suggests using a standard Point
object (http://www.dotnet247.com/247reference/msgs/36/182264.aspx),
which I've tried with no luck.

Can anyone point me in the right direction?
 
While I could not get your code to work as written, I do think I found a
solution for you.

First up, we need to change your definition of SendMessage() to be a bit
more generic:

[DllImport("user32", CharSet=CharSet.Auto, EntryPoint="SendMessage")]
private extern static int SendMessage(IntPtr handle, int msg, int wParam,
int lParam );

Then when we call:

int n = SendMessage(HandleOfControl,EM_POSFROMCHAR, characterIndex, 0 );

This SendMessage returns an the X,Y position as a single integer which you
can use to create a new point for your return ala:

return new Point(n);

You can find some info on the difference between your original way of
calling and this new one at
http://msdn.microsoft.com/library/d...erence/editcontrolmessages/em_posfromchar.asp.

Brendan
 
Thanks, I see now that I was reading the wParam and lParam details for
the RichText box, not default edit controls.
 
Back
Top