Jump to content

Write to .txt file!


Skorm
 Share

Recommended Posts

Allright,

(Highly needed) I'm working on a little system for school.

I need to create something that when a user types something into an input box and presses "Sla op in Database" (Save in DB) that it will save his/her input to an text file.

I got it working, but not quite as it has to be working.

When i open up the txt file, it has a bunch of 3's in it...

(Optional) And how can i ID every input?

so when i open it up later i have an overview of what has been saved into that txt file?

#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("Magazijnbeheer", 300, 70)
GuiSetIcon(@SystemDir & "\explorer.exe", 0)

$invoer = GUICtrlCreateInput("", 15, 23, 180, 20)
$voerin = GUICtrlCreateButton("Sla op in Database", 197, 23, 100, 20)

GuiCtrlCreateLabel("Model:", 125, 7, 400, 15)
GuiCtrlCreateLabel("bijvoorbeeld: Videokaart, Computer, Netwerkkaart etc etc", 12, 50, 400, 15)


GUISetState() 

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $voerin

                $TextFileName = "invoer.txt"
 
                FileOpen ( "invoer.txt" , 1 )
                FileRead("invoer.txt")
                FileWrite($TextFileName, $invoer)

        EndSelect
    WEnd



; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Thank you in Advance!!

Geoffrey

Link to comment
Share on other sites

  • Moderators

Skorm,

The 3's you are seeing in the file are the ControlID of the Input - the code AutoIt uses to identify it. You need to save the contents of the input which you get through GUICtrlRead.

Take a look at this:

#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

; GUI
GUICreate("Magazijnbeheer", 300, 70)
GUISetIcon(@SystemDir & "\explorer.exe", 0)

$invoer = GUICtrlCreateInput("", 15, 23, 180, 20)
$voerin = GUICtrlCreateButton("Sla op in Database", 197, 23, 100, 20)

GUICtrlCreateLabel("Model:", 125, 7, 400, 15)
GUICtrlCreateLabel("bijvoorbeeld: Videokaart, Computer, Netwerkkaart etc etc", 12, 50, 400, 15)

GUISetState()

$hFile = FileOpen("invoer.txt", 1)

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $voerin
            FileWriteLine($hFile, GUICtrlRead($invoer))
            GUICtrlSetData($invoer, "")
    EndSelect
WEnd

FileClose($hFile)

I have also tidied up the file writing code a bit for you - and removed the surplus loop at the end of the script. :idea:

And how can i ID every input? so when i open it up later i have an overview of what has been saved into that txt file?

Not quite sure what you want here. Can you explain a bit more.

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

Example to write user input into a txt file

$Form1 = GUICreate("Login", 355, 350)
$Label1 = GUICtrlCreateLabel("USERNAME:", 16, 100, 133, 17)
$Username = GUICtrlCreateInput("", 145, 95, 185, 21)
GUISetState(@SW_SHOW)

;Write to aw.txt
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
        Case $GUI_EVENT_CLOSE 
                     Exit 1
        Case $Login
                 $s_username  = GUICtrlRead($Username)
                 _FileWriteToLine("aw.txt", 11(line 11), 'Username = '& $s_username, 1)
        EndSwitch
    WEnd
Edited by adik2dmax666
First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack. -George Carrette[sub]GD Keylogger Creator (never released)[/sub][sub]Garena Autojoin v3.0[/sub]
Link to comment
Share on other sites

Not quite sure what you want here. Can you explain a bit more.

First of all thanks for the help, It works!

The program i'm working on is meant to register what comes in and out a storage.

Like if someone needs a computer to repair, He has to register his Model/In or out/Serial Number/Person that takes the product/Date

And later on at the end of the day, We need to check what went in and out.

So now im stuck again.

When you saved something to that Text file (Sla op in Database) the GUI needs to close and another GUI must pop-up For the serial number.

And i need to figure out how to create a overview on what has been comming in and has been going out on that day.

Help would be appreciated!

Geoffrey

Edited by Skorm
Link to comment
Share on other sites

  • Moderators

Skorm,

Here is a very rough and ready version of how you might do what you want - plenty of room for improvement here! :idea:

