MultiLine StringGadget

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

MultiLine StringGadget

Post by BackupUser »

Code updated for 5.20+. Same as EditorGadget()

Restored from previous forum. Originally posted by PB.

Variations on this have been posted before, but they are long and may
confuse newbies... so here's a simple demonstration.

Code: Select all

If OpenWindow(0,200,200,300,200,"test",#PB_Window_SystemMenu)
  t$="This text goes inside a multiline StringGadget."+Chr(13)+Chr(10)
  For r=1 To 10 : t$+Str(r)+Chr(13)+Chr(10) : Next
  StringGadget(0,10,10,200,100,t$,#ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

nice one. Thanks.

Btw: how do I save and load the contents of this Gadget? If I use WriteStringN(t$) it is saved correctly. But how do I read it again? I tried it simply by t$=ReadString() but then it only reads the first line. How do I know the number of lines?

Of course I could use eof(), but I want to save the contents of other gadgets - line by line.

regards,
Redacid
---
Viva Los Tioz!
registered user of PB3.50 on WinXP SP1
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

RedAcid, if you use the below modification with the two extra 'disable' flags, you will get a stringgadget that 'wraps' around the visible window and is treated as a single string by GetGadgetText. I do not believe (and have been wrong before) that there is a way to get individual lines from a multiline stringgadget so adopted the wrapping approach that works.

Normally, for a text box to have multiple lines and be able to retreive individual lines, you would have to use a RichEdit box.

StringGadget(#Mybox,93,130,440,135,"",#ES_MULTILINE | #ESB_DISABLE_LEFT | #ESB_DISABLE_RIGHT | #ES_AUTOVSCROLL | #WS_VSCROLL)


Fangles woz ear orright den?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

hmmm...still doesn't work as it should (or as I want it to be...). Maybe I didn't make my problem clear enough.

My input in the stringgadget is for example:

Bill Gates
Redmond
USA

Now I save it with WriteStringN(GetGadgetText(0)). The resulting Textfile contains the text as shown above (3 lines). So if I try to read it again using ReadString() it only reads "Bill Gates".



regards,
Redacid
---
Viva Los Tioz!
registered user of PB3.50 on WinXP SP1
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Try something like this:

Code: Select all

Text.s = Space(Lof())  ; allocate some Memory (the length of the File)
ReadData(@Text, Lof()) ; read whole file to Text.s (including all the Line breaks)
SetGadgetText(0, Text) ; update StringGadget
Didn't test it, but it should work.

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

strange, I am writing to a database using my modified tip above and I have checked the data and it is all there.

I enter the same was as you do, pressing enter after each line and then writing the string and reading later on and I can see all 3 lines exactly as they were.

I am writing it back to the same sort of multiline, wrap enabled string gadget though so it interprets the invisible line returns correctly and displays them.

When you did you read back, were you printing the text back to the same sort of gadget by any chance? (because that would have worked perfectly I think)

Correct me if I am wrong (and I usually am)


Fangles woz ear orright den?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Redacid, here's a better example with more info about the StringGadget:

Code: Select all

If OpenWindow(0,200,200,300,150,#PB_Window_SystemMenu,"test")
  CreateGadgetList(WindowID())
  t$="This text goes inside a multiline StringGadget."+Chr(13)+Chr(10)
  For r=2 To 10 : t$+Str(r)+Chr(13)+Chr(10) : Next
  StringGadget(0,10,10,200,100,t$,#PB_String_Multiline|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL)
  ButtonGadget(1,230,10,50,20,"Save")
  ButtonGadget(2,230,40,50,20,"Load")
  ButtonGadget(3,230,70,50,20,"Info")
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget
      Select EventGadgetID()
        Case 1 ; Save
          If CreateFile(0,"MultiLine.txt")
            WriteStringN(GetGadgetText(0))
            CloseFile(0)
          EndIf
        Case 2 ; Load
          If ReadFile(0,"MultiLine.txt")
            a$=""
            Repeat
              a$+ReadString()+Chr(13)+Chr(10)
            Until Eof(0)0
            CloseFile(0)
            SetGadgetText(0,a$)
          EndIf
        Case 3 ; Info
          lines=SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
          where=SendMessage_(GadgetID(0),#EM_LINEFROMCHAR,-1,0)+1
          MessageRequester("Info",Str(lines)+" lines in the box."+Chr(13)+"You are on line "+Str(where)+".",0)
      EndSelect
    EndIf
  Until ev=#PB_Event_CloseWindow
EndIf
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Jondo.

thanks for the tip PB, and can you please tell me where can i see all the other flags execept those wrriten in PB Help file?

Jondo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> can you please tell me where can i see all the other flags execept
> those wrriten in PB Help file?

I use a Visual Basic document that has a lot of Win32 API flags in it,
which I downloaded from here (143k):

http://www.mvps.org/vb/code/Win32api.zip
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Originally posted by PB


lines=SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
where=SendMessage_(GadgetID(0),#EM_LINEFROMCHAR,-1,0)+1
MessageRequester("Info",Str(lines)+" lines in the box."+Chr(13)+"You are on
*PB*

I've never seen those two constants but am not surprised I didn't know about them. Your method is the best one for individual items of text (as redacid seems to be doing) and mine is better for long strings of text wrapped within a window, with or without line feed chars etc.

*redacid*

Pardon me for asking but why sepearate items in one string gadget? Why not in separate fields (gadgets) for ease of use? I just find that way a little messier to debug (and I make many big mistakes)

regards.

Fangles woz ear orright den?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

I am coding a little database for my DVDs. I have some "single-line-string-gadgets" for title, actors, director and so on. And then some multi-line-gadgets for remarks/trivia.

I save the contents of the gadgets with

WriteStringN(GetGadgetText(0))
WriteStringN(GetGadgetText(1))
WriteStringN(GetGadgetText(2))
WriteStringN(GetGadgetText(3))

and so on.

That's why I have trouble with reading the file if one of the gadgets is multiline.

But I didn't test the example above - no time. But tonight I will see if it works better than my code...

Thanks for all your help!

regards,
Redacid
---
Viva Los Tioz!
registered user of PB3.50 on WinXP SP1
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

PB: thanks. With your code it seems that I am finally able to work it out!

regards,
Redacid
---
Viva Los Tioz!
registered user of PB3.50 on WinXP SP1
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Originally posted by redacid


That's why I have trouble with reading the file if one of the gadgets is multiline.
I think I know why too. With PB's flags only, you are creating a true multine stringgadget (at least, that's how it appears to the GetGadgetText call) so when you try reading it back, there must be an EOL (End Of Line) char added to it.

With mine, it creates a SINGLE string with embedded carriage return/line feeds that is passed through (and not interpreted) by GetGadgetText so that EOL is written after the whole lot and that's why GetGadgetText grabs the whole lot when you try to read it back. When I place the text into a string gadget with created with #ESB_DISABLE_LEFT | #ESB_DISABLE_RIGHT (and grabbed from the same originally), it interprets all the CR/LF's and displays them exactly as you typed them.

Oh well, many ways to do the same job, all of them interesting

Fangles woz ear orright den?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.

@PB: another german user told me, that reading gadget contents with GetGadgetText() is limited to 4,999 Bytes.

So I tested today your Multiline example, if that works correctly. And - while anything works fine - it saves also only max. 4,999 Byte in the Multiline.txt file :cry:

So the question is, if there is a generally (Windows) limit to such text/string functions or if their is a PureBasic restriction/bug ???


Regards
André

*** German PureBasic Support ***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> So the question is, if there is a generally (Windows) limit to such
> text/string functions or if their is a PureBasic restriction/bug ???

PureBasic used to have a 5k limit on string variables, so perhaps this
is why StringGadgets can only save the first 5k (Fred?). I tested it
and yes, only the first 5k is saved (using WriteStringN(GetGadgetText(0))).

So it's either WriteStringN() having a 5k limit, or GetGadgetText().
I note, using my code above, that I can load 64k into a StringGadget,
but that may be simply due to the current string limitation of 64k.

In Visual Basic, a multiline textbox can hold up to 32k of data only.
Post Reply