how to use Declare function for get count num of line in TextArea

Hi,
I have textArea then if I want to use Declare function for count num of line in TextArea1.text
I write code

Declare Function GetDiskFreeSpaceExW Lib “Kernel32” (dirname As WString, ByRef freeBytesAvailable As UInt64, _
ByRef totalbytes As UInt64, ByRef totalFreeBytes As UInt64) As Integer

What should I do?

Thank you!

Hi Wittawat,

Not sure how you would use GetDiskFreeSpaceExW count the number of lines in a string, but you can use this instruction to do it:

lineCount = CountFields(TextArea1.Text, Chr(13))

I,m sorry I write incorrect function . It is

Declare Function SendMessage lib “user32” alias “SendMessageA”(hwnd as integer,wMsg as integer,wParam as integer,lParam as integer)AS integer

I would recommend you only use declares when absolutely necessary.

Using the built-in Xojo functions, such as “CountFields”, make it much easier to compile your application for all the supported platforms (e.g. Windows, OS X and Linux).

really
declares can use in all the supported platforms?

[quote=31838:@wittawat suwannarak]really
declares can use in all the supported platforms?[/quote]

SendMessage look like a declare for windows os

You have to use a different declare for each platform. That makes them harder to code cross-platform.

But I saw your previous thread. The code provided there should have worked for you. Was there a problem?

[quote=31838:@wittawat suwannarak]really
declares can use in all the supported platforms?[/quote]

No, the opposite. Declares only work on the OS it is declared for. Built-in functions workson all platforms.

[quote=31832:@Alwyn Bester]Hi Wittawat,

Not sure how you would use GetDiskFreeSpaceExW count the number of lines in a string, but you can use this instruction to do it:

lineCount = CountFields(TextArea1.Text, Chr(13)) [/quote]

i have not idea there is a CountFields function.

I also only recently discovered CountFields. Looks like a useful function.

sort of similar to ubound of a split??

Yes, but its 1-based. It’s meant to be used in conjunction with the NthField method.

does the countfields take into account if there isn’t no line feed in the textarea?? one big paragraph of text??

a “line” is something with an “endofline”… therefore no linefeed and/or carriage return… then it is “one” line

You’d have to write code based on the font and width of the textarea to count “displayed lines” vs “physical lines”

I’ve just done a bunch of work with a text area and found some very useful functions that I think will give you want you want.

So this is what you do in the code for the text area:

me.selstart = me.len  ' Put the selection cursor at the end of the text

Dim LineNumber as integer = me.LineNumAtCharPos(me.selstart)

You now have the number of lines in the text area.

No declares needed.

Dim LineNumber as integer = me.LineNumAtCharPos(me.len)

won’t move the selection area or cursor

[quote=137425:@Dave S] Dim LineNumber as integer = me.LineNumAtCharPos(me.len)
won’t move the selection area or cursor[/quote]
DOH! Much better. Not sure why I didn’t think of that.

amazing!! i didn’t know we can do that.

I didn’t either until a couple of weeks ago. I am trying to develop what is basically a VT-100 terminal emulator of sorts (since none of the existing projects out there work or are supported any longer). I needed to be able to determine what line I am on in a text area. So I spent some time digging around looking at the methods of a text area and found a number of useful things. Get the line number a particular character position is at, get the first character at a particular line number, etc. There’s a lot of things buried in the text area’s capabilities.

LineNumAtCharPos is extremely interesting too since it provides a way to detect soft return that a simple split does not.