VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
Go Back  Xtreme Visual Basic Talk > > > VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2010, 12:30 PM
GGoldenborne GGoldenborne is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs


Hi, y'all,

In VB .Net (2008), I'm trying to figure out how to count the number of lines in a text box, taking into account when the text wraps because the line is too long (word wraps) as well as when a new line is forced by a hard return (carriage return / line feed (CRLF) or just a line feed (LF)). I've found several posts on the internet, which usually are almost identical, for the use of the SendMessageAsLong function. One from Xtreme VB Talk is this one in the VB 4/5/6 General area:

http://www.xtremevbtalk.com/showthread.php?p=376089


It works great in VB 6, but in .Net, it doesn't know what to do with the hWnd:
lCount = SendMessageAsLong(TextBox1.hWnd, EM_GETLINECOUNT, 0&, 0&)
I get the message:
'hWnd' is not a member of 'System.Window.Forms.TextBox'.
If I convert the hWnd as follows:
lCount = SendMessageAsLong(CLng(TextBox1.Handle), EM_GETLINECOUNT, 0&, 0&)
lCount is 0.

By the way, this is what I have at the top of my form class:

Private Declare Function SendMessageAsLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const EM_GETLINECOUNT As Long = 186&


The particular code I have provided uses ampersands in a few places. Most examples I've found don't do that. It doesn't make a difference, anyway.

I agree with someone's comment that, in a perfect world, the TextBox control would have a method to provide the number of lines for us. Oh, well.

So, does anyone have any idea how to do this? I appreciate your input.

a humble state worker drone
Reply With Quote
  #2  
Old 06-28-2010, 01:33 PM
PlausiblyDamp's Avatar
PlausiblyDampVB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You are using a VB6 declare statement in VB.Net - amongst the many changes data types have been modified. Try using the definition from http://www.pinvoke.net/default.aspx/user32.SendMessage and see if that helps.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 06-28-2010, 03:02 PM
GGoldenborne GGoldenborne is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default

Excellent! With a little modification, I have had success. Thank you, PlausiblyDamp!

The modified code is as follows:

<Runtime.InteropServices.DllImport( _
"user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

Private Const EM_GETLINECOUNT As Long = 186

lCount = CLng(SendMessage(txtMessage.Handle, EM_GETLINECOUNT, CType(0, IntPtr), CType(0, IntPtr)))
Reply With Quote
  #4  
Old 12-09-2014, 01:20 AM
smhiker smhiker is offline
Newcomer
 
Join Date: Dec 2014
Posts: 1
Smile Thank You For Sharing your Code Solution

Thank You So much for sharing your code solution. I had been searching for and experimenting for hours until luckily I saw your post and you actually had the exact code that I was looking for and it worked right away. I am using it to count the lines in a Rich Text Box.

It was great to be able to grab a simple code solution. I only had to do a few (just a few very very minor adjustments to fit my situation).

Thank You again for posting the code solution. Like I said, I had been searching for hours to find a solution, a few that I did find did Not work; I was getting very frustrated especially because I knew it had to be a simple solution to just be able to count the actual lines in the Rich Text Box.

Thank you for the code solution. It is a simple straightforward solution and it Works!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
 
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
VB .Net - TextBox - Counting # of Lines (Accounting for Word Wrapping and CRLFs
 
-->