Jump to content
Sign in to follow this  
SjaakTV

Write something via GUICtrlCreateInput to notepad.

Recommended Posts

SjaakTV

Hi,

I'm new in autoit and i'm trying to make a gui where you can type something in a GUICtrlCreateInput. After you did that you press add and it is added in the notepad file. The functie to write it to the notepad does not work i don't know why and how to fix the add button to to submit the input i can't even imagine how to do that.

A bit help would be awesome because i'm trying the learn autoit.

Thnx for your time.

HotkeySet ("{ESC}", "Stop")

Func Stop ()
        Exit 0
EndFunc

MENU()
Func MENU()

GUICreate("Stop spam", 400, 288) 
GUISetBkColor(0xaaaaaa)
GUICtrlCreateLabel((@MON & "/" & @MDAY & "/" & @YEAR), 325, 10)
$NewWord = GUICtrlCreateInput("New word",30,50,200,20,1,1) 
$add = GUICtrlCreateButton("Add", 270, 47, 33)
GUISetState(@SW_SHOW)

GUISetState()
    While 1
        

        $msg = GUIGetMsg()
        
            If $msg = $NewWord then AddWord()
wend
            
    GUIDelete()
EndFunc

Func AddWord()
$a = $NewWord
$file1 = FileOpen("\test1.txt", 1)
FileWriteLine($file1, $a)
FileCLose($file1)
EndFunc

Share this post


Link to post
Share on other sites
Melba23

SjaakTV,

Welcome to the AutoIt forum. :mellow:

A nice try for a first script. I have made a few changes here - take a look and then read why I made them below:

#include <GUIConstantsEx.au3>

HotKeySet("{ESC}", "Stop")

Global $NewWord

Func Stop()
    Exit 0
EndFunc   ;==>Stop

MENU()

Func MENU()

    GUICreate("Stop spam", 400, 288)
    GUISetBkColor(0xaaaaaa)
    GUICtrlCreateLabel((@MON & "/" & @MDAY & "/" & @YEAR), 325, 10)
    $NewWord = GUICtrlCreateInput("New word", 30, 50, 200, 20, 1, 1)
    $add = GUICtrlCreateButton("Add", 270, 47, 33)
    GUISetState(@SW_SHOW)

    GUISetState()
    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $add
                AddWord()
        EndSwitch

    WEnd

    GUIDelete()
EndFunc   ;==>MENU

Func AddWord()
    $a = GUICtrlRead($NewWord)
    $file1 = FileOpen("test1.txt", 1)
    FileWriteLine($file1, $a)
    FileClose($file1)
EndFunc   ;==>AddWord

The changes:

While...WEnd loop: I added the ability to close the GUI by clicking on the [X] - Windows gives you that option, so you might as well use it! I also changed the message to run the function - you were looking for $NewWord, you need to look for the $add button. I also used a Switch structure - if you have to checkmore than one thing, it is more elegant, easier to maintain and faster than using lots of If statements.

AddWord function: You need the content of the Input, so you have to use GUICtrlRead - you were just getting the ControlID. You also had an unneeded "\" in your file name - AutoIt needs you to add "\" to paths quite often but this was not one of the times! :P

Global variables: You need to declare the $NewWord Input as Global so both your functions can see it!

I hope that is all clear - please ask if not. :(

As you are just starting with Autoit, reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here. There are even video tutorials on YouTube if you prefer watching to reading.

I know you want to start coding NOW, but a little study will save you a lot of trouble later on, believe me. :lol:

M23

Edit: Typnig

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
SjaakTV

SjaakTV,

Welcome to the AutoIt forum. :mellow:

A nice try for a first script. I have made a few changes here - take a look and then read why I made them below:

#include <GUIConstantsEx.au3>

HotKeySet("{ESC}", "Stop")

Global $NewWord

Func Stop()
    Exit 0
EndFunc   ;==>Stop

MENU()

Func MENU()

    GUICreate("Stop spam", 400, 288)
    GUISetBkColor(0xaaaaaa)
    GUICtrlCreateLabel((@MON & "/" & @MDAY & "/" & @YEAR), 325, 10)
    $NewWord = GUICtrlCreateInput("New word", 30, 50, 200, 20, 1, 1)
    $add = GUICtrlCreateButton("Add", 270, 47, 33)
    GUISetState(@SW_SHOW)

    GUISetState()
    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $add
                AddWord()
        EndSwitch

    WEnd

    GUIDelete()
EndFunc   ;==>MENU

Func AddWord()
    $a = GUICtrlRead($NewWord)
    $file1 = FileOpen("test1.txt", 1)
    FileWriteLine($file1, $a)
    FileClose($file1)
EndFunc   ;==>AddWord

The changes:

While...WEnd loop: I added the ability to close the GUI by clicking on the [X] - Windows gives you that option, so you might as well use it! I also changed the message to run the function - you were looking for $NewWord, you need to look for the $add button. I also used a Switch structure - if you have to checkmore than one thing, it is more elegant, easier to maintain and faster than using lots of If statements.

AddWord function: You need the content of the Input, so you have to use GUICtrlRead - you were just getting the ControlID. You also had an unneeded "\" in your file name - AutoIt needs you to add "\" to paths quite often but this was not one of the times! :P

Global variables: You need to declare the $NewWord Input as Global so both your functions can see it!

I hope that is all clear - please ask if not. :(

As you are just starting with Autoit, reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here. There are even video tutorials on YouTube if you prefer watching to reading.

I know you want to start coding NOW, but a little study will save you a lot of trouble later on, believe me. :lol:

M23

Edit: Typnig

Thnx man. I now understand it. I'll try some more.

Share this post


Link to post
Share on other sites
Melba23

SjaakTV,

Glad I could help. :mellow:

When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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
Sign in to follow this  

×