Jump to content

[SOLVED]StringSplit not working with ini file, need help =/


Recommended Posts

Hi,

I have been working on a program which among other things should save a string from a gui edit field to a ini file. Which is later loaded back to the edit field.

Since it is not possible to use line feeds in ini files I had to replace them with a string.

When loading the string again it will not read past the first line feed in the ini file.

For example, edit field:

this

is

a

test

I replace this to be "thisIITIIisIITIIaIITIItest"

When reading this back from the ini file I only get the first part of the string, "this".

I think it is because the stringsplit uses another text type which when saved keeps this typ. When read the ini file autoit does not like that a value it reads switched text type.

Simple program to demontstrate what happens:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 319, 140, 254, 124)
$Button1 = GUICtrlCreateButton("save", 32, 88, 129, 33, $WS_GROUP)
$Button2 = GUICtrlCreateButton("load", 176, 88, 121, 33, $WS_GROUP)
$Edit1 = GUICtrlCreateEdit("", 32, 16, 257, 73, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
$count = 0
$saving = ""

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If GUICtrlRead($Edit1) <> "" Then
                If GUICtrlRead($Edit1) <> "" Then
                    $solution_string = ""
                    $solution = StringSplit(GUICtrlRead($Edit1), Chr(10))
                    While $count <= $solution[0]
                        $solution_string = $solution_string & $solution[$count] & "IITII"
                        $count += 1
                    WEnd
                    $solution_string = StringTrimRight($solution_string, 5)
                    $solution_string = StringTrimLeft($solution_string, 6)
                    $saving = $saving & "solution=" & $solution_string & @LF
                    $count = 0
                EndIf
            EndIf
            IniWriteSection("Test.ini", "test", $saving)
        Case $Button2
            $solution = IniReadSection("Test.ini", "test")
            $solution_string = ""
            $solution = StringSplit($solution[1][1], "IITII")
            While $count <= $solution[0]
                $solution_string = $solution_string & $solution[$count] & @CRLF
                $count += 1
            WEnd
            $solution_string = StringTrimRight($solution_string, 1)
            $solution_string = StringTrimLeft($solution_string, 2)
            $count = 0
            GUICtrlSetData($Edit1, $solution_string)
    EndSwitch
WEnd

For some reason the code above adds to many line feeds when loading when typing in what to load in the ini file manually. (for example, adding the string "anotherIITIItest" manually to the ini file)

This code is taken from my program and it seems like it is adding the proper amount of line feeds there, but this is a test, so why bother with details. :)

Any help is much appreciated :mellow:

forum test.au3

Edited by bovanshi
Link to comment
Share on other sites

  • Moderators

bovanshi,

You need to set the "flag" parameter of StringSplit: "flag = 1, entire delimiter string is needed to mark the split". :)

This works for me - look for the <<<<<<<<<< lines as usual: :mellow:

#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 300, 150)
$Button1 = GUICtrlCreateButton("Save", 10, 110, 130, 30)
$Button2 = GUICtrlCreateButton("Load", 160, 110, 130, 30)
$Edit1 = GUICtrlCreateEdit("", 10, 10, 280, 90)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If GUICtrlRead($Edit1) <> "" Then
                $solution = StringSplit(GUICtrlRead($Edit1), @CRLF, 1) ; @CRLF is 2 chars <<<<<<<<<<<<<<<<<<<<<<<<<<
                $solution_string = $solution[1]
                For $i = 2 To $solution[0]
                    $solution_string &= "IITII" & $solution[$i]
                Next
            EndIf
            IniWrite("Test.ini", "test", "solution", $solution_string)
        Case $Button2
            $solution = IniRead("Test.ini", "test", "solution", "")
            If $solution Then
                $solution = StringSplit($solution, "IITII", 1) ; And here you  have 5 chars <<<<<<<<<<<<<<<<<<<<<<<<
                $solution_string = $solution[1]
                For $i = 2 To $solution[0]
                    $solution_string &= @CRLF & $solution[$i]
                Next
                GUICtrlSetData($Edit1, $solution_string)
            EndIf
    EndSwitch
WEnd

I also streamlined the loops a bit. Please ask if anything is unclear. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Nice, it works now, thanks!

I now realise how much I suck at programming, your code is soo much nicer than mine :mellow:

The rest of my code is about as clear and well designed as this XD

Now if you could clean up the rest of my messy program ;P

Edited by bovanshi
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...