Page 1 of 1

[DONE] Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 4:10 pm
by PresFox
Hello,

I have a stringgadget in my window, and i want to take the input from it and write it to a file.

I am not sure how to go about this.

I have tried this:

Code: Select all

Procedure Open_Window_1()
  If OpenWindow(#Window_1, 308, 114, 391, 76, "New window ( 1 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_1))
      StringGadget(#String_0, 20, 10, 340, 30, newlander$)
      ButtonGadget(#Button_3, 290, 50, 70, 20, "add")
      
    EndIf
  EndIf
EndProcedure
and this is the in the main loop:

Code: Select all

 ElseIf GadgetID = #Button_3
    
    ; add a landing page to the list
    
    If OpenFile(0, "Landers.dat")    ; opens an existing file or creates one, if it doesn't exist yet
    FileSeek(0, Lof(0))         ; jump to the end of the file (result of Lof() is used)
    WriteStringN(0, newlander$)
    CloseFile(0)
  EndIf
The file gets created, but nothing is written in it?

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 4:14 pm
by gnasen
As far as I can see there may be different mistakes, however I can only guess, because the code is not working.
Pleass post next time copy-able code which can be run immediatly.

1. newlander$ seems to be an empty string
2. #String_0, #Button_3 may have the same value

A very very useful command is by the way enable explicit. It forces you to declare every variable. At first it seems annoying, but you wont miss it in future.

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 4:16 pm
by Arctic Fox
Have you checked the value of the newlander$ string? (send it to the debug output window using 'Debug')

If you want the contents of the StringGadget you should use GetGadgetText().
For instance

Code: Select all

WriteStringN(0, GetGadgetText(#String_0))
Edit
Beaten by gnasen :lol:

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 4:17 pm
by KJ67
You may be looking for GetGadgetText()?

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 5:13 pm
by PresFox
Thanks, indeed i was!

Works :)

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 6:18 pm
by Michael Vogel
Hi PresFox, welcome to the board :wink:

Just a note how to indicate that a question has been solved: just edit your first posting and write something like "[Done] " at the front of the subject...

Michael

Re: Writing to a file from a stringgadet

Posted: Sat Apr 03, 2010 11:54 pm
by UserOfPure
Putting [Done] at the start just means better solutions (when possible) may be ignored, though. People with better solutions may just skip the thread. Something to be aware of. I wouldn't be so quick to close off future solutions to lurkers.