Jump to content

[Solved] iniwrite multiple inputs (shorten code)


Recommended Posts

Could anybody be my hero and help me out? ^_^

Quick explanation:

I dont want:

IniWrite("TEST.INI","Section", $Name1 , GUICtrlRead($Input1))
IniWrite("TEST.INI","Section", $Name2 , GUICtrlRead($Input2))
IniWrite("TEST.INI","Section", $Name3 , GUICtrlRead($Input3))
...

I want to increase $Name and $Input by itself.

 

A week passed now and i cant figure this out,  since i'll hardtry to find solutions by myself.

Thanks in advance.

 

 

Link to comment
Share on other sites

Hi @p1xeL, and welcome to the AutoIt forum :)
If you have created a set of variables named $Name1...n, and $Input1...n, then you could easily make an array in which you put all the variables ( $Name1...n ) and another array in which you put inputs ( $Input1...n ), and loop them to write in the .ini file.

I think you could use another approach to do this... Maybe @Melba23 knows a little bit about this! :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Developers

Simply use an Array for the Names and the Inputs and use a for..Next loop to write them all into the ini.
Post your runable test script when you want us to assist to make the change.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I played around with arrays but i dont get how to loop it,  incase my approach is right.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <TabConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

$Form = GUICreate("Form", 310, 297, 4, 35)

Const $sAppIni=@AppDataDir&"\TEST.INI"

WinSetOnTop("Form","",True)

;#####################################################################################################

$maintab = GUICtrlCreateTab(0, 0, 550, 400)

;#####################################################################################################

GUICtrlCreateTabItem("Tab1")

$Input1 = GUICtrlCreateInput("", 2, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input1, 0)

$Input2 = GUICtrlCreateInput("", 42, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input2, 0)

$Input3 = GUICtrlCreateInput("", 82, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input3, 0)

$Input4 = GUICtrlCreateInput("", 122, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input4, 0)

$sb1    = GUICtrlCreateButton("Save",             1, 275, 150,  22)
$sb2    = GUICtrlCreateButton("Load",           152, 275, 150,  22)

$ec1        = GUICtrlCreateCombo("",              1, 254, 162,  22)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
_GUICtrlComboBox_SetCueBanner($ec1, "Name it")

GUISetState()



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
            Exit
         Case $sb1
            save()
         Case $sb2
            load()
            EndSwitch
         WEnd
         Func save()
            $ec1empty = GUICtrlRead($ec1)
         If $ec1empty = "" Then
            MsgBox(0, "", "Name it!")
         Else

Local $array1[4] = ["$Name1","$Name2","$Name3","$Name4"]
Local $array2[4] = ["$Input1","$Input1","$Input1","$Input1"]

;For $a = ? To ?
            IniWrite("TEST.INI","Section", $array1[$a], GUICtrlRead($array2[$a]))
Next
            EndIf
         EndFunc

         Func load()
            $readInput1 = IniRead("TEST.INI", "$Name1", "$Input1", "Error")
            GUICtrlSetData($Input1,$readInput1)
            $readInput2 = IniRead("TEST.INI", "$Name2", "$Input2", "Error")
            GUICtrlSetData($Input2,$readInput2)
            $readInput3 = IniRead("TEST.INI", "$Name3", "$Input3", "Error")
            GUICtrlSetData($Input3,$readInput3)
            $readInput4 = IniRead("TEST.INI", "$Name4", "$Input4", "Error")
            GUICtrlSetData($Input4,$readInput4)
            EndFunc

 

Link to comment
Share on other sites

Okay i figured it out myself. I just dont know where the "=0" in the ini file is coming from.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <TabConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

$Form = GUICreate("Form", 310, 297, 4, 35)

Const $sAppIni=@AppDataDir&"\TEST.INI"

WinSetOnTop("Form","",True)

;#####################################################################################################

$maintab = GUICtrlCreateTab(0, 0, 550, 400)

;#####################################################################################################

GUICtrlCreateTabItem("Tab1")

$Input1 = GUICtrlCreateInput("", 2, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input1, 0)

$Input2 = GUICtrlCreateInput("", 42, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input2, 0)

