Jump to content

regex read from ini splits into combobox


Recommended Posts

 

 

The regex pattern that the user entered in the ComboBox, here are sometimes separators in my regex patterns that I print to the ini file,
Separator : |
And an example of my regex pattern: (.com|\.net|\.org|\.info|\.biz|\.eu|\.fr|\.ch|\.kr|\.edu|\.us)(.*)

When I read from the ini file and set data to the combobox, the regex pattern divides into parts, how can I prevent this? Thanks.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>

$aDirPro = @ScriptDir & "\IniFolder"
If FileExists($aDirPro) = 0 Then DirCreate($aDirPro)
$SectionName = "RegexPatterns"

$Form1 = GUICreate("Form", 500, 200)
$ComboSelectIniFile = GUICtrlCreateCombo("SelectIniFile", 160, 24, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$ComboLoadPattern = GUICtrlCreateCombo("SelectPattern", 16, 56, 473, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$ButtonSaveIni = GUICtrlCreateButton("SaveRegex", 16, 100, 475, 70)
GUISetState(@SW_SHOW)
_FUNC_INI_READ()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $ComboSelectIniFile
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If GUICtrlRead($ComboSelectIniFile) <> 'SelectIniFile' Or GUICtrlRead($ComboSelectIniFile) > 0 Then
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', ''))
            EndIf

        Case $ButtonSaveIni
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If Not GUICtrlRead($ComboLoadPattern) = "" Then
                IniWrite($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', GUICtrlRead($ComboLoadPattern))
                MsgBox(0, "Saved Ok.", "Regex pattern Saved Ok.", 3)
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', ''))
            Else
                MsgBox(48, "Incorrect operation!", "Cannot be empty - Regex patterns...", 5)
                GUICtrlSetState($ComboLoadPattern, $GUI_FOCUS)
            EndIf

    EndSwitch
WEnd

Func _FUNC_INI_READ()
    Local $INI_READ
    Local $VAR_READ
    $aIni_List = _FileListToArray($aDirPro, "*.ini", 1)
    $sIni_List = ""
    If Not @error Then
        For $i = 1 To $aIni_List[0]
            $sIni_List &= "|" & StringTrimRight($aIni_List[$i], 4)
        Next
        GUICtrlSetData($ComboSelectIniFile, $sIni_List)
    EndIf
EndFunc   ;==>_FUNC_INI_READ

 

Spoiler

rOtuWgB.png

 

Edited by youtuber
Link to comment
Share on other sites

You can change the default separator with

AutoItSetOption('GUIDataSeparatorChar', '<separator>')

Here is a full example:

AutoItSetOption('GUIDataSeparatorChar', '^')

$hMain = GUICreate("Example", 400, 200)
$cCombo = GUICtrlCreateCombo('', 10, 10, 380, 20)
GUICtrlSetData($cCombo, '(.com|\.net|\.org|\.info|\.biz|\.eu|\.fr|\.ch|\.kr|\.edu|\.us)(.*)')
GUISetState(@SW_SHOW, $hMain)

Do
    Sleep(10)
Until GUIGetMsg() = -3

You can even have a specialized function to set data in combo with a certain separator and then restore the default separator:

$hMain = GUICreate("Example", 400, 200)
$cCombo = GUICtrlCreateCombo('', 10, 10, 380, 20)
SetCombo($cCombo, '(.com|\.net|\.org|\.info|\.biz|\.eu|\.fr|\.ch|\.kr|\.edu|\.us)(.*)', '^')
GUICtrlSetData($cCombo, 'Item 1|Item 2')
GUISetState(@SW_SHOW, $hMain)

Do
    Sleep(10)
Until GUIGetMsg() = -3

Func SetCombo($cCombo, $sData, $sSeparator)
    Local $sPrevSeparator = AutoItSetOption('GUIDataSeparatorChar', $sSeparator)
    GUICtrlSetData($cCombo, $sData)
    AutoItSetOption('GUIDataSeparatorChar', $sPrevSeparator)
EndFunc

 

Edited by Andreik
Link to comment
Share on other sites

It was just an example. You can pick whatever you want as separator.

$hMain = GUICreate("Example", 400, 200)
$cCombo = GUICtrlCreateCombo('', 10, 10, 380, 20)
SetCombo($cCombo, '(.com|\.net|\.org|\.info|\.biz|\.eu|\.fr|\.ch|\.kr|\.edu|\.us)(.*)', Chr(5))
GUICtrlSetData($cCombo, 'Item 1|Item 2')
GUISetState(@SW_SHOW, $hMain)

Do
    Sleep(10)
Until GUIGetMsg() = -3

Func SetCombo($cCombo, $sData, $sSeparator)
    Local $sPrevSeparator = AutoItSetOption('GUIDataSeparatorChar', $sSeparator)
    GUICtrlSetData($cCombo, $sData)
    AutoItSetOption('GUIDataSeparatorChar', $sPrevSeparator)
EndFunc

 

Edited by Andreik
Link to comment
Share on other sites

same problem persists when i try by reading from ini file

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>

$aDirPro = @ScriptDir & "\IniFolder"
If FileExists($aDirPro) = 0 Then DirCreate($aDirPro)
$SectionName = "RegexPatterns"

$Form1 = GUICreate("Form", 500, 200)

$ComboSelectIniFile = GUICtrlCreateCombo("SelectIniFile", 160, 24, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$ComboLoadPattern = GUICtrlCreateCombo("SelectPattern", 16, 56, 473, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
SetCombo($ComboLoadPattern, '(.com|\.net|\.org|\.info|\.biz|\.eu|\.fr|\.ch|\.kr|\.edu|\.us)(.*)', Chr(5))
GUICtrlSetData($ComboLoadPattern, 'Item 1|Item 2')

$ButtonSaveIni = GUICtrlCreateButton("SaveRegex", 16, 100, 475, 70)
GUISetState(@SW_SHOW)
_FUNC_INI_READ()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $ComboSelectIniFile
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If GUICtrlRead($ComboSelectIniFile) <> 'SelectIniFile' Or GUICtrlRead($ComboSelectIniFile) > 0 Then
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', ''))
            EndIf

        Case $ButtonSaveIni
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If Not GUICtrlRead($ComboLoadPattern) = "" Then
                IniWrite($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', GUICtrlRead($ComboLoadPattern))
                MsgBox(0, "Saved Ok.", "Regex pattern Saved Ok.", 3)
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', ''))
            Else
                MsgBox(48, "Incorrect operation!", "Cannot be empty - Regex patterns...", 5)
                GUICtrlSetState($ComboLoadPattern, $GUI_FOCUS)
            EndIf

    EndSwitch
WEnd

Func SetCombo($cComboX, $sData, $sSeparator)
    Local $sPrevSeparator = AutoItSetOption('GUIDataSeparatorChar', $sSeparator)
    GUICtrlSetData($ComboLoadPattern, $sData)
    AutoItSetOption('GUIDataSeparatorChar', $sPrevSeparator)
EndFunc

Func _FUNC_INI_READ()
    Local $INI_READ
    Local $VAR_READ
    $aIni_List = _FileListToArray($aDirPro, "*.ini", 1)
    $sIni_List = ""
    If Not @error Then
        For $i = 1 To $aIni_List[0]
            $sIni_List &= "|" & StringTrimRight($aIni_List[$i], 4)
        Next
        GUICtrlSetData($ComboSelectIniFile, $sIni_List)
    EndIf
EndFunc   ;==>_FUNC_INI_READ

 

Spoiler

1XQaUy8.png

 

Link to comment
Share on other sites

Case $ComboSelectIniFile
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If GUICtrlRead($ComboSelectIniFile) <> 'SelectIniFile' Or GUICtrlRead($ComboSelectIniFile) > 0 Then
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', '')) ; ????
            EndIf

        Case $ButtonSaveIni
            $Ini_FileNameReadCombo = $aDirPro & '\' & GUICtrlRead($ComboSelectIniFile) & '.ini'
            If Not GUICtrlRead($ComboLoadPattern) = "" Then
                IniWrite($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', GUICtrlRead($ComboLoadPattern))
                MsgBox(0, "Saved Ok.", "Regex pattern Saved Ok.", 3)
                GUICtrlSetData($ComboLoadPattern, "")
                GUICtrlSetData($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', '')) ; ????

How do you expect to work if you don't use the function that change default separator?

SetCombo($ComboLoadPattern, IniRead($Ini_FileNameReadCombo, $SectionName, 'REGEXPATTERN', ''), Chr(5))

 

Edited by Andreik
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...