Goto a line number in a text box?

  • Thread starter Thread starter Mike O.
  • Start date Start date
M

Mike O.

Is there a way to go to and select a particular line
number in a text box or rich text box?

I already know the line number that I need to go to.

Thanks in advance for any help you can provide.
 
Try this:

Use the Select() method to select the spot in the text
that you want, then use the ScrollToCaret() method to go
there.

Hope this is is what you wanted...
 
Thanks! The ScrollToCaret() solves the next problem I
would have had :) But I still have my original problem,
since select goes by character position, I can't just pass
the line number.

I do not know the text that I want to select either, so I
can't do a search on that. I just know the line number.
 
I think you're looking for the EM_LINEFROMCHAR message. You need to do
something like the following (I haven't tested it).

[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEFROMCHAR = 0xC9;
public static int GetLineNumberFromChar(TextBoxBase control)
{
return SendMessageInt(control.Handle, EM_LINEFROMCHAR, -1, 0) + 1;
}

The docs say:

The EM_LINEFROMCHAR message retrieves the index of the line that contains
the specified character index in a multiline edit control. A character index
is the zero-based index of the character from the beginning of the edit
control. You can send this message to either an edit control or a rich edit
control.

Tom Clement
Apptero, Inc.
 
Oops, I misunderstood your original message. The message you want is
EM_LINEINDEX. You can use it to get the character indexes of the beginning
of a specified line. Try something like:

[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEINDEX= 0xBB;
public static int GetCharFromLineNumber(TextBoxBase control, int lineNumber)
{
return SendMessageInt(control.Handle, EM_LINEINDEX, lineNumber, 0);
}

Tom Clement
Apptero, Inc.

P.S. Also the code in my earlier post about EM_LINEFROMCHAR didn't pass in
the character position, so that would have to be fixed.

Tom Clement said:
I think you're looking for the EM_LINEFROMCHAR message. You need to do
something like the following (I haven't tested it).

[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEFROMCHAR = 0xC9;
public static int GetLineNumberFromChar(TextBoxBase control)
{
return SendMessageInt(control.Handle, EM_LINEFROMCHAR, -1, 0) + 1;
}

The docs say:

The EM_LINEFROMCHAR message retrieves the index of the line that contains
the specified character index in a multiline edit control. A character index
is the zero-based index of the character from the beginning of the edit
control. You can send this message to either an edit control or a rich edit
control.

Tom Clement
Apptero, Inc.

Mike O. said:
Is there a way to go to and select a particular line
number in a text box or rich text box?

I already know the line number that I need to go to.

Thanks in advance for any help you can provide.
 
That worked thanks! :)
-----Original Message-----
Oops, I misunderstood your original message. The message you want is
EM_LINEINDEX. You can use it to get the character indexes of the beginning
of a specified line. Try something like:

[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEINDEX= 0xBB;
public static int GetCharFromLineNumber(TextBoxBase control, int lineNumber)
{
return SendMessageInt(control.Handle, EM_LINEINDEX, lineNumber, 0);
}

Tom Clement
Apptero, Inc.

P.S. Also the code in my earlier post about EM_LINEFROMCHAR didn't pass in
the character position, so that would have to be fixed.

Tom Clement said:
I think you're looking for the EM_LINEFROMCHAR message. You need to do
something like the following (I haven't tested it).

[DllImport("user32.dll", EntryPoint="SendMessage",
CharSet =
CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEFROMCHAR = 0xC9;
public static int GetLineNumberFromChar(TextBoxBase control)
{
return SendMessageInt(control.Handle, EM_LINEFROMCHAR, -1, 0) + 1;
}

The docs say:

The EM_LINEFROMCHAR message retrieves the index of the line that contains
the specified character index in a multiline edit
control. A character
index
is the zero-based index of the character from the beginning of the edit
control. You can send this message to either an edit
control or a rich
edit
control.

Tom Clement
Apptero, Inc.


.
 
Glad to help.
Tom
Mike O said:
That worked thanks! :)
-----Original Message-----
Oops, I misunderstood your original message. The message you want is
EM_LINEINDEX. You can use it to get the character indexes of the beginning
of a specified line. Try something like:

[DllImport("user32.dll", EntryPoint="SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEINDEX= 0xBB;
public static int GetCharFromLineNumber(TextBoxBase control, int lineNumber)
{
return SendMessageInt(control.Handle, EM_LINEINDEX, lineNumber, 0);
}

Tom Clement
Apptero, Inc.

P.S. Also the code in my earlier post about EM_LINEFROMCHAR didn't pass in
the character position, so that would have to be fixed.

Tom Clement said:
I think you're looking for the EM_LINEFROMCHAR message. You need to do
something like the following (I haven't tested it).

[DllImport("user32.dll", EntryPoint="SendMessage",
CharSet =
CharSet.Auto)]
private static extern int SendMessageInt(IntPtr hWnd, UInt32 Msg, Int32
wParam, Int32 lParam);
private const int EM_LINEFROMCHAR = 0xC9;
public static int GetLineNumberFromChar(TextBoxBase control)
{
return SendMessageInt(control.Handle, EM_LINEFROMCHAR, -1, 0) + 1;
}

The docs say:

The EM_LINEFROMCHAR message retrieves the index of the line that contains
the specified character index in a multiline edit
control. A character
index
is the zero-based index of the character from the beginning of the edit
control. You can send this message to either an edit
control or a rich
edit
control.

Tom Clement
Apptero, Inc.

Is there a way to go to and select a particular line
number in a text box or rich text box?

I already know the line number that I need to go to.

Thanks in advance for any help you can provide.


.
 
Back
Top