Change a text or edit field's colors dynamically in a Gui Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Change a text or edit field's colors dynamically in a Gui

Post by arcylix » 07 Jun 2023, 07:38

I want to preface this by stating that I do not think background colors work in text fields, despite it clearly being an option as far as I can tell. I've used "BackgroundBlack" while adding text fields and it ignores it completely, unless I'm doing something wrong.

Now on to the reason for the post.

I have a DropDownList with a Change event that modifies the textcolor and backcolor of an object. However, in the gui itself, the field does not update and remains (visually) the same color. I thought using Redraw() would do the trick, but it does not. Neither does making the field hidden then visible. Am I missing some key components? Here is a sample of the code I am using:

Code: Select all

#SingleInstance Force
#Requires AutoHotkey v2.0

customColorsMap := Map(1, Map("text", "FF8080", "back", "black"), 2, Map("text", "FFFF80", "back", "black"), 3, Map("text", "80FF80", "back", "black"), 4, Map("text", "80FFFF", "back", "black"), 5, Map("text", "0080FF", "back", "black"), 6, Map("text", "FF80c0", "back", "black"), 7, Map("text", "red", "back", "black"), 8, Map("text", "0080c0", "back", "black"), 9, Map("text", "fuchsia", "back", "black"), 10, Map("text", "804040", "back", "black"), 11, Map("text", "FF8040", "back", "black"), 12, Map("text", "teal", "back", "black"), 13, Map("text", "004080", "back", "black"), 14, Map("text", "FF0080", "back", "black"), 15, Map("text", "green", "back", "black"), 16, Map("text", "blue", "back", "black"))

PInfo := Gui("+Resize +MinSize500x25", "Dynamic Color Viewer")
tcustomColorE := PInfo.Add("DropDownList", "w140 h300 Choose1", ["(no change)", "Custom1", "Custom2", "Custom3", "Custom4", "Custom5", "Custom6", "Custom7", "Custom8", "Custom9", "Custom10", "Custom11", "Custom12", "Custom13", "Custom14", "Custom15", "Custom16", "Other"])
tcustom_sampleText := PInfo.Add("Edit", "xp+200 yp w100 h16 Hidden Center", "Sample text")
tcustomColorE.OnEvent("Change", customColorChange)

PInfo.Show

customColorChange(thisDDL, info)
{
    if (thisDDL.value == 1)
    {
        tcustom_sampleText.Visible := false
        return
    }
    
    tcustom_sampleText.Visible := false
    tcustom_sampleText.TextColor := customColorsMap[thisDDL.value - 1]["text"]
    tcustom_sampleText.BackColor := customColorsMap[thisDDL.value - 1]["back"]
    tcustom_sampleText.Redraw()
    tcustom_sampleText.Visible := true
}
So, is there a way I can update the "Sample text" whenever a change in value is detected with the DDL?

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Change a text or edit field's colors dynamically in a Gui  Topic is solved

Post by teadrinker » 07 Jun 2023, 09:40

Code: Select all

colors := [{text: 'FF8080', back: '196363'},
           {text: 'FFFF80', back: '8E5AAA'},
           {text: '80FF80', back: 'A62A2A'},
           {text: '80FFFF', back: 'FFBA80'}]

ddlItems := ['(no change)', 'Custom1', 'Custom2', 'Custom3', 'Custom4']

wnd := Gui()
wnd.SetFont('s12', 'Calibri')
wnd.AddDropDownList('Choose1', ddlItems).OnEvent('Change', (s, *) => (
    txt := s.Gui['Edit1'],
    s.value = 1 ? (txt.SetFont('cDefault'),
                   txt.Opt('Background'))
                : (txt.SetFont('c'      . colors[s.value - 1].text),
                   txt.Opt('Background' . colors[s.value - 1].back))
))
wnd.AddEdit('xp y+10 wp', 'Sample text')
wnd.Show()

arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Re: Change a text or edit field's colors dynamically in a Gui

Post by arcylix » 07 Jun 2023, 10:04

@teadrinker

Once again, you've been a help! Thanks!

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Change a text or edit field's colors dynamically in a Gui

Post by teadrinker » 07 Jun 2023, 10:28

It's even easier this way:

Code: Select all

colors := [{text: 'Default', back: ''},
           {text: 'FF8080' , back: '196363'},
           {text: 'FFFF80' , back: '8E5AAA'},
           {text: '80FF80' , back: 'A62A2A'},
           {text: '80FFFF' , back: 'FFBA80'}]

ddlItems := ['(no change)', 'Custom1', 'Custom2', 'Custom3', 'Custom4']

wnd := Gui()
wnd.SetFont('s12', 'Calibri')
wnd.AddDropDownList('Choose1', ddlItems).OnEvent('Change', (s, *) => (
    txt := s.Gui['Edit1'],
    txt.SetFont('c'      . colors[s.value].text),
    txt.Opt('Background' . colors[s.value].back)
))
wnd.AddEdit('xp y+10 wp', 'Sample text')
wnd.Show()

Draken
Posts: 42
Joined: 08 Dec 2022, 01:19

Re: Change a text or edit field's colors dynamically in a Gui

Post by Draken » 08 Oct 2023, 06:50

@teadrinker
Hello, I would like to use a part of this code to make the Choose option loaded on startup, the script loads the values only when changed. At the same time please fix the bug where Edit1 is overwritten by Edit2 value. Thank you very much.

Code: Select all

colors := [{var1: '500', var2: '25'},
           {var1: '600', var2: '35'},
           {var1: '700', var2: '45'},
           {var1: '800', var2: '55'},
           {var1: '900', var2: '65'}]


ddlItems := ['(no change)', 'Custom1', 'Custom2', 'Custom3', 'Custom4']

Gui1 := Gui()
Gui1.SetFont('s12', 'Calibri')
Gui1.AddDropDownList('Choose2', ddlItems).OnEvent('Change', (s, *) => (
    s.Gui['Edit1'].Value := colors[s.value].var1
    s.Gui['Edit2'].Value := colors[s.value].var2
))
Gui1.AddEdit('xp y+10 wp', )
Gui1.AddEdit('xp y+10 wp', )
Gui1.Show()

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Change a text or edit field's colors dynamically in a Gui

Post by teadrinker » 08 Oct 2023, 07:14

Code: Select all

colors := [{var1: '500', var2: '25'},
           {var1: '600', var2: '35'},
           {var1: '700', var2: '45'},
           {var1: '800', var2: '55'},
           {var1: '900', var2: '65'}]


ddlItems := ['(no change)', 'Custom1', 'Custom2', 'Custom3', 'Custom4']

Gui1 := Gui()
Gui1.SetFont('s12', 'Calibri')
Gui1.AddDropDownList('Choose2', ddlItems).OnEvent('Change', (s, *) => (
    s.Gui['Edit1'].Value := colors[s.value].var1,
    s.Gui['Edit2'].Value := colors[s.value].var2
))
Gui1.AddEdit('xp y+10 wp', '600')
Gui1.AddEdit('xp y+10 wp', '35')
Gui1.Show()

Post Reply

Return to “Ask for Help (v2)”