Announcement

Collapse
No announcement yet.

How to write data in a txt

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

  • How to write data in a txt

    Hello, first of all, excuse my English, I am using a translator.


    I am trying to manage to read certain lines of a txt, which I have managed to achieve and make the line I want appear in a ParagraphX, now what I need is to write the value of a combobox when selecting it and update paragraph1 with the new information written, I have tried in several ways but I cannot continue the project, can someone help me? I am somewhat inexperienced in all this.​​

  • #2
    Does this help.

    Within the project is a text file
    AutoPlay\\Docs\\TextFile.txt
    A button set to open the text file. ( this code could be added to the combo box code if desired )

    A combo box
    Code:
    -- Gets the index of the selected item.
    selected_index = ComboBox.GetSelected("ComboBox1");
    
    -- Gets the item text of the selected item.
    item_text = ComboBox.GetItemText("ComboBox1",selected_index);
    
    -- Writes the text file, adds the new line to the bottom of the text file without overriding the text already there.
    TextFile.WriteFromString("AutoPlay\\Docs\\TextFile.txt","r\n".. item_text, true);
    ​

    Comment


    • #3
      Does this help

      Code:
      -- Gets the index of the selected item.
      selected_index = ComboBox.GetSelected("ComboBox2");
      
      -- Gets the item text of the selected item.
      item_text = ComboBox.GetItemText("ComboBox2",selected_index);
      
      
      -- Get the paragraph properties
      tProperties = Paragraph.GetProperties("Paragraph1");
      
      -- Get the currently displayed text
      sText = tProperties.Text
      
      -- Adds the Combo Box Text to the existing Paragraph
      Paragraph.SetText("Paragraph1",sText.."\r\n".. item_text);​

      Comment


      • #4
        This may help also

        Code:
        Paragraph.SetText("Paragraph2", "");
        
        
        TextFileString = TextFile.ReadToString("AutoPlay\\Docs\\TextFile.txt");
        
        
        -- Gets the index of the selected item.
        selected_index = ComboBox.GetSelected("ComboBox3");
        
        -- Gets the item text of the selected item.
        item_text = ComboBox.GetItemText("ComboBox3", selected_index);
        
        -- Adds the Combo Box Text to the existing Paragraph
        Paragraph.SetText("Paragraph2",TextFileString..item_text);​

        Unless i am reading it wrong

        Comment


        • #5
          Hi bbypka and welcome to the forum,
          I think you should have posted some code so we can follow what you are trying to do.

          I think this is maybe what you are after

          ComboBox2Para.apz

          Cheers

          Comment


          • #6
            I can't include the code because honestly, as I said, I'm somewhat inexperienced and what I've done so far is trying various codes both from the internet and from manuals.

            What I want to do is:

            1 - Read X line of a txt (Line 5 for example) and show the data in a ParagraphX, I have already achieved this thanks to people like you and some manual.

            From here is where I get stuck:

            2 - A combobox that when selecting an item writes the selected value in X line of the txt (Line 5 for example)

            3 - If possible, it is not essential, that the Paragraph of point 1 be updated with the new value written by the combobox of point 2

            4 - If point 3 is not possible, a button that updates/refreshes the paragraph of point 1 would be good​

            Comment


            • #7
              Well this is interesting me but like colc says, not too sure on what exactly you are trying to do.

              Hoping colc does not mind, I have put together an example too.

              bbypka Project - 1.apz

              How I read it you have a text file with information on.
              You want this information to show up in a paragraph.
              You want a combo box which already has information stored in it. You want to make a selection from the combo box, have it show up in the paragraph which represents the actual text file and have the combo box selection to be added to the text file. Which is your end result.

              I added a text file holding names of some computer folders.
              When the app is run it reads the text file and loads the information into the paragraph with no interaction from the user.
              The combo box holds the names of car manufactures, Ford, BMW etc. When a selection is made it updates the paragraph and writes the information to the text file.

              Is this what you are after ?

              Maybe more info from yourself could help out.

              As you can tell my script are very basic compared to colc's. I wish I could understand more.

              Comment


              • #8
                Hi Old Fellow, I think he wants a line in the text file to be shown in a paragraph then updated with what is selected in the data of combobox and written back to text file
                here is an update :
                ComboBox2Para_Mod.apz

                PS I don't think he checked my first example

                Comment


                • #9
                  Originally posted by colc View Post
                  Hi Old Fellow, I think he wants a line in the text file to be shown in a paragraph then updated with what is selected in the data of combobox and written back to text file
                  here is an update :
                  [ATTACH]n309650[/ATTACH]

                  PS I don't think he checked my first example
                  Pretty darn neat colc.... Added that to my collection for future reference.

                  Comment


                  • #10
                    First of all, thank you very much for your interest, let's see if I can explain myself and remember that I use a translator, maybe some word will dance a bit XD

                    First copy the file test.txt in C:

                    When opening the project in Paragraph1 it shows line 4 of the txt, so far fine, although it is a bit slow to display it XD

                    What I would like to do now is that when selecting an option from the Combobox (whatever it is) line 4 of the txt is modified by writing the value of the selected item data, that is:

                    If I select "Red" that changes line 4 of the txt (put what it puts) by Line4=Red
                    If I select "Green" that changes line 4 of the txt (put what it puts) by Line4=Green
                    If I select "Blue" change line 4 of the txt (put whatever it puts) to Line4=Blue​
                    Attached Files

                    Comment


                    • #11
                      Sorry, I forgot, and if it is possible that when selecting the X option of the combobox, in addition to writing the Item Data on line 4, it refreshes the Paragraph to show the new value just written by the combobox.

                      Comment


                      • #12
                        Sending autoplay is NO good.

                        Go to File --> Export and export as an apz file

                        What is the
                        selecting the X option of the combobox
                        Did you check my apz ???

                        Comment


                        • #13
                          As you do not seem to know how to export or open apz files try this [It cannot be made simpler]

                          Code:
                          -- ################################################## ###########
                          -- SCRIPT: Page: Page1, Event: On Show Script
                          -- ################################################## ###########
                          
                          -- Path to File
                          --sFilePath = _SourceFolder.."\\Autoplay\\Docs\\test.txt"
                          
                          sFilePath = "C:\\test.txt"--You should not use C drive directory as you will need admin rights to write to file
                          
                          -- Check if File exists
                          bPath = File.DoesExist(sFilePath);
                          
                          if bPath then
                          --Read Text File
                          tFile = TextFile.ReadToTable(sFilePath);
                          --Get Contents of file
                          for i in pairs(tFile) do
                          sLine = tFile[4]; -- variable for table line 4
                          end
                          Paragraph.SetText("Paragraph1", sLine); -- Load Paragraph
                          else
                          Dialog.Message("Error", "Cannot find File", MB_OK, MB_ICONSTOP, MB_DEFBUTTON1);
                          ComboBox.SetVisible("ComboBox1", false); -- Hides Combobox
                          end
                          
                          
                          -- ################################################## ###########
                          -- SCRIPT: Page: Page1, Object: ComboBox1, Event: On Select Script
                          -- ################################################## ###########
                          
                          -- This returns the index number (each item of data is listed with a number, ie. 1..2..3..4..5..etc.)
                          index = ComboBox.GetSelected("ComboBox1")
                          
                          -- Use that number to get the data from the text that was selected
                          --(there is text and there is data, see the Combobox SETTINGS tab (left fields hold what you see, right the data)
                          selected = ComboBox.GetItemData("ComboBox1", index)
                          
                          
                          -- Set the Paragraph's text to the data selected!
                          Paragraph.SetText("Paragraph1", selected);
                          
                          
                          rLine = Table.Remove(tFile, 4); -- Removes Line 4
                          Table.Insert(tFile, 4, selected); -- Replaces Line 4 in Text File updated combobox Data
                          TextFile.WriteFromTable(sFilePath, tFile, false); -- Writes to text file (Saves file with new Data)
                          
                          File.Open(sFilePath, "", SW_SHOWNORMAL); -- Opens file for viewing​

                          Comment


                          • #14

                            Simply wonderful, perfect, thank you, thank you, thank you very much, sorry for not having exported the example, I didn't fall for it, I didn't realize it, I'm sorry, when I have finished the project, it will be more yours than mine XDD, I'll pass it on to you so that see the end result.

                            Thanks again, I've been trying for a long time.​

                            Finally say that what I try to do is not even for me XDDDD, it is for certain people who do not know how to do certain things (you will see what I mean with the final result) and I hope this helps those people, and thanks to I think this little code will now be possible to do.

                            Comment


                            • #15
                              A curiosity, it is not absolutely necessary and with what you have given me I have plenty and I thank you again, but, one question, would it be possible instead of searching and changing a line, searching and changing a line according to a text?

                              That is, could you find where the text is (example): Marker=XXX, where XXX can be anything, and change that entire line to what we put in the Combobox?​

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