$Input3 = GUICtrlCreateInput("", 82, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input3, 0)

$Input4 = GUICtrlCreateInput("", 122, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input4, 0)

$sb1    = GUICtrlCreateButton("Save",             1, 275, 150,  22)
$sb2    = GUICtrlCreateButton("Load",           152, 275, 150,  22)

$ec1        = GUICtrlCreateCombo("",              1, 254, 162,  22)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
_GUICtrlComboBox_SetCueBanner($ec1, "Name it")

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
            Exit
         Case $sb1
            save()
         Case $sb2
            load()
            EndSwitch
         WEnd

         Func save()
            $ec1empty = GUICtrlRead($ec1)
         If $ec1empty = "" Then
            MsgBox(0, "", "Name it!")
         Else

      Local $array1[5] = ["$Name1","$Name2","$Name3","$Name4"]
      Local $array2[5] = [$Input1,$Input2,$Input3,$Input4]


      For $i = 0 To 4
            IniWrite("TEST.INI","Section", $array1[$i], GUICtrlRead($array2[$i]))
      Next
            EndIf
         EndFunc

         Func load()
            $readInput1 = IniRead("TEST.INI", "$Name1", "$Input1", "Error")
            GUICtrlSetData($Input1,$readInput1)
            $readInput2 = IniRead("TEST.INI", "$Name2", "$Input2", "Error")
            GUICtrlSetData($Input2,$readInput2)
            $readInput3 = IniRead("TEST.INI", "$Name3", "$Input3", "Error")
            GUICtrlSetData($Input3,$readInput3)
            $readInput4 = IniRead("TEST.INI", "$Name4", "$Input4", "Error")
            GUICtrlSetData($Input4,$readInput4)
            EndFunc

 

Link to comment
Share on other sites

Uhm ... nm found it :sweating:.

Final:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <TabConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

$Form = GUICreate("Form", 310, 297, 4, 35)

Const $sAppIni=@AppDataDir&"\TEST.INI"

WinSetOnTop("Form","",True)

;#####################################################################################################

$maintab = GUICtrlCreateTab(0, 0, 550, 400)

;#####################################################################################################

GUICtrlCreateTabItem("Tab1")

$Input1 = GUICtrlCreateInput("", 2, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input1, 0)

$Input2 = GUICtrlCreateInput("", 42, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input2, 0)

$Input3 = GUICtrlCreateInput("", 82, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input3, 0)

$Input4 = GUICtrlCreateInput("", 122, 55, 37, 30, BitOR($ES_CENTER,$ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GuiCtrlSetData($Input4, 0)

$sb1    = GUICtrlCreateButton("Save",             1, 275, 150,  22)
$sb2    = GUICtrlCreateButton("Load",           152, 275, 150,  22)

$ec1        = GUICtrlCreateCombo("",              1, 254, 162,  22)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
_GUICtrlComboBox_SetCueBanner($ec1, "Name it")

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
            Exit
         Case $sb1
            save()
         Case $sb2
            load()
            EndSwitch
         WEnd

         Func save()
            $ec1empty = GUICtrlRead($ec1)
         If $ec1empty = "" Then
            MsgBox(0, "", "Name it!")
         Else

      Local $array1[5] = ["$Name1","$Name2","$Name3","$Name4"]
      Local $array2[5] = [$Input1,$Input2,$Input3,$Input4]


      For $i = 0 To 3
            IniWrite("TEST.INI","Section", $array1[$i], GUICtrlRead($array2[$i]))
      Next
            EndIf
         EndFunc

         Func load()
            $readInput1 = IniRead("TEST.INI", "$Name1", "$Input1", "Error")
            GUICtrlSetData($Input1,$readInput1)
            $readInput2 = IniRead("TEST.INI", "$Name2", "$Input2", "Error")
            GUICtrlSetData($Input2,$readInput2)
            $readInput3 = IniRead("TEST.INI", "$Name3", "$Input3", "Error")
            GUICtrlSetData($Input3,$readInput3)
            $readInput4 = IniRead("TEST.INI", "$Name4", "$Input4", "Error")
            GUICtrlSetData($Input4,$readInput4)
            EndFunc

Thx FrancescoDiMuro & Jos for the tipp. ^_^

Link to comment
Share on other sites

Try this, it's much shorter.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <TabConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

$Form = GUICreate("Form", 310, 297, 4, 35)

Const $sAppIni = @AppDataDir & "\TEST.INI"

WinSetOnTop("Form", "", True)

;#####################################################################################################

$maintab = GUICtrlCreateTab(0, 0, 550, 400)

;#####################################################################################################

GUICtrlCreateTabItem("Tab1")
Global $array1[4] = ["$Name1", "$Name2", "$Name3", "$Name4"]

$array1[0] = GUICtrlCreateInput("", 2, 55, 37, 30, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GUICtrlSetData($array1[0], 0)

$array1[1] = GUICtrlCreateInput("", 42, 55, 37, 30, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GUICtrlSetData($array1[1], 0)

$array1[2] = GUICtrlCreateInput("", 82, 55, 37, 30, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GUICtrlSetData($array1[2], 0)

$array1[3] = GUICtrlCreateInput("", 122, 55, 37, 30, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 15, 800, 0, "Arial")
GUICtrlSetData($array1[3], 0)

$sb1 = GUICtrlCreateButton("Save", 1, 275, 150, 22)
$sb2 = GUICtrlCreateButton("Load", 152, 275, 150, 22)

$ec1 = GUICtrlCreateCombo("", 1, 254, 162, 22)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
_GUICtrlComboBox_SetCueBanner($ec1, "Name it")

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $sb1
            save()
        Case $sb2
            load()
    EndSwitch
WEnd

Func save()
    $ec1empty = GUICtrlRead($ec1)
    If $ec1empty = "" Then
        MsgBox(0, "", "Name it!")
    Else
        For $i = 0 To 3
            IniWrite("TEST.INI", "Section", "$Input" & $i, GUICtrlRead($array1[$i]))
        Next
    EndIf
EndFunc   ;==>save

Func load()
    For $i = 0 To 3
    $readInput1 = GUICtrlSetData($array1[$i], IniRead("TEST.INI", "Section", "$Input" & $i, "Error"))
    Next
EndFunc   ;==>load

An even shorter way.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <TabConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

$Form = GUICreate("Form", 310, 297, 4, 35)

Const $sAppIni = @AppDataDir & "\TEST.INI"

WinSetOnTop("Form", "", True)

;#####################################################################################################

$maintab = GUICtrlCreateTab(0, 0, 550, 400)

;#####################################################################################################

GUICtrlCreateTabItem("Tab1")
Global $array1[4] = ["$Name1", "$Name2", "$Name3", "$Name4"]
For $I = 0 To 3
    $array1[$i] = GUICtrlCreateInput("", 2 + ($i * 40), 55, 37, 30, BitOR($ES_CENTER, $ES_NUMBER))
    GUICtrlSetFont(-1, 15, 800, 0, "Arial")
    GUICtrlSetData($array1[$i], 0)
Next

$sb1 = GUICtrlCreateButton("Save", 1, 275, 150, 22)
$sb2 = GUICtrlCreateButton("Load", 152, 275, 150, 22)

$ec1 = GUICtrlCreateCombo("", 1, 254, 162, 22)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
_GUICtrlComboBox_SetCueBanner($ec1, "Name it")

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $sb1
            save()
        Case $sb2
            load()
    EndSwitch
WEnd

Func save()
    $ec1empty = GUICtrlRead($ec1)
    If $ec1empty = "" Then
        MsgBox(0, "", "Name it!")
    Else
        For $i = 0 To 3
            IniWrite("TEST.INI", "Section", "$Input" & $i, GUICtrlRead($array1[$i]))
        Next
    EndIf
EndFunc   ;==>save

Func load()
    For $i = 0 To 3
    $readInput1 = GUICtrlSetData($array1[$i], IniRead("TEST.INI", "Section", "$Input" & $i, "Error"))
    Next
EndFunc   ;==>load

 

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • p1xeL changed the title to [Solved] iniwrite multiple inputs (shorten code)

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...