Jump to content



Photo

Change GUICtrl* background color when user edit input, date, etc..


  • Please log in to reply
14 replies to this topic

#1 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 04 February 2012 - 04:55 PM

Hi,
is there a simple way to change background or border color, or at least add a sign near the input, when user edit a input, date or other control?

Thanks







#2 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 04 February 2012 - 05:09 PM

maba,

Be careful about colouring active AutoIt controls - it can lead to unexpected behaviour because of the way this was implemented in the core code many years ago. ;)

As to checking to see if the user has changed anything in a control, you can either monitor the content of the control in your idle loop (simple) or look for a suitable Windows message (more elegant). This script shows how to do both:
AutoIt         
#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> ; Create a GUI $hGUI = GUICreate("Test", 500, 500) $hLabel_1 = GUICtrlCreateLabel("Input 1", 10, 10, 200, 20) $hInput_1 = GUICtrlCreateInput("", 10, 30, 200, 20) $hLabel_2 = GUICtrlCreateLabel("Input 2", 10, 110, 200, 20) $hInput_2 = GUICtrlCreateInput("", 10, 130, 200, 20) GUISetState() ; Look for the WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") $sInput_1_Content = "" While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch     ; Get the content of the input and compare the the last version     $sInput_1_Text = GUICtrlRead($hInput_1)     If $sInput_1_Text <> $sInput_1_Content Then         ; It has changed so save it         $sInput_1_Content = $sInput_1_Text         ; Colour the label to red         GUICtrlSetBkColor($hLabel_1, 0xFFCCCC)         ; And after 1 sec set it back to white         AdlibRegister("_Reset", 1000)     EndIf WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         ; Chenge label to green         GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)         ; And after 1 sec set it back to white         AdlibRegister("_Reset", 1000)     EndIf EndFunc   ;==>_WM_COMMAND Func _Reset()     ; Stop the function from being called again     AdlibUnRegister("_Reset")     ; Set the labels to white     GUICtrlSetBkColor($hLabel_1, 0xFEFEFE)     GUICtrlSetBkColor($hLabel_2, 0xFEFEFE) EndFunc

Please ask if you have any questions. :)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#3 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 04 February 2012 - 05:38 PM

[dangerous mode on]
Ooops.. :)
[dangerous mode off]

Thanks for your answer!

Posted Image

This is an example of my target, I don't like so much because there are the green border even when user don't made changes.
Your solution can solve the problem and I prefer change label background color (when possible), but.. when the focus change colors are reset to white, is it possible to "fix" the color? If user does a change color is set to red, if user reset to default.. windows color are applied.

Edited by maba, 04 February 2012 - 05:39 PM.


#4 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 04 February 2012 - 05:43 PM

maba,

Just check the content like this: ;)
AutoIt         
#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> ; Create a GUI $hGUI = GUICreate("Test", 500, 500) $hLabel_1 = GUICtrlCreateLabel("Input 1", 10, 10, 200, 20) $hInput_1 = GUICtrlCreateInput("", 10, 30, 200, 20) $hLabel_2 = GUICtrlCreateLabel("Input 2", 10, 110, 200, 20) $hInput_2 = GUICtrlCreateInput("", 10, 130, 200, 20) GUISetState() ; Look for the WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") $sInput_1_Content = "" While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch     ; Get the content of the input and compare the the last version     $sInput_1_Text = GUICtrlRead($hInput_1)     If $sInput_1_Text <> $sInput_1_Content Then         ; It has changed so save it         $sInput_1_Content = $sInput_1_Text         If $sInput_1_Content <> "" Then             ; Colour the label to red             GUICtrlSetBkColor($hLabel_1, 0xFFCCCC)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_1, 0xFEFEFE)         EndIf     EndIf WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         If GUICtrlRead($hInput_2) <> "" Then             ; Chenge label to green             GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_2, 0xFEFEFE)         EndIf     EndIf EndFunc   ;==>_WM_COMMAND

All clear? :)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#5 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 04 February 2012 - 06:02 PM

Ok, thanks.
But if I want to check about 20 fields in a form and theirs default value (like and edit form for a sqlite record) I've to save this value in a second variable in the form creation?
I cannot use to retrieve the "default" value.

#6 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 04 February 2012 - 06:10 PM

maba,

So we compare the content to the pre-set default value: :)
AutoIt         
#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> ; Set default values Global $sDef_1 = "Default value 1" Global $sDef_2 = "Default value 2" ; Create a GUI $hGUI = GUICreate("Test", 500, 500) $hLabel_1 = GUICtrlCreateLabel("Input 1", 10, 10, 200, 20) $hInput_1 = GUICtrlCreateInput($sDef_1, 10, 30, 200, 20) $hLabel_2 = GUICtrlCreateLabel("Input 2", 10, 110, 200, 20) $hInput_2 = GUICtrlCreateInput($sDef_2, 10, 130, 200, 20) GUISetState() ; Look for the WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") ; Set current value $sInput_1_Content = $sDef_1 While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch     ; Get the content of the input and compare the the last version     $sInput_1_Text = GUICtrlRead($hInput_1)     If $sInput_1_Text <> $sInput_1_Content Then         ; It has changed so save it         $sInput_1_Content = $sInput_1_Text         If $sInput_1_Content <> $sDef_1 Then             ; Colour the label to red             GUICtrlSetBkColor($hLabel_1, 0xFFCCCC)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_1, 0xFEFEFE)         EndIf     EndIf WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         If GUICtrlRead($hInput_2) <> $sDef_2 Then             ; Chenge label to green             GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_2, 0xFEFEFE)         EndIf     EndIf EndFunc   ;==>_WM_COMMAND

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#7 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 04 February 2012 - 06:23 PM

Yep, perfect! Thanks

I change only the default color:

Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         If GUICtrlRead($hInput_2) <> $sDef_2 Then             ; Chenge label to green             GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)      Else             ; Set it back to default             GUICtrlSetBkColor($hLabel_2, _WinAPI_GetSysColor($COLOR_MENU))         EndIf EndIf EndFunc   ;==>_WM_COMMAND


In any case I must declare vars/consts for any fields that I want control.
In your opinion is this a good choice for a form with 20/30 fields?

#8 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 04 February 2012 - 06:29 PM

maba,

You would probably want to use a Switch structure to check whether the control was one you wished to check. :)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#9 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 04 February 2012 - 06:40 PM

Ok, I've readed about $WM_COMMAND here http://www.autoitscript.com/wiki/GUIRegisterMsg
Thanks!

#10 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 22 February 2012 - 03:15 PM

Is it possible manange icons also?
Show an icon only if input is modified..

Bad question above..
How to hide icon when input is set back to default?

AutoIt         
#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> ; Set default values Global $sDef_1 = "Default value 1" Global $sDef_2 = "Default value 2" ; Create a GUI $hGUI = GUICreate("Test", 500, 500) $hLabel_1 = GUICtrlCreateLabel("Input 1", 10, 10, 200, 20) $hInput_1 = GUICtrlCreateInput($sDef_1, 10, 30, 200, 20) $hIcon_1  = GUICtrlCreateIcon(@AutoItExe, -1, 220, 30, 16, 16) $hLabel_2 = GUICtrlCreateLabel("Input 2", 10, 110, 200, 20) $hInput_2 = GUICtrlCreateInput($sDef_2, 10, 130, 200, 20) GUISetState() ; Look for the WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") ; Set current value $sInput_1_Content = $sDef_1 While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch     ; Get the content of the input and compare the the last version     $sInput_1_Text = GUICtrlRead($hInput_1)     If $sInput_1_Text <> $sInput_1_Content Then         ; It has changed so save it         $sInput_1_Content = $sInput_1_Text         If $sInput_1_Content <> $sDef_1 Then             ; Colour the label to red             GUICtrlSetBkColor($hLabel_1, 0xFFCCCC)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_1, 0xFEFEFE)         EndIf     EndIf WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         If GUICtrlRead($hInput_2) <> $sDef_2 Then             ; Chenge label to green             GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)             GUICtrlCreateIcon(@AutoItExe, -1, 220, 130, 16, 16)      Else             ; Set it back to default             GUICtrlSetBkColor($hLabel_2, _WinAPI_GetSysColor($COLOR_MENU))             ; HOW TO REMOVE ICON?         EndIf EndIf EndFunc   ;==>_WM_COMMAND


Thanks

Edited by maba, 22 February 2012 - 03:30 PM.


#11 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 22 February 2012 - 04:34 PM

maba,

Two ways to do this:

- 1. Delete and create the icon each time - this is what I have done with Icon_1.

- 2. Create the icon once and then hide/show it as necessary - and this is what happens with Icon_2.
AutoIt         
#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> ; Set default values Global $sDef_1 = "Default value 1", $hIcon_1 Global $sDef_2 = "Default value 2", $hIcon_2 ; Create a GUI $hGUI = GUICreate("Test", 500, 500) $hLabel_1 = GUICtrlCreateLabel("Input 1", 10, 10, 200, 20) $hInput_1 = GUICtrlCreateInput($sDef_1, 10, 30, 200, 20) $hLabel_2 = GUICtrlCreateLabel("Input 2", 10, 110, 200, 20) $hInput_2 = GUICtrlCreateInput($sDef_2, 10, 130, 200, 20) $hIcon_2  = GUICtrlCreateIcon(@AutoItExe, -1, 220, 130, 16, 16) GUICtrlSetState(-1, $GUI_HIDE) GUISetState() ; Look for the WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") ; Set current value $sInput_1_Content = $sDef_1 While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch     ; Get the content of the input and compare the the last version     $sInput_1_Text = GUICtrlRead($hInput_1)     If $sInput_1_Text <> $sInput_1_Content Then         ; It has changed so save it         $sInput_1_Content = $sInput_1_Text         If $sInput_1_Content <> $sDef_1 Then             ; Colour the label to red             GUICtrlSetBkColor($hLabel_1, 0xFFCCCC)             $hIcon_1  = GUICtrlCreateIcon(@AutoItExe, -1, 220, 30, 16, 16)         Else             ; Set it back to white             GUICtrlSetBkColor($hLabel_1, 0xFEFEFE)             GUICtrlDelete($hIcon_1)         EndIf     EndIf WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)     ; If it was an update message from the correct input     If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then         If GUICtrlRead($hInput_2) <> $sDef_2 Then             ; Chenge label to green             GUICtrlSetBkColor($hLabel_2, 0xCCFFCC)             GUICtrlSetState($hIcon_2, $GUI_SHOW)      Else             ; Set it back to default             GUICtrlSetBkColor($hLabel_2, _WinAPI_GetSysColor($COLOR_MENU))             GUICtrlSetState($hIcon_2, $GUI_HIDE)         EndIf EndIf EndFunc   ;==>_WM_COMMAND

All clear? :)

M23
  • footswitch likes this
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#12 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 23 February 2012 - 08:10 PM

Melba23,
yes, it works. I prefer 2nd solution again :) Thanks!
Now.. I would add some input validation functions at this point but.. is it the right place?

"Warning: blocking of running user functions which executes window messages with commands such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible !!!"

Is it referred to a "waiting user input function" only or for all "complex" funtions?

#13 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 23 February 2012 - 09:12 PM

maba,

When it says "as fast as possible", it means as fast as possible!!! :)

I try to spend as little time as possible in the handler itself. If you need to run a longish function, and certainly anything that involves a loop, then you should use the handler to set a flag and then check for it in your idle loop. Search for "+m23 +guiregistermsg" and you will find plenty of examples of how this can be done. ;)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#14 maba

maba

    Seeker

  • Active Members
  • 38 posts

Posted 24 February 2012 - 10:40 AM

Yes, I have founded some docs.
Suggestion: flags or the dummy controls?

#15 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,398 posts

Posted 24 February 2012 - 11:44 AM

maba,

Just a question of personal taste in my opinion. I use both - but usually not in the same script (unless it is deliberate for an example). :)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users