Jump to content

Is there a way to have a GUI textbox that acts like a console? [solved]


icu
 Share

Recommended Posts

Dear AutoIt Community,

I want to have a box in my GUI that keeps track of all the things my program is doing. Pretty much exactly like what you get at the bottom of SciTE4AutoIt3.

I'll be using OnEvent mode for my GUI. I've been experimenting with different things but can't seem to get it working.

This is what I've got so far:

$data = ""
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 405, 294, 334, 476)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
$Button1 = GUICtrlCreateButton("Button1", 24, 184, 73, 41)
GUICtrlSetOnEvent(-1, "Button1Click")
GUICtrlCreateEdit($data, 48, 24, 321, 145)
GUICtrlSetOnEvent(-1, "Edit1Change")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $data &= ConsoleRead()
    If @error Then ExitLoop
    Sleep(100)
WEnd

Func Button1Click()
    ConsoleWrite("Button 1 clicked ")
EndFunc

Func Form2Close()
    Exit
EndFunc

Many thanks for any and all help.

-icu

Edited by icu
Link to comment
Share on other sites

You need to assign a variable to the control ID of the edit box and send the data received from the console to it with GUICtrlSetData. Right now what you're doing is putting the information into the $data variable, and then when there's no more console output, and you get the error, the script will close.

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

@BrewManNH: Thanks for the quick reply. I've tried your suggestion and I'm not sure I entirely understand the solution.

The edited code is below:

$data = ""
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 405, 294, 334, 476)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
$Button1 = GUICtrlCreateButton("Button1", 24, 184, 73, 41)
GUICtrlSetOnEvent(-1, "Button1Click")
$BoxID = GUICtrlCreateEdit("", 48, 24, 321, 145)
GUICtrlSetOnEvent(-1, "Edit1Change")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $data &= ConsoleRead()
    Sleep(100)
WEnd

Func Button1Click()
    ConsoleWrite("Button 1 clicked ")
    GUICtrlSetData($BoxID, $data)
EndFunc

Func Form2Close()
    Exit
EndFunc

When you say "assign a variable to the control ID of the edit box" I take that and the help file's instruction for GUICtrlCreateEdit of "Return Value Success: Returns the identifier (controlID) of the new control" as the following line of code:

$BoxID = GUICtrlCreateEdit("", 48, 24, 321, 145)

And when you say "send the data received from the console to it with GUICtrlSetData" I take that to mean the following line:

GUICtrlSetData($BoxID, $data)

So where am I going wrong?

Kind regards,

-icu

Link to comment
Share on other sites

Because there's no information in the $data variable when you pressed the button, it's not going to put anything into the Edit control. You need a While...Wend loop inside your button function to read the console information and then put it into the Edit control.

Like this;

$data = ""
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 405, 294, 334, 476)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
$Button1 = GUICtrlCreateButton("Button1", 24, 184, 73, 41)
GUICtrlSetOnEvent(-1, "Button1Click")
$BoxID = GUICtrlCreateEdit("", 48, 24, 321, 145)
GUICtrlSetOnEvent(-1, "Edit1Change")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()
;~  $data = ""  <<<<<<<<<<<<<<<<<<<< Use this if you want the $data variable blanked out when you press the button.
    ConsoleWrite("Button 1 clicked ")
    While 1
        $data &= ConsoleRead()
        If @error Then ExitLoop
    WEnd
    GUICtrlSetData($BoxID, $data)
EndFunc

Func Form2Close()
    Exit
EndFunc

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

@BrewManNH: Thanks once again for your help. I ran your code and the GUI gets stuck in the While loop once the button has been clicked.

Taking your pointers I was able to write the following code which is kind of what I'm trying to do (just without the console bit):

$data = ""
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 405, 294, 334, 476)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
$Button1 = GUICtrlCreateButton("Button1", 24, 184, 73, 41)
GUICtrlSetOnEvent(-1, "Button1Click")
$BoxID = GUICtrlCreateEdit("", 48, 24, 321, 145)
GUICtrlSetOnEvent(-1, "Edit1Change")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()
    ConsoleWrite("Button 1 clicked ")
    $data &= ("Button 1 clicked" & @CRLF)
    GUICtrlSetData($BoxID, $data)
EndFunc

Func Form2Close()
    Exit
EndFunc

Any ideas?

Link to comment
Share on other sites

Are you compiling the script when you run it? I saw in the example script in the help file for ConsoleRead that the example script had to be compiled to work correctly.

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

@icu

What's with all the console stuff? It sounds to me that you just want to append data to the Edit, so just by taking a quick look at GUICtrlSetData() in the helpfile=

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 405, 294, 334, 476)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
;~ GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
;~ GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
;~ GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
$Button1 = GUICtrlCreateButton("Button1", 24, 184, 73, 41)
GUICtrlSetOnEvent(-1, "Button1Click")
$BoxID = GUICtrlCreateEdit("", 48, 24, 321, 145)
;~ GUICtrlSetOnEvent(-1, "Edit1Change")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()
    GUICtrlSetData($BoxID, "Button 1 clicked" & @CRLF, "1")
EndFunc

Func Form2Close()
    Exit
EndFunc
Link to comment
Share on other sites

@BrewManNH: Thanks again for your reply. Nope didn't compile... I guess this is another PICNIC error again :-)

@AdmiralAlkex: Thanks for your reply and the final solution... I feel bad that the solution is as simple as:

Func Button1Click()
    GUICtrlSetData($BoxID, "Button 1 clicked" & @CRLF, "1")
EndFunc

I did actually look up the help file and I tried something like it but I didn't put the "1" flag in to trigger the List control. When I read the following in the help file:

Remarks

For Combo or List control :

If the "data" corresponds to an already existing entry it is set as the default.

If the "data" starts with GUIDataSeparatorChar or is an empty string "" the previous list is destroyed.

For ListView, ListViewItem controls :

To update a specific column just forget about the others ie "||update" to update 3rd column.

If "update" is empty the column/subitem will be erased. For example "|" will erase the second column/subitem, "" will erase the first.

I didn't understand a word. I still don't know what any of it translates to in plain English except that I need that flag to make it work :-)
Link to comment
Share on other sites

You are not using GUICtrlCreateList(), you are using GUICtrlCreateEdit(), so look at the explanation for the "default" parameter:

default [optional] Combo, List: The default value.

Edit, Input: If non-empty (""), the string is inserted at the current insertion point (caret).

Note that someone could move the caret and screw it all up :)

Look at _GUICtrlEdit_AppendText() in the helpfile and it's example for how to fix that ;)

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