Jump to content
Sign in to follow this  
gertsolo

Help: Simple script not working anymore

Recommended Posts

gertsolo

Hey,

I made a simple script that reads the text in a file and displays it in an edit box. It ran fine, than I altered it a bit and now it's not working anymore.

It keeps on looping in case button1.

What did I do wrong?

#include <GUIConstants.au3>
#include <file.au3>

$file1 = "C:\Documents and Settings\Desktop\AutoIt\Webbuilding\msg1.txt"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 629, 644, 193, 115)
$Edit1 = GUICtrlCreateEdit("", 56, 32, 329, 169)
$Edit2 = GUICtrlCreateEdit("", 56, 211, 329, 169)
$Edit3 = GUICtrlCreateEdit("", 56, 391, 329, 169)
;GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("Opvragen huidge berichten", 408, 32, 209, 57, 0)
$Button2 = GUICtrlCreateButton("Wegschrijven nieuwe berichten", 407, 108, 209, 57, 0)
$Button3 = GUICtrlCreateButton("Stoppen", 414, 510, 209, 57, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
                $file = FileOpen($file1, 0)
                GUICtrlSetData($Edit1,"")
                While 1
                    $line = FileReadLine($file)
                    GUICtrlSetData($Edit1,$line,"k")
                    If @error = -1 Then ExitLoop
                Wend
                FileClose($file1)
        Case $Button2
                FileOpen($file1,2)
                $ingave = Guictrlread($Edit1)
                Filewrite($file1,$ingave)
                ConsoleWrite($ingave)
                FileClose($file1)
        Case $Button3
            Exit
    EndSwitch
WEnd

The more you learn, the less you know.

Share this post


Link to post
Share on other sites
Jos

this looks wrong:

$line = FileReadLine($file)
GUICtrlSetData($Edit1,$line,"k")
If @error = -1 Then ExitLoop

The @error test needs to be done right after the FileReadLine() to test its @error code.

$line = FileReadLine($file)
If @error = -1 Then ExitLoop
GUICtrlSetData($Edit1,$line,"k")

:P


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

Share this post


Link to post
Share on other sites
gertsolo

Thx for the fast reply Jos,

but it doesn't work either. As soon as I enter that GUICtrlSetData($Edit1,$line,"k") line it starts looping. If i put a for example msgbox instead, it works fine.

rgds,

My bad Jos, works like a charm, thx !!!

Edited by gertsolo

The more you learn, the less you know.

Share this post


Link to post
Share on other sites
MrCreatoR

Hi,

I think you should check if the file was really opened...

#include <GUIConstants.au3>
#include <file.au3>

$file1 = "C:\Documents and Settings\Desktop\AutoIt\Webbuilding\msg1.txt"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 629, 644, 193, 115)
$Edit1 = GUICtrlCreateEdit("", 56, 32, 329, 169)
$Edit2 = GUICtrlCreateEdit("", 56, 211, 329, 169)
$Edit3 = GUICtrlCreateEdit("", 56, 391, 329, 169)
;GUICtrlSetData(-1, "Edit1")
$Button1 = GUICtrlCreateButton("Opvragen huidge berichten", 408, 32, 209, 57, 0)
$Button2 = GUICtrlCreateButton("Wegschrijven nieuwe berichten", 407, 108, 209, 57, 0)
$Button3 = GUICtrlCreateButton("Stoppen", 414, 510, 209, 57, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
                $hFile = FileOpen($file1, 0)
                
                If $hFile = -1 Then
                    MsgBox(16, "Error", "There was an error to open file.")
                    ContinueLoop
                EndIf
                
                GUICtrlSetData($Edit1, "")
                While 1
                    $line = FileReadLine($hFile)
                    If @error = -1 Then ExitLoop
                    GUICtrlSetData($Edit1,$line,"k")
                Wend
                FileClose($hFile)
        Case $Button2
                $hFile = FileOpen($file1, 2)
                
                If $hFile = -1 Then
                    MsgBox(16, "Error", "There was an error to open file.")
                    ContinueLoop
                EndIf
                
                $ingave = Guictrlread($Edit1)
                Filewrite($hFile, $ingave)
                ConsoleWrite($ingave)
                FileClose($hFile)
        Case $Button3
            Exit
    EndSwitch
WEnd

And also see how i fix some of your mistakes - you open file, and write to the file, not to the file handle that returned by FileOpen(), and when you do FileClose(), you should close the handle ($hFile).

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
gertsolo

Hi,

I think you should check if the file was really opened...

And also see how i fix some of your mistakes - you open file, and write to the file, not to the file handle that returned by FileOpen(), and when you do FileClose(), you should close the handle ($hFile).

You're right, changed it all. Was working to fast I guess and messed up the $file and $file1,

thx


The more you learn, the less you know.

Share this post


Link to post
Share on other sites
gertsolo

If I may I would like to add an addendum to my question:

If I have 3 edit boxes I wanted to do the stuff with I'm doing in the script, what is the easiest way of doing that?

Working with arrays? And if so, how to do that?


The more you learn, the less you know.

Share this post


Link to post
Share on other sites
John117

just an example of text to array or multiple text files to multiple edit boxes. I had the button already so I used it. -You dont have to. Also I left the random in there for fun. -Just part of a joke I put together once to create random insults. :P

CODE
;Include Autoit Functions

#include <GuiConstants.au3>

#include <file.au3>

;Set Hot Keys - (They call Functions listed near the bottom)

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

;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;A

rrays;Arrays;Arrays;Arrays;Arrays;Arrays

;***************************************************************************************************

***************************************

Dim $aRecords, $bRecords, $cRecords

If Not _FileReadToArray(@ScriptDir & "\A.txt", $aRecords) Then

MsgBox(4096, "Error", " Error reading A.txt log to Array error:" & @error)

Exit

EndIf

If Not _FileReadToArray(@ScriptDir & "\B.txt", $bRecords) Then

MsgBox(4096, "Error", " Error reading B.txt log to Array error:" & @error)

Exit

EndIf

If Not _FileReadToArray(@ScriptDir & "\C.txt", $cRecords) Then

MsgBox(4096, "Error", " Error reading C.txt log to Array error:" & @error)

Exit

EndIf

;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI

;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI

;***************************************************************************************************

***************************************

;Create the basic GUI

$window = GUICreate("MY Messenger", 610, 600, 300, 250)

$Group1 = GUICtrlCreateGroup("My Message:", 24, 40, 561, 100)

$Edit1 =GUICtrlCreateEdit ("Message Here", 34, 60, 440, 65, $ES_WANTRETURN)

$Button1 = GUICtrlCreateButton("Send", 495, 120, 60, 17)

;Show the finished resluts of the GUI

GUISetState(@SW_SHOW)

;Keep the GUI open and monitoring.

While 1

$nMsg = GUIGetMsg()

$cursorinfo = GUIGetCursorInfo($window)

If $cursorinfo[4] = $Edit1 Then

If $cursorinfo[2] = 1 Then

GUICtrlSetData($Edit1, "")

EndIf

EndIf

Select

Case $nMsg = $GUI_EVENT_CLOSE

Exit

Case ($nMsg = $Button1)

LoadEdit1()

EndSelect

WEnd

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;***************************************************************************************************

****************************************

Func LoadEdit1()

;GUICtrlSetData($Edit1, $aRecords[Random(1, UBound($aRecords) - 1, 1) ]) ;Pulls a random text line from the array.

GUICtrlSetData($Edit1, $aRecords[1] & " " & $aRecords[2] & " " & $aRecords[3] & " " & $aRecords[4] & " " & $aRecords[5] & " " & $aRecords[6]) ;If you use an array like this you will have to add each line here

EndFunc ;==>SendMessage

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;***************************************************************************************************

****************************************

Func Terminate()

Exit 0

EndFunc ;==>Terminate

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS

;***************************************************************************************************

****************************************

-you can mix up the insert to the edit how ever you want, some from a.txt then some from b.txt them more from either . . . You can also add more edit boxes and send stuff there also.

-you will either need to change the three txt file names or add a.txt, b.txt and c.txt text files to the same folder as this script. Or just change the directory in the script to point to them.

Edited by Hatcheda

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  

×