Jump to content

Upon Button click submit text to ini or txt file


 Share

Go to solution Solved by BrewManNH,

Recommended Posts

Hi all, After reading through posts I have not really find what I am looking for, I just started using AutoIT and after reading numerous posts I decided to ask the pro's. :-) Please bare with me as I am still learning, but here goes. I have a small GUI where a user enters a tel number in the text field and upon clicking "Ok" it should write that tell/ext number to 2 different text file on 2 different locations of the pc's. This is my code so far:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
#Region ### START Koda GUI section ### Form=C:AutoIT_GUIFormsSFE_EXT.kxf
$Form1 = GUICreate("SFE/EXT", 256, 115, 185, 550)
$Button1 = GUICtrlCreateButton("OK", 96, 64, 57, 25, $BS_DEFPUSHBUTTON)
$Input1 = GUICtrlCreateInput("Input1", 72, 24, 145, 21)
$Label1 = GUICtrlCreateLabel("Extension", 16, 24, 50, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
        Case $Label1
    EndSwitch
WEnd

$Button1
$szFile = "C:test.conf"
$szText = FileRead($szFile,FileGetSize($szFile))
$szText = StringReplace($szText, "<username>1111</username>","<username>2222</username>")
$szText = StringReplace($szText, "<callerid>1111</callerid>","<callerid>2222</callerid>")
FileDelete($szFile)
FileWrite($szFile,$szText)
$szFile = "C:testagent.ini"
$szText = FileRead($szFile,FileGetSize($szFile))
$szText = StringReplace($szText, "PBXID=1111","PBXID=2222")
FileDelete($szFile)
FileWrite($szFile,$szText)

 

Firstly, I need the 1111 and 1111 with PBXID=1111 to read as a Wildcard as the '1111' aka extension will be different on the computers. The 2222 will be the new value added into the text field within the GUI. In other words, the user types in 2222 and it change the 1111"and the 1111 together with PBXID=1111 value to whatever was typed. The wildcard mentioned above, I have read into the wildcards but yet it doesnt seem to change the value if I dont specify the current extension within the txt file. The 1111 field can be any extension, 1234, 3221, 3211, 2314 etc etc - and if not specified it doesnt change. How do I go about linking the button, text field and the actual script together? I hope I explained this in a way that people can understand. Thank you in advance and as always I appreciated any direction given.

Edited by Bertus
Link to comment
Share on other sites

Hi,

Welcome to the autoit forum :)

You need to use a proper Ini structure so you can use the autoit native Ini* functions.

For your conf file, I don't suggest to use a XML library to read the entries as I'm sure this one will be kept simple.

I suggest you this code, which includes a function to read the value of XML nodes.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

#Region ### START Koda GUI section ### Form=C:\AutoIT_GUI\Forms\SFE_EXT.kxf
$hGUI = GUICreate("SFE/EXT", 256, 115)
$Label1 = GUICtrlCreateLabel("Extension", 16, 24, 50, 17)
$iInput1 = GUICtrlCreateInput("Input1", 72, 24, 145, 21)
$iButton1 = GUICtrlCreateButton("OK", 96, 64, 57, 25, $BS_DEFPUSHBUTTON)
GUISetState()
#EndRegion ### END Koda GUI section ###

Local $iMsg = 0
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iButton1
            MsgBox($MB_SYSTEMMODAL, "", GUICtrlRead($iInput1))
    EndSwitch
WEnd

GUIDelete($hGUI)

Func XMLNodeGetValue($sXML, $sNode)
    Local $aRet = StringRegExp($sXML, "(?m)<" & $sNode & ">(.*?)</" & $sNode & ">", 3)
    Return (IsArray($aRet) = 0 ? 0 : $aRet[0])
EndFunc   ;==>XMLNodeGetValue

Notes : If you don't specify the second parameter for the FileRead function, it will read the whole file (so no need to use FileGetSize).

Please use autoit code tags to post your code ;)

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Thanks for the reply.

Ok, I guess I have to go back to the drawing board - i have no idea what you just did there. I will most definetely raise my IQ when it comes to AutoIT - alot to learn.

I will take your code above as a fundamental base and wrok from that.

Thank you again for your direction.

:-)

Link to comment
Share on other sites

  • 4 weeks later...

Been running with this and raise my general understanding within AutoIT. The forum helped me out in some way but I think I have lost it some how. :sweating:

I only need to edit the ini file and not the conf at this moment. Im stuck at the part where I run the code, it opens and closes in an instant, below is the code. Can someone please explain to me where I made the mistake? Im not getting any errors.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

$IniFile = @ScriptDir & "c:\settings.ini"

#Region ### START Koda GUI section ### 
$hGUI = GUICreate("SFE/EXT", 256, 115)
$Label1 = GUICtrlCreateLabel("Extension", 16, 24, 50, 17)
$Extension = GUICtrlCreateInput("", 72, 24, 145, 21)
$iButton1 = GUICtrlCreateButton("OK", 96, 64, 57, 25, $BS_DEFPUSHBUTTON)
GUISetState()
#EndRegion ### END Koda GUI section ###

Func Add_ext ()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $iButton1
                GUIDelete($hGUI)

            Case $iButton1
                IniWrite($IniFile, "Phone", "HeadsetID=", GUICtrlRead($Extension))
        EndSwitch
    WEnd
EndFunc

Im sure it is just something simple, but at the moment I cant wrap my head around it.

As always I apprecaite any direction. :)

 

Link to comment
Share on other sites

  • Solution

Try this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

$IniFile = @ScriptDir & "c:\settings.ini"

#Region ### START Koda GUI section ### 
$hGUI = GUICreate("SFE/EXT", 256, 115)
$Label1 = GUICtrlCreateLabel("Extension", 16, 24, 50, 17)
$Extension = GUICtrlCreateInput("", 72, 24, 145, 21)
$iButton1 = GUICtrlCreateButton("OK", 96, 64, 57, 25, $BS_DEFPUSHBUTTON)
GUISetState()
#EndRegion ### END Koda GUI section ###



While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $iButton1
                GUIDelete($hGUI)

            Case $iButton1
                IniWrite($IniFile, "Phone", "HeadsetID=", GUICtrlRead($Extension))
        EndSwitch
WEnd

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

Thank you Brewman - I really appreciate the time you put into this.

I can see it opens as expected but the porcess still runs after I added the new extension and click on "ok" and i have to manually kill it. (The gui does close though, process still runs)

Is there no need to wrap this into a function? The reason I ask is that even if I do add the ext into the text field it doesnt add this anywhere in the ini file.

This is the part of the ini that needs to be updated:

[Phone]
PBXID=1
HeadsetID=1111 ---> there will always be a value no matter what.

IniWrite($IniFile, "Phone", "HeadsetID=", GUICtrlRead($Extension)) --> whatever the user add into the text field ie. 2121 needs to replace the

current value - in this case 1111

Just to add, I have played around with this but it either add only [Phone], HeadsetID= (no extension ) and remove the other "values" under this "key" or it adds [phone] and headsetID at the bottom of the file, this then results in 2 keys with the same name in the ini file.

I hope the above make sense in a way.

As always I appreciate the directions.

Link to comment
Share on other sites

Remove the "=" after the text HeadsetID.

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

Got that one, thanks again BrewMan  - I will continue you with my quest, will post again if I get stuck at some point.

Will now work to incorprate the FileRead into the script to initiate the second part of the script.

Thanks for the pointers.

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