Trying to create a GUI preview which changes based on DDL selection but it doesnt work Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ntvinh
Posts: 3
Joined: 21 Mar 2023, 21:57

Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by ntvinh » 21 Mar 2023, 22:25

Can anyone tell me what's wrong with my code? Why doesn't the variable in the template for preview section get updated upon changes in DDL selection? Thanks!

Code: Select all

animals := ["elephant", "horse", "whale"]
favanimal := "elephant"
content :=
(
  "I'd love to have a safari trip.
  My most favorite animal is " favanimal
)

myGui := Gui()
myGui.AddText("w300", "Select animal:")
ddlanimal := myGui.AddDropDownList("w300 Choose1", animals)
myGui.AddText("w300", "Preview:")
preview := myGui.AddEdit("w300 R5", content)

ddlanimal.OnEvent("Change", selection)

selection(*) {
  favanimal := ddlanimal.Text
}

SubmitBtn(*) {
  ExitApp
}

myGui.AddButton("Default wp y+10", "Submit")
  .OnEvent("Click", SubmitBtn)
myGui.Show()  

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by mikeyww » 22 Mar 2023, 05:05

Welcome to this AutoHotkey forum!

Your preview object is called preview. To change the object's text, set preview.Text.

Code: Select all

preview.Text := ddlanimal.Text

ntvinh
Posts: 3
Joined: 21 Mar 2023, 21:57

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by ntvinh » 22 Mar 2023, 06:27

mikeyww wrote:
22 Mar 2023, 05:05
Welcome to this AutoHotkey forum!

Your preview object is called preview. To change the object's text, set preview.Text.

Code: Select all

preview.Text := ddlanimal.Text
Thanks for your reply. Sorry for being a little dim but I still don't get it. Isn't

Code: Select all

preview.Text := ddlanimal.Text
only changed the text in "preview" to just the animal name selected from the drop down list?

My intention is that I want the preview text to be "I'd love to have a safari trip. My most favorite animal is XXX" where XXX is the animal selected from the drop drop list. That's why I set up the preview text to be the variable "content" and just update the animal name variable "favanimal" when there is a change in DDL but somehow it does not work :| I know the "favanimal" did indeed receive new value when I tried to show it up by

Code: Select all

selection(*) {
  favanimal := ddlanimal.Text
  MsgBox favanimal
}
but the "content" is still the same.

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work  Topic is solved

Post by mikeyww » 22 Mar 2023, 07:13

Sure, you can have any text you like.

Changing favanimal does not change preview.Text, so change the object as I have already indicated.

Code: Select all

preview.Text := "I'd love to have a safari trip.`r`nMy favorite animal is " ddlanimal.Text "."

ntvinh
Posts: 3
Joined: 21 Mar 2023, 21:57

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by ntvinh » 22 Mar 2023, 07:39

mikeyww wrote:
22 Mar 2023, 07:13
Sure, you can have any text you like.

Changing favanimal does not change preview.Text, so change the object as I have already indicated.

Code: Select all

preview.Text := "I'd love to have a safari trip.`r`nMy favorite animal is " ddlanimal.Text "."
This is working, wow. Thank you :D

However If you don't mind can you explain to me why does

Code: Select all

selection(*) {
  favanimal := ddlanimal.Text
  preview.Text := content
}
also not working? Why content does not change although the variable inside it favanimal changed, which makes changing the preview.Text through content not working? Sorry if I sound stubborn as I really need to understand why it doesn't work as this is only a simplified version of what I'm trying to code, in which the actual text template is fairly large with quite a few variables as favanimal and the template content needs to be reusable so I chose to construct it that way.

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by neogna2 » 22 Mar 2023, 09:16

ntvinh wrote:
22 Mar 2023, 07:39
Why content does not change although the variable inside it favanimal changed
Because when you created the variable content you assigned it the string "I'd love to..." and another string "elephant" (the contents of favanimal at that time). When you later read the content variable it still has exactly the same strings that you put in it.

Code: Select all

a := "world"
b := "hello " a
a := "hi"
MsgBox b ;hello world

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Trying to create a GUI preview which changes based on DDL selection but it doesnt work

Post by mikeyww » 22 Mar 2023, 10:01

Yes, when a variable is defined by referring to another variable, it is still determined at that specific moment.

Post Reply

Return to “Ask for Help (v2)”