I have switched to using the IniFile format - it makes saving and loading data much easier. However, be aware that IniFiles are limited to about 32k of data, so if you have a lot of transactions to save you (and suppose I) may need to think again. :(

I have commented what is going on:

#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#Include <GuiListView.au3>

; Data file
$sFile = "invoer.txt"

; List of valid names - you coudl alsohave a list of valid models as well
$sNames = "Tom|Dick|Harry"

; creat emain GUI
$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Name", 10, 10, 200, 20)
$hCombo_Name = GUICtrlCreateCombo("", 10, 30, 200, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, $sNames)
GUICtrlCreateLabel("Model", 10, 60, 200, 20)
$hInput_Model = GUICtrlCreateInput("", 10, 80, 200, 20)
GUICtrlCreateLabel("Serial No", 10, 110, 200, 20)
$hInput_Serial = GUICtrlCreateInput("", 10, 130, 200, 20)

GUIStartGroup()
$hRadio_In = GUICtrlCreateRadio(" In", 10, 160, 50, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$hRadio_Out = GUICtrlCreateRadio(" Out", 100, 160, 50, 20)
GUIStartGroup()

$hButton_Save = GUICtrlCreateButton("Save", 10, 200, 80, 30)
$hButton_Show = GUICtrlCreateButton("Show", 100, 200, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_Save
            save_data()
        Case $hButton_Show
            show_data()
    EndSwitch

WEnd

Func save_data()

    If GUICtrlRead($hRadio_In) = 1 Then
        $sType = "IN"
    Else
        $sType = "OUT"
    EndIf
    
    ; Create values for saving to file
    $sSaveSection = @YEAR & "/" & @MON & "/" & @MDAY
    $sSaveKey = @HOUR & ":" & @MIN & ":" & @SEC
    $sSaveValue = GUICtrlRead($hCombo_Name) & "|" & GUICtrlRead($hInput_Model) & "|" & GUICtrlRead($hInput_Serial) & "|" & $sType

    ; Save in the special ini format
    IniWrite($sFile, $sSaveSection, $sSaveKey, $sSaveValue)

    ; Empty inputs
    GUICtrlSetData($hInput_Model, "")
    GUICtrlSetData($hInput_Serial, "")


EndFunc

Func show_data()

    ; Create display GUI
    $hTrans_GUI = GUICreate("Transactions", 400, 400)

    ; Fill and display combos
    $sDay = ""
    For $i = 1 To 31
        $sDay &= StringFormat("%02i", $i) & "|"
    Next
    $sMon = ""
    For $i = 1 To 12
        $sMon &= StringFormat("%02i", $i) & "|"
    Next
    $sYear = ""
    For $i = 2010 To 2020
        $sYear &= $i & "|"
    Next

    GUICtrlCreateLabel("Day", 10, 10, 30, 20)
    $hCombo_Day = GUICtrlCreateCombo("", 40, 10, 40, 20)
    GUICtrlSetData(-1, $sDay, @MDAY)
    GUICtrlCreateLabel("Mon", 100, 10, 30, 20)
    $hCombo_Mon = GUICtrlCreateCombo("", 140, 10, 40, 20)
    GUICtrlSetData(-1, $sMon, @MON)
    GUICtrlCreateLabel("Year", 200, 10, 40, 20)
    $hCombo_Year = GUICtrlCreateCombo("", 240, 10, 60, 20)
    GUICtrlSetData(-1, $sYear, @YEAR)

    ; Create Listview
    $hListView = GUICtrlCreateListView("Time|Name|Model|Serial|Type", 10, 50, 380, 200)

    For $i = 0 To 3
        _GUICtrlListView_SetColumnWidth($hListView, $i, 76)
    Next
    _GUICtrlListView_SetColumnWidth($hListView, 4, $LVSCW_AUTOSIZE_USEHEADER)

    $hButton_Display = GUICtrlCreateButton("Show", 10, 270, 80, 30)

    GUISetState()

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete($hTrans_GUI)
                Return
            Case $hButton_Display
                ; Get date for section read
                $sSection = GUICtrlRead($hCombo_Year) & "/" & GUICtrlRead($hCombo_Mon) & "/" & GUICtrlRead($hCombo_Day)
                ; Read all transaction into an array
                $aTransactions = IniReadSection($sFile, $sSection)
                If @error Then
                    GUICtrlCreateListViewItem("No Data", $hListView)
                Else
                    ; Display each transaction
                    For $i = 1 To $aTransactions[0][0]
                        $sTransaction = $aTransactions[$i][0] & "|" & $aTransactions[$i][1]
                        GUICtrlCreateListViewItem($sTransaction, $hListView)
                    Next
                EndIf
        EndSwitch
    WEnd

EndFunc

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

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