locked
File Commands In GUI??? RRS feed

  • Question

  • Can I create a command prompt to have a user enter in some data from the screen and have it write to a file and then later read the data in the file to the screen?

    But here is the catch: Can it be done in the graphis mode! Not the text window mode? I can not get this to work at all? GraphicsWindow. Are there commands that are different from the text read,write,append? If so what are they and what does the syntax look like. Code examples.

    Any help is appreciated. Thanks.

    Looks like very limited resources on the web as far as good code examples.
    Friday, July 22, 2011 5:05 PM

Answers

  • Probably easiest to use the controls method, then use a textbox option in this method.  Search for TextBox in the SmallBasic forum for some examples and have a go, then post back with your code so far if you have specific questions.  Very simple starter example below.

    textbox1 = Controls.AddTextBox(50,50)
    button1 = Controls.AddButton("Enter text",300,50)
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
     If (Controls.LastClickedButton = button1) Then
      input = Controls.GetTextBoxText(textbox1)
      GraphicsWindow.ShowMessage(input,"TextBox1 Text Entered")
     EndIf
    EndSub


    Friday, July 22, 2011 5:24 PM
  • EDIT,

    Missed the *i question - this is not a pointer (none of these nasty things in SB) - it is just 'multiplied by' the loop counter 'i' to position the textboxes with a 40 pixel spacing vertically.

     

    So if we want the data on one line of the file and have the data comma deliminated we need to do more work - this is not how SB naturally saves data.

    Note we are always putting the data on line 1, you could extend it to put the data on any line you want - more like records of a database.

    We could write a couple of subroutines to write and read the data with this format.  Note that it will fail if your text includes commas unless we do a bit more work!

     

    numTextbox = 5
    For i = 1 To numTextbox
     textbox[i] = Controls.AddTextBox(10,40*i)
    EndFor
    
    save = Controls.AddButton("Save Data",200,40)
    read = Controls.AddButton("Read Data",200,80)
    clear = Controls.AddButton("Clear Data",200,120)
    exit = Controls.AddButton("Exit",200,160)
    
    Controls.ButtonClicked = OnButtonClicked
    
    '====================================================
    'SUBROUTINES
    '====================================================
    
    Sub OnButtonClicked
     lastButton = Controls.LastClickedButton
     
     If (lastButton = exit) Then
     GraphicsWindow.ShowMessage("Exit Program","Information")
     Program.End()
     ElseIf (lastButton = save) Then
     writeData()
     ElseIf (lastButton = read) Then
     readData()
     ElseIf (lastButton = clear) Then
     For i = 1 To numTextbox
     Controls.SetTextBoxText(textbox[i],"")
     EndFor 
     EndIf
    EndSub
    
    Sub writeData 'Form the data to one comma separated list and write it to line 1 of the file
     line = ""
     For i = 1 To numTextbox
     line = line+Controls.GetTextBoxText(textbox[i])+","
     EndFor 
     File.WriteLine("C:\Check\TESTFILE.txt", 1, line)
    EndSub
    
    Sub readData 'Read the data from line 1 and parse the comma separated list to the contents for each textbox
     line = File.ReadLine("C:\Check\TESTFILE.txt", 1)
     i = 1
     pos = Text.GetIndexOf(line,",")
     While (pos > 0)
     txt = Text.GetSubText(line,1,pos-1)
     Controls.SetTextBoxText(textbox[i],txt)
     line = Text.GetSubTextToEnd(line,pos+1)
     pos = Text.GetIndexOf(line,",")
     i = i+1
     EndWhile
    EndSub
    Thursday, August 4, 2011 8:24 PM
  • Hi!

    Question 1:

    No, I don't think you can. Maybe in the future, or in an extension or something.

    Question 2:

    Visit http://smallbasic.com/doc/. However, this isn't really for 0.1. I could write up another Reference for you, would you like that?

    Question 3:

    If you want to do that on a new line, use Text.GetCharacter(10) Example:

    File.AppendContents(FilePath, Text.Append(Text.GetCharacter(10), "He wrote many plays."))


    ~~AirWaves!!~~
    Friday, August 5, 2011 9:38 PM

All replies

  • Probably easiest to use the controls method, then use a textbox option in this method.  Search for TextBox in the SmallBasic forum for some examples and have a go, then post back with your code so far if you have specific questions.  Very simple starter example below.

    textbox1 = Controls.AddTextBox(50,50)
    button1 = Controls.AddButton("Enter text",300,50)
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
     If (Controls.LastClickedButton = button1) Then
      input = Controls.GetTextBoxText(textbox1)
      GraphicsWindow.ShowMessage(input,"TextBox1 Text Entered")
     EndIf
    EndSub


    Friday, July 22, 2011 5:24 PM
  • Ok, I'll try this out.  Thank you.

     

    Friday, July 22, 2011 5:34 PM
  • In the event mode, how do I write more than one textboxes data to a file for append?

    textbox1 = some data entered in from user

    textbox2 = more data entered in from user

    Now how do I write to one file in sequential order?

    How to format my data file?  data.txt = nancy,tom,charle,mike  ect.....

    Next is the read from file command? 

    1. Why is it so hard to find syntax for Small Basic commands?

    2. When I do find some, the syntax doesn't seem to work or the examples given are not complete or don't work?

    Syntax:

     APPEND a, val [, val [, …]]
    a – An array-variable.  val – Any value or expression

    Example:

    APPEND 10 20   

    Doesn't work?????

     



    • Edited by Whalers1988 Wednesday, August 3, 2011 1:13 AM fix
    Wednesday, August 3, 2011 1:12 AM
  • 1] Suggest you use arrays to set the data from textboxes (the order will be defined by the array index) and then use File.WriteLine or File.AppendContents.

    Start with a simple text example to understand the file writing/reading and array syntax, then start putting it together when you understand how these work.

    2] The syntax for commands should come up with the intellisense, methods(functions) require brackets () and sometimes have arguments and return values.  Try:

    myText = Text.Append("Hello ","World")
    TextWindow.WriteLine(myText)



    Wednesday, August 3, 2011 5:33 PM
  • That will only work in text mode right?  I am trying to do this in GUI.  Can it be down with arrays in there also? 

    Another words:  I have a program that allows a user to enter in several items in different textboxes.  I have one button to save the data and another buuton to end the program

    1. This is the only way I understand how to create and use buttons with the mouse on click event ?  I would like to seperate them and have them work independently!

    2.  I am only getting data from one of the textboxes stored data in my file, not both data fields.  How do I accomplish this?   Below is my code.  Work in progress.

    numButton = 2
    For i = 1 To numButton
        button[i] = Controls.AddButton("Button ",200,80*i)
    EndFor
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      lastButton = Controls.LastClickedButton
      

      If (lastButton = button[2]) Then
        GraphicsWindow.ShowMessage("Exit Program"button [i])
         Program.End()
      EndIf
      
      If (lastButton = button[1]) Then
      GraphicsWindow.ShowMessage("Button 1"button [i])
     
      HeadingForTextBoxWindow = "Below is the information in the file that I entered in From Above!"
      File.WriteLine("C:\Check\TESTFILE.txt", 1, Controls.GetTextBoxText(textbox1))
      GraphicsWindow.ShowMessage(File.ReadContents("C:\Check\TESTFILE.txt"),HeadingForTextBoxWindow)
      EndIf
     
    EndSub
    textbox1 = Controls.AddTextBox(10,80)
    textbox2 = Controls.AddTextBox(10,120)

    button1 = Controls.AddButton("Save Data",920,600)

    Thursday, August 4, 2011 7:28 PM
  • First, it is generally best (not essential, but much easier to read) to put all of the subroutines after the main code.

    Button2 works correctly, ending the program

    Button1 also works correctly, writing the contents of textbox1 to line 1 of your file and reading it back to check with the ShowMessage - this is good prototyping.

    So far so good.

    So, if I understand correctly we want the contents of textbox1 in line 1 and texbox2 in line 2 of your file.  We can extend this the several textboxes with corresponding lines in the file using arrays for the textboxes and loop to do the saving etc.

    Lets start with 5 textboxes and the 2 buttons, one to save the data and one to end the program.

    numTextbox = 5
    For i = 1 To numTextbox
      textbox[i] = Controls.AddTextBox(10,40*i)
    EndFor

    add = Controls.AddButton("Save Data",200,40)
    exit = Controls.AddButton("Exit",200,80)

    Controls.ButtonClicked = OnButtonClicked

    Now the subroutine to handle the button presses, you already have the exit, to save the textbox data to separate lines of the file we just need to loop through the 5 texboxes and put the contents of each on a line of the file.

        For i = 1 To numTextbox
          File.WriteLine("C:\Check\TESTFILE.txt", i, Controls.GetTextBoxText(textbox[i]))
        EndFor

    Finally, we add another button to read the data back and put it back in the textboxes for test purposes, and a button to clear the textboxes.

    The whole thing:

    numTextbox = 5
    For i = 1 To numTextbox
     textbox[i] = Controls.AddTextBox(10,40*i)
    EndFor
    
    save = Controls.AddButton("Save Data",200,40)
    read = Controls.AddButton("Read Data",200,80)
    clear = Controls.AddButton("Clear Data",200,120)
    exit = Controls.AddButton("Exit",200,160)
    
    Controls.ButtonClicked = OnButtonClicked
    
    '====================================================
    'SUBROUTINES
    '====================================================
    
    Sub OnButtonClicked
     lastButton = Controls.LastClickedButton
     
     If (lastButton = exit) Then
      GraphicsWindow.ShowMessage("Exit Program","Information")
      Program.End()
     ElseIf (lastButton = save) Then
      For i = 1 To numTextbox
       File.WriteLine("C:\Check\TESTFILE.txt", i, Controls.GetTextBoxText(textbox[i]))
      EndFor 
     ElseIf (lastButton = read) Then
      For i = 1 To numTextbox
       Controls.SetTextBoxText(textbox[i],File.ReadLine("C:\Check\TESTFILE.txt", i))
      EndFor 
     ElseIf (lastButton = clear) Then
      For i = 1 To numTextbox
       Controls.SetTextBoxText(textbox[i],"")
      EndFor 
     EndIf
    EndSub

    Thursday, August 4, 2011 7:55 PM
  • Great but how can I combine the data in the file to one line?  And is it comma delimited?

    Also what is the *i is this a pointer to the array?  If so is it the address in memory?

     

    • Edited by Whalers1988 Thursday, August 4, 2011 8:07 PM Forgot more questions to it.
    Thursday, August 4, 2011 8:01 PM
  • EDIT,

    Missed the *i question - this is not a pointer (none of these nasty things in SB) - it is just 'multiplied by' the loop counter 'i' to position the textboxes with a 40 pixel spacing vertically.

     

    So if we want the data on one line of the file and have the data comma deliminated we need to do more work - this is not how SB naturally saves data.

    Note we are always putting the data on line 1, you could extend it to put the data on any line you want - more like records of a database.

    We could write a couple of subroutines to write and read the data with this format.  Note that it will fail if your text includes commas unless we do a bit more work!

     

    numTextbox = 5
    For i = 1 To numTextbox
     textbox[i] = Controls.AddTextBox(10,40*i)
    EndFor
    
    save = Controls.AddButton("Save Data",200,40)
    read = Controls.AddButton("Read Data",200,80)
    clear = Controls.AddButton("Clear Data",200,120)
    exit = Controls.AddButton("Exit",200,160)
    
    Controls.ButtonClicked = OnButtonClicked
    
    '====================================================
    'SUBROUTINES
    '====================================================
    
    Sub OnButtonClicked
     lastButton = Controls.LastClickedButton
     
     If (lastButton = exit) Then
     GraphicsWindow.ShowMessage("Exit Program","Information")
     Program.End()
     ElseIf (lastButton = save) Then
     writeData()
     ElseIf (lastButton = read) Then
     readData()
     ElseIf (lastButton = clear) Then
     For i = 1 To numTextbox
     Controls.SetTextBoxText(textbox[i],"")
     EndFor 
     EndIf
    EndSub
    
    Sub writeData 'Form the data to one comma separated list and write it to line 1 of the file
     line = ""
     For i = 1 To numTextbox
     line = line+Controls.GetTextBoxText(textbox[i])+","
     EndFor 
     File.WriteLine("C:\Check\TESTFILE.txt", 1, line)
    EndSub
    
    Sub readData 'Read the data from line 1 and parse the comma separated list to the contents for each textbox
     line = File.ReadLine("C:\Check\TESTFILE.txt", 1)
     i = 1
     pos = Text.GetIndexOf(line,",")
     While (pos > 0)
     txt = Text.GetSubText(line,1,pos-1)
     Controls.SetTextBoxText(textbox[i],txt)
     line = Text.GetSubTextToEnd(line,pos+1)
     pos = Text.GetIndexOf(line,",")
     i = i+1
     EndWhile
    EndSub
    Thursday, August 4, 2011 8:24 PM
  • Thanks a lot.  Just a few more questions if you don't mind.

    1. Is there a step by step debugger, where I can view the values being placed into the variables,arrays, ect, during execution?

    Glad to hear about the nightmare of pointers and things.

    What I used to like was Quick Basic.  It kept programming very simple, but yet very powerful.  I wished we could have kept that and built in a few of the new things. 

    2.  Is there a reference guide other than the one on this website that has examples of all the commands for SB?  That would be great.  I have looked online, but I am not comming up with too much.  Also there is another program named Small Basic as well?  How do I know if I am getting the correct commands for this SB, if I am doing a web search.  Gets a little intense looking for the answers. 

    3.  File.AppendContents(FilePath, "He wrote many plays."))
    Can I pass a variable here instead of just a text?  Example:

    File.AppendContents(FilePath, line)  ??? This is not allowed?

    When I try to use same program to append new data to the same file on a new

    line in the file, I can not do with the Append command???

    I have no idea how I am going to read one line of data from the file each time.  Ugh.

    You have been a great help.  Thank you so much.  I'm sure I'll have more questions in the future.

    Thanks again.

     


    • Edited by Whalers1988 Friday, August 5, 2011 7:12 PM More Questions!!
    Friday, August 5, 2011 5:27 PM
  • Hi!

    Question 1:

    No, I don't think you can. Maybe in the future, or in an extension or something.

    Question 2:

    Visit http://smallbasic.com/doc/. However, this isn't really for 0.1. I could write up another Reference for you, would you like that?

    Question 3:

    If you want to do that on a new line, use Text.GetCharacter(10) Example:

    File.AppendContents(FilePath, Text.Append(Text.GetCharacter(10), "He wrote many plays."))


    ~~AirWaves!!~~
    Friday, August 5, 2011 9:38 PM
  • I also wanted a "reference guide".

    But there wasn't what i wanted.

    So I'm making a guide now. 

    Please refer below.

    LVX800 From "Aero" to "GameControls" 

    XDM400 From "GameKeyboard" to "Turtle"

    Tuesday, August 9, 2011 12:45 AM
    Answerer
  • Keep in mind that Aero is not part of the Standard Small Basic library. Neither are any of the Data Extension or FC Libraries.
    ~~AirWaves!!~~
    Tuesday, August 9, 2011 4:36 AM