Announcement

Collapse
No announcement yet.

Listbox to Text file need help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Listbox to Text file need help

    Hello eveyone can someone help me how to same the entries from Listbox into a text file?
    here is my code it returns nothing..


    save = Dialog.FileBrowse(false, "Save As - [Participants Data]", _DesktopFolder.."\", "All Files (*.txt)|*.txt|", "", "txt", false, false);

    if (save ~= "CANCEL") then

    nRows = ListBox.GetCount("ListBox1");
    for i=1,nRows do

    datos = ListBox.GetItemText("ListBox1", i);


    TextFile.WriteFromString(saver[1].." - ".., datos, true);

    end

    end

  • #2
    The purpose of your initial Dialog.FileBrowse action is a little unclear. I think maybe Dialog.FolderBrowse is what you meant? Anyhow, try this on for size:

    Code:
    -- count total number of line-items in listbox
    local nRows = ListBox.GetCount("ListBox1");
    
    -- set destination for output of textfile, "Participant's Data"
    tPath = Dialog.FolderBrowse("Please select a destination for saving Participant's Data", _DesktopFolder);
    
    if (save ~= "CANCEL") then
        -- initiate a 'for loop' to grab all item-text from listbox            
        min = 1;
        max = nRows;
        for count = min, max do
            sData = ListBox.GetItemText("ListBox1", count);    
            -- and write results to textfile
            TextFile.WriteFromString(tPath .. "\\Participant's Data.txt", sData.."\r\n", true);
        end    
    end
    Nb.
    Tag your code samples, yeah?. Makes for much easier reading.
    (It's the little #hash symbol - 7th from the right)

    Comment


    • #3
      EDIT,

      Sorry, some very minor errors in the above code (LOL, must be getting late):
      • misnamed variable in Line 07
      • Dialog.FileBrowse returns a table but Dialog.FolderBrowse returns a string
      So, the whole code-block should really read as:
      (alterations are marked in red) :

      Code:
      -- count total number of line-items in listbox
      local nRows = ListBox.GetCount("ListBox1");
      
      -- set destination for output of textfile, "Participant's Data"
      [COLOR=#FF0000]s[/COLOR]Path = Dialog.FolderBrowse("Please select a destination for saving Participant's Data", _DesktopFolder);
      
      if ([COLOR=#FF0000]sPath[/COLOR] ~= "CANCEL") then
          -- initiate a 'for loop' to grab all item-text from listbox            
          min = 1;
          max = nRows;
          for count = min, max do
              sData = ListBox.GetItemText("ListBox1", count);    
              -- and write results to textfile
              TextFile.WriteFromString([COLOR=#FF0000]s[/COLOR]Path .. "\\Participant's Data.txt", sData.."\r\n", true);
          end    
      end

      Comment


      • #4
        Hello i used those code because i want the user can type the specific filename of for the save file...
        Example. Click save button.. then dialog.filebrowse Save. then the user will type the filename.. EG: NewDatos.txt

        Comment


        • #5
          Hello Bio, thanks for your example i already got it.. thanks so much..

          Comment


          • #6
            Originally posted by telco View Post
            i used those code because i want the user can type the specific filename of for the save file...
            Okay, I get you. You'd be better off then, by just adding in a function to display a Dialog.Input where the user can enter this info.

            The Dialog.FileBrowse is designed for selecting pre-existing files, not for dumping string-output. And although it may serve the purpose, is ultimately a 'buggy' way to do it.

            So, I'd use a Dialog.Input field (with error-checking) instead. By adjusting the whole code-block to something like this:

            Code:
            -- count total number of line-items in listbox
            local nRows = ListBox.GetCount("ListBox1");
            
            -- provide option for user to specify filename for outputted textfile
            sFileName = "";
            
            -- Loop until a filename is entered or the user cancels
            while (sFileName == "") and (sFileName ~= "CANCEL") do
                -- dispaly DialogInput asking user to enter a filename 
                sFileName = Dialog.Input("Save ListBox Data as", "Enter a filename:", "", MB_ICONINFORMATION);    
                
                -- if DialogInput left blank, display error message.    
                if sFileName == "" then
                    Dialog.Message("Oops!", "Please enter a filename for\r\nthe data you wish to save", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);   
                
                -- else if user doesn't cancel, then continue ...
                elseif sFileName ~= "CANCEL" then
                   
                    -- set destination for output of textfile.
                    sPath = Dialog.FolderBrowse("Please select a destination for saving "..sFileName, _DesktopFolder);
                    
                    if (sPath ~= "CANCEL") then
                           -- initiate a 'for loop' to grab all item-text from listbox            
                        min = 1;
                        max = nRows;
                        for count = min, max do
                            sData = ListBox.GetItemText("ListBox1", count);    
                            -- and write results to textfile using filename specified by user at DialogInput
                            TextFile.WriteFromString(sPath.."\\"..sFileName..".txt", sData.."\r\n", true);
                        end    
                    end
                end
            end

            Comment


            • #7
              EDIT

              @telco,
              Sorry dude, some superfluous code in the above example - just noticed it. (It'll still work but best to strip out the superfluous code and simplify for a cleaner result). So use this, instead:

              Code:
              -- count total number of line-items in listbox
              local nRows = ListBox.GetCount("ListBox1");
              
              -- dispaly DialogInput requesting filename for data output 
              sFileName = Dialog.Input("Save ListBox Data as", "Enter a filename:", "", MB_ICONINFORMATION);    
                  
              -- if DialogInput left blank, display error message.    
              if sFileName == "" then
                  Dialog.Message("Oops!", "Please enter a filename for\r\nthe data you wish to save", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);       
              
              -- else if user doesn't cancel, select destination for datafile output
              elseif sFileName ~= "CANCEL" then
                  sPath = Dialog.FolderBrowse("Select destination to save: "..sFileName, _DesktopFolder);
                  
                  -- and if user doesn't cancel, initiate 'for loop' to grab listbox data and write to file 
                  if (sPath ~= "CANCEL") then
                      for count = 1, nRows do
                          sData = ListBox.GetItemText("ListBox1", count);
                          TextFile.WriteFromString(sPath.."\\"..sFileName..".txt", sData.."\r\n", true);    
                      end
                  end
              end

              Comment


              • #8
                Thanks Bio all your shared code is working.. and really help me.. thanks so much..

                Comment

                Working...
                X