locked
Help with saving/writing files: Code might be wrong. RRS feed

  • Question

  • Right so basically I've written some code that is  beginning to form a text editor:

    GraphicsWindow.Show()
    GraphicsWindow.Title = ("NotePad")
    
    exit = Controls.AddButton ("EXIT", 15, 13)
    save = Controls.AddButton ("Save", 60, 13)
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
      Button = Controls.LastClickedButton
      If Button = exit Then
        Program.End()
      ElseIf Button = save    Then
        File.WriteContents(savedir, input)
    EndIf 
    EndSub
    
    inputbox = Controls.AddMultiLineTextBox (30, 50)
    Controls.SetSize(inputbox, 600, 450)
    input = Controls.GetTextBoxText(inputbox)
    
    directory = Controls.AddTextBox (105, 13)
    Controls.SetTextBoxText(directory, "Insert save directory")
    dir = Controls.GetTextBoxText (directory)
    type = Controls.AddTextBox (270, 13)
    Controls.SetTextBoxText(type, "File Type: 'txt' or 'html' etc")
    filetype = Controls.GetTextBoxText(type)
    
    savedir = dir + "." + filetype

    And for some reason it won't write the file to a drive, Any ideas? Is it because i've used a variable in the directory and contents?

    Import code:PBG922

    Thanks!


    Thursday, June 13, 2013 7:06 PM

Answers

  • If you look at your code, the dir and savedir variables are only set once at the start of the program, before the user has time to type anything in the text boxes.

    You need to read the variables after the user has entered something.  You can see the issue by adding a TextWindow.WriteLine debug statement inside the save section.

    Sub OnButtonClicked
      Button = Controls.LastClickedButton
      If Button = exit Then
        Program.End()
      ElseIf Button = save    Then
        File.WriteContents(savedir, input)
        TextWindow.WriteLine("savedir : "+savedir)
      EndIf 
    EndSub

    Perhaps something like:

    Sub OnButtonClicked
      Button = Controls.LastClickedButton
      If Button = exit Then
        Program.End()
      ElseIf Button = save    Then
        dir = Controls.GetTextBoxText (directory)
        filetype = Controls.GetTextBoxText(type)
        savedir = dir + "." + filetype
        input = Controls.GetTextBoxText(inputbox)
        File.WriteContents(savedir, input)
      EndIf 
    EndSub


    • Edited by litdev Thursday, June 13, 2013 8:15 PM
    • Marked as answer by igeorgetaylor Thursday, June 13, 2013 9:06 PM
    Thursday, June 13, 2013 8:10 PM

All replies

  • If you look at your code, the dir and savedir variables are only set once at the start of the program, before the user has time to type anything in the text boxes.

    You need to read the variables after the user has entered something.  You can see the issue by adding a TextWindow.WriteLine debug statement inside the save section.

    Sub OnButtonClicked
      Button = Controls.LastClickedButton
      If Button = exit Then
        Program.End()
      ElseIf Button = save    Then
        File.WriteContents(savedir, input)
        TextWindow.WriteLine("savedir : "+savedir)
      EndIf 
    EndSub

    Perhaps something like:

    Sub OnButtonClicked
      Button = Controls.LastClickedButton
      If Button = exit Then
        Program.End()
      ElseIf Button = save    Then
        dir = Controls.GetTextBoxText (directory)
        filetype = Controls.GetTextBoxText(type)
        savedir = dir + "." + filetype
        input = Controls.GetTextBoxText(inputbox)
        File.WriteContents(savedir, input)
      EndIf 
    EndSub


    • Edited by litdev Thursday, June 13, 2013 8:15 PM
    • Marked as answer by igeorgetaylor Thursday, June 13, 2013 9:06 PM
    Thursday, June 13, 2013 8:10 PM
  • Cheers ;) cannot believe I missed that! 
    Friday, June 14, 2013 5:21 PM