Number of Lines

Just starting out? Need help? Post your questions and find answers here.
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Number of Lines

Post by tbohon »

Is there a built-in way to retrieve the number of lines in a multiline stringgadget? I need to be able to pull the last line and then, later on, append a new line to the text.

Thanks in advance as always.

Tom
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Number of Lines

Post by Fluid Byte »

Use the EditorGadget :wink:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Number of Lines

Post by #NULL »

maybe a bit goofy, but why not like this

Code: Select all

CountString(GetGadgetText(0), #LF$) + 1
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Number of Lines

Post by Blood »

#NULL wrote:maybe a bit goofy, but why not like this

Code: Select all

CountString(GetGadgetText(0), #LF$) + 1
Is this cross-platform?
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Number of Lines

Post by #NULL »

it should work with windows (#CRLF) and linux (#LF). with some CompilerIfs it will work on mac os (#CR) too.

<edit>
tbohon wants to add a newline anyway, so in that case he will have to detect compiler_os to choose the appropiate newline character for all operations.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Number of Lines

Post by RASHAD »

@tbohon Hi

Take 'Fluid Byte' advice seriously
But if for some reasons you want to use StringGadget()
Next snippet For Windows

Compile it In Unicode mode

Code: Select all


Text$ = "Text in Multiline StringGadget with WordWrap and Line Number"
Linet$ = Space(#MAX_PATH)
OpenWindow(0,0,0,300,240,"String Gadget with WordWrap",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE|#ESB_DISABLE_BOTH)
ButtonGadget(1,10,200,80,22,"Replace")
SetGadgetFont(0,LoadFont(0,"Arial",12))

NL = SendMessage_(GadgetID(0), #EM_GETLINECOUNT,0,0)         ;Get No. of Lines
Debug NL
SendMessage_(GadgetID(0), #EM_GETLINE,Nl-1,@Linet$)          ;Get the text in the last line
Debug Linet$

Repeat
   EventID = WaitWindowEvent()
   
   Select EventGadget()
      Case 0
      
      Case 1
        If Run = 0
            SetGadgetText(0,ReplaceString(GetGadgetText(0),RTrim(Linet$),"New String",1))  ;Replace Lastline Text with New
            Run = Run +1
        EndIf
        
   EndSelect
Until EventID = #PB_Event_CloseWindow

Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Number of Lines

Post by charvista »

@RASHAD Hi
#EM_GETLINECOUNT works fine but #EM_GETLINE to get the last line does not work ?
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Number of Lines

Post by RASHAD »

@charvista Hi
It is OK here PB 4.51 x86 Win 7 x64
What is your configurations?
And Remember to compile using Unicode
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Number of Lines

Post by charvista »

@RASHAD Hi
Config: PB 4.50 x86 Vista x86
You are right. All my programs are compiled in Unicode, except when testing your code.
Compiled in Unicode gives correct results.
My fault, sorry.
Thanks Rashad ;)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply