Jump to content

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


maba
 Share

Recommended Posts

  • Moderators

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:

#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

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

[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
Link to comment
Share on other sites

  • Moderators

maba,

Just check the content like this: ;)

#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

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

  • Moderators

maba,

So we compare the content to the pre-set default value: :)

#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

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

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?

Link to comment
Share on other sites

  • Moderators

maba,

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

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

  • 3 weeks later...

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?

#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
Link to comment
Share on other sites

  • Moderators

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.

#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

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

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?

Link to comment
Share on other sites

  • Moderators

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

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

  • Moderators

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

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