found it! was: counting lines of textbox

H

HDB

Thanks for the responses in a former thread about getting the count of lines
in a multiline textbox with wordwrap.

After some tinkering, I found the solution (based on a VB6 solution, had to
modify a bit for VB.NET):

<DllImport("user32.dll")> Private Shared Function SendMessage(ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32
End Function

Dim EM_GETLINECOUNT As Long = &HBA&
Dim lineCount As Int32 = SendMessage(txtBox.Handle, EM_GETLINECOUNT, 0, 0&)

Works like a charm.
 
C

Cor Ligthert

Hi HDB,

Did not see such a compact solution for this problem before.

And although that it is an API, which I think you should avoid in dotnet, I
put it in my snippets because it is such a special purpose.

(Most that I saw had to do with measure line height and box height, which I
did found terrible and a lot of work when the texbox would be resizable)

However this compact code I save.

Thanks

Cor
 
T

Tom Shelton

HDB said:
Thanks for the responses in a former thread about getting the count of lines
in a multiline textbox with wordwrap.

After some tinkering, I found the solution (based on a VB6 solution, had to
modify a bit for VB.NET):

<DllImport("user32.dll")> Private Shared Function SendMessage(ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32
End Function

Dim EM_GETLINECOUNT As Long = &HBA&

That should be changed to:
[Public|Private] Const EM_GETLINECOUNT As Integer = &HBA

Long is 64 bits in VB.NET. In fact, the following call to SendMessage
should give you an error if you have Option Strict On (It is on right?).
Other then that - this is a good way to do what you want.
Dim lineCount As Int32 = SendMessage(txtBox.Handle, EM_GETLINECOUNT, 0, 0&)

Works like a charm.

Tom Shelton [MVP]
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
And although that it is an API, which I think you should avoid in dotnet, I
put it in my snippets because it is such a special purpose.

Why avoid p/invoke?!

*shocked*
 

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