Ho to get a value of ListBox1 control here?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Ho to get a value of ListBox1 control here?

12 Apr 2024, 16:08

Hello.

How to get a value of ListBox1 control here?

Thanks.

Code: Select all

Home:: {
  MyGui := Gui('AlwaysOnTop')
  MyGui.AddText(, "Select category")
  ListBox1 := MyGui.AddListBox('r6 vCategoryChoice', ['Option1', 'Option2', 'Option3', 'Option4', 'Option5', 'Option6'])
  MyBtn1 := MyGui.AddButton('Default w80', 'Submit').OnEvent('Click', MyBtn1_Click)
  MyGui.Show('w400')
}

MyBtn1_Click() {
  ; How to get a value of ListBox1 control above ??
}
Last edited by slishnevsky on 12 Apr 2024, 16:19, edited 1 time in total.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Ho to get a value of ListBox1 control here?

12 Apr 2024, 16:19

Code: Select all

Home:: 
	{
	global ListBox1												; <<<<<<<<<<<
	MyGui := Gui('AlwaysOnTop')
	MyGui.AddText(, "Select category")
	ListBox1 := MyGui.AddListBox('r6 vCategoryChoice', ['Option1', 'Option2', 'Option3', 'Option4', 'Option5', 'Option6'])
	MyBtn1 := MyGui.AddButton('Default w80', 'Submit').OnEvent('Click', MyBtn1_Click)
	MyGui.Show('w400')
	}

MyBtn1_Click(GuiCtrlObj, Info) 
	{
	msgbox listbox1.text										;or
	msgbox ControlGetChoice(listbox1)
	}
You might want to build your GUI outside of the function and have a hotkey only to show it.
14.3 & 1.3.7
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: Ho to get a value of ListBox1 control here?

12 Apr 2024, 16:24

@flyingDman

Cool. Thanks !!

Can I do this inline, without external click event handler function?
I need ListBoxGetChoice() function to return ListBox1 selected option when I click Submit button.

Something like this:

Code: Select all


Ins:: result := ListBoxGetChoice() ; Ins Hotkey to call a function that displays a popup, waits for user selection and returns it

MyGui := Gui('AlwaysOnTop')
MyGui.AddText(, 'Select your option')
ListBox1 := MyGui.AddListBox('r6', ['Option1', 'Option2', 'Option3'])
MyBtn1 := MyGui.AddButton('Default w80', 'Submit')
  
ListBoxGetChoice() {
  MyGui.Show('w400')
  ; What's next? How do I force this window to wait for user selection, and return selected value when a user clicks on Submit button?
  return ListBox1.Text ; This obviously doesn't work
}
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

How to get a value of ListBox1 control here (conitnue)?

12 Apr 2024, 18:09

This is a continuation of my previous thread [Moderator note: Now the same thread.]

Still struggling with passing a value selected in the ListBox back to the Hotkey code, the result variable.
So I have a ButtonClicked event handler, fine, not sure if it is needed at all.
In the Hotkey code I show the popup window, but the code doesnt wait for the user to make a selection, the code continues. I need it to wait for the user to make a selection and click the submit button.

Code: Select all

MyGui := Gui('AlwaysOnTop')
MyGui.AddText(, 'Select your category')
ListBox1 := MyGui.AddListBox('r6', ['Option1', 'Option2', 'Option3'])
MyGui.AddButton('Default w80', 'Submit').OnEvent('Click', ButtonClicked)
ButtonClicked(control, event) {
  return ListBox1.Text ; Not shure how to pass it back to the result variable in the Hotkey defined below
}

Ins:: {
  ; How do I display the popup window and make it wait until a user clicks on Submit button and then assign a value of ListBox1 to the result variable below?
  MyGui.Show() ; Displays the popup
  ; Here the code continues the execution, doesnt wait for user to click the Submit button -> problem, it needs to wait. How?
  result := ListBox1.Text; 
}
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: How to get a value of ListBox1 control here (conitnue)?

12 Apr 2024, 19:17

So you want to create a hotkey (ins::) to send the option selected in the popup?. Try this:

Code: Select all

MyGui := Gui('AlwaysOnTop')
MyGui.AddText(, 'Select your category')
ListBox1 := MyGui.AddListBox('r6', ['Option1', 'Option2', 'Option3'])
MyGui.AddButton('Default w80', 'Submit').OnEvent('Click', ButtonClicked)
MyGui.show()

ButtonClicked(control, event) 
	{
	result := ListBox1.Text	
	Hotkey "Ins", (*) => send(result)
	MyGui.destroy	
	}
14.3 & 1.3.7
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: How to get a value of ListBox1 control here (conitnue)?

13 Apr 2024, 06:24

I think that this is a general (and common) problem with understanding how GUIs work. Showing the GUI does not automatically wait for anything. Instead, you can reorganize your code to accommodate a GUI event. The code to be executed is moved into a function that is called when the user triggers the event. When you understand this, then the approach is fairly simple and straightforward.

Code: Select all

#Requires AutoHotkey v2.0
result := ''
LB_Change := (LB, info) => btn.Enabled := True
g := Gui(, 'ListBox')
g.SetFont 's10'
g.AddListBox('w230 r3 vLB', ['Option1', 'Option2', 'Option3']).OnEvent('Change', LB_Change)
btn := g.AddButton('wp Default Disabled', 'Submit'), btn.OnEvent('Click', btn_Click)

Ins::g.Show

btn_Click(btn, info) {
 form := btn.Gui.Submit()
 Global result := form.LB
 MsgBox result, 'Result', 'Iconi'
}
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Ho to get a value of ListBox1 control here?

13 Apr 2024, 07:28

The separate threads have been merged. @slishnevsky, please do not create new threads to continue the conversation on the same topic.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: akirofe, mikeyww, onandoffwhat, WarlordAkamu67 and 22 guests