How to send a variable to a keys value in a map? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

How to send a variable to a keys value in a map?

Post by Krd » 06 Jun 2023, 06:47

Hey!

Is it possible to send the input from the edit control to the value of the corresponding key in the map as shown below?
The problem here is that the "MyVar" requires to be set before even the GUI is created which is useless in this case as the user must input that value first.

If I remove line 6 then the script doesn't work.

Or am I again trying something which is not designed to work as desired here?

Code: Select all

F12::MyGUIFunc()


MyGUIFunc()
{
	global MyVar := ''
	MyMap := MyMap_Func()
	keyarr := []
	For key In MyMap
	keyarr.Push(key)
	MyGui := Gui()
	MyGui.Add("DropDownList", "vMyDDL sort", keyarr)
	MyGui.Add("Edit", "vMyEdit")
	MyGui.Add("Button", , "Submit").OnEvent("Click", OK)
	MyGui.Show()

OK(*)
    {
	Saved := MyGui.Submit(true)
	global MyVar := Saved.MyEdit
	MsgBox MyMap[Saved.MyDDL]
	}

}

MyMap_Func()
{
global MyVar
Return Map(
	'Key1', 'This is MyEdit value: ' MyVar ', in value one',
	'Key2', 'This is MyEdit value: ' MyVar ', in value two'
	)
}

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

Re: How to send a variable to a keys value in a map?

Post by mikeyww » 06 Jun 2023, 07:22

What is your example? Are you typing a key or a value? How will the other part be identified?

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to send a variable to a keys value in a map?

Post by Krd » 06 Jun 2023, 07:31

When the user selects, for example, "Key2," the resulting value will be the value associated with "Key2" but with the additional content from MyEdit included.

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

Re: How to send a variable to a keys value in a map?

Post by mikeyww » 06 Jun 2023, 07:35

It will help if you can provide more detail in your example. Consider a list of steps, detailing every individual step, in terms of what the user does, and what the script does.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to send a variable to a keys value in a map?

Post by Krd » 07 Jun 2023, 02:22

I have a variation of this approach where I utilize maps to store static values for use in a GUI.

However, I recently had another idea as I find the use of maps convenient for easy maintenance.
I am currently using the same map for another purpose and I thought about utilizing it here as well, before deciding what steps to take next if this is not feasible.

Let's refer to the keys as customer names, with the static addresses being the corresponding static values stored in the map.
The "MyEdit" control would represent the phone number that users paste in the GUI. I now want to update the rest of the values in the map for each customer, along with their unique static address.

The problem I'm facing is that I don't have the value of "MyVar" before utilizing the map itself. Due to this, the code fails as the variable is not initialized when the "For" loop is executed.

My actual question is: How can I pass the newly chosen global variable from the GUI to the corresponding value in the map, overwriting the initially empty value declared at the beginning of the GUI, just to make it work?

The goal here is to copy the value from the map to the clipboard so that the user can easily paste it wherever they want.

If there was a way to make this actually work:

Code: Select all

	global MyVar := Saved.MyEdit
	MsgBox MyMap[Saved.MyDDL]
No matter what I try to do the MyVar is empty because it is declared as such in the beginning of the GUI.

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

Re: How to send a variable to a keys value in a map?

Post by mikeyww » 07 Jun 2023, 06:47

Code: Select all

#SingleInstance Force

#Requires AutoHotkey v2.0
phone := Map()
gui1  := Gui()
gui1.SetFont('s10')
gui1.AddText('w50', 'Name:')
gui1.AddText('wp' , 'Phone:')
gui1.AddEdit('w230 vkey ym')
gui1.AddEdit('wp   vval')
gui1.AddButton('xm w294 Default', 'Save').OnEvent('Click', save_Click)
gui1.Show

save_Click(btn, info) {
 g := gui1.Submit()
 phone[g.key] := g.val
 MsgBox g.key ': ' phone[g.key]
}

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to send a variable to a keys value in a map?

Post by Krd » 07 Jun 2023, 08:12

I ca not connect that logic to make this to work: :crazy:

Code: Select all

F12::MyGUIFunc()

MyGUIFunc()
{
	MyMap := MyMap_Func()
	keyarr := []
	For key In MyMap
	keyarr.Push(key)
	MyGui := Gui()
	MyGui.Add("DropDownList", "vMyDDL sort", keyarr)
	MyGui.Add("Edit", "vMyEdit")
	MyGui.Add("Button", , "Submit").OnEvent("Click", OK)
	MyGui.Show()

OK(*)
    {
	Saved := MyGui.Submit(true)
	global MyVar := Saved.MyEdit   ;,,,,,,,,,?
	MsgBox MyMap[Saved.MyDDL]   ;,,,,,,,,,?
	}

}

MyMap_Func()
{
Return Map(
	'Key1', 'This is MyEdit value: ' MyVar ', in value one',
	'Key2', 'This is MyEdit value: ' MyVar ', in value two'
	)
}

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

Re: How to send a variable to a keys value in a map?  Topic is solved

Post by mikeyww » 07 Jun 2023, 10:28

Your initial values can be blank or null, but you cannot access a variable without defining it first.

Code: Select all

#Requires AutoHotkey v2.0
phone  := Map(
             'Key1', ''
           , 'Key2', ''
          )
keyarr := []
gui1   := Gui()
For key in phone
 keyarr.Push(key)
gui1.AddDDL( 'vkey Sort', keyarr)
gui1.AddEdit('vval')
gui1.AddButton(, 'Submit').OnEvent('Click', submit_Click)

F12::gui1.Show

submit_Click(btn, info) {
 g := gui1.Submit()
 phone[g.key] := g.val
 MsgBox g.key ': ' phone[g.key]
}

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to send a variable to a keys value in a map?

Post by Krd » 08 Jun 2023, 01:25

That is exactly what I was afraid of. :)

Thank you for delivering exceptional support!

Post Reply

Return to “Ask for Help (v2)”