how to make a list box

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sbrady19
Posts: 348
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

how to make a list box

24 May 2024, 10:44

I tried this code from the manual, could not get what I need to happen.
MyGui.Add("ListView", "r20 w700", ["Name","In Folder","Size (KB)","Type"])
also this
InputBoxObj := InputBox(Prompt, Title, Options, Default)

I need a MsgBox that has 4 options, click on what you want and it runs code for that selection.
Lil help please.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to make a list box

24 May 2024, 11:02

Code: Select all

#Requires AutoHotkey v2.0
g := Gui(, 'Select')
g.SetFont 's10'
g.AddRadio 'w230 vrad', 'Opt1'
g.AddRadio 'wp'       , 'Opt2'
g.AddRadio 'wp'       , 'Opt3'
g.AddRadio 'wp'       , 'Opt4'
g.AddButton('wp Default', 'Go').OnEvent('Click', btn_Click)
g.Show

btn_Click(btn, info) {
 form := g.Submit()
 Switch form.rad {
  Case 1: MsgBox 1
  Case 2: MsgBox 2
  Case 3: MsgBox 3
  Case 4: MsgBox 4
 }
}
sbrady19
Posts: 348
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

Re: how to make a list box

24 May 2024, 13:59

here is your code with my button names, it doesn't work

Code: Select all

g := Gui(, 'Select')
g.SetFont 's10'
g.AddRadio 'w230 vrad', 'Email Features'
g.AddRadio 'wp'       , 'Email NEWS'
g.AddRadio 'wp'       , 'Email Interactive'
g.AddRadio 'wp'       , 'Email Social'
g.AddButton('wp Default', 'Go').OnEvent('Click', btn_Click)
g.Show

btn_Click(btn, info) {
 form := g.Submit()
 Switch form.rad {
  Case 1: ControlClick "Email Feat", "DAW3 v2.ahk",,'Left',1
  Case 2: ControlClick "Email NEWS", "DAW3 v2.ahk",,'Left',1
  Case 3: ControlClick "Email Interactive", "DAW3 v2.ahk",,'Left',1
  Case 4: ControlClick "Email Social", "DAW3 v2.ahk",,'Left',1
 }
}
sbrady19
Posts: 348
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

Re: how to make a list box

24 May 2024, 15:06

I came up with this, I need to get text I clicked on instead of "OK"

Code: Select all

MyGUI := GUI()
MyGUI.SetFont("cBLACK s12 w900","Arial")
MyGui.Add("ListBox", "r5 vColorChoice", ["Email Feat","Email NEWS","Email Inter","Email Social"])
MyGui.AddButton("Default w80", "OK").OnEvent("Click", OK)     ;Call OK when clicked.
MyGUI.Show


OK(*)
{
MsgBox "OK"
}
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Re: how to make a list box

24 May 2024, 15:35

There are probably other ways too, but this seems to work.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2.0

MyGUI := GUI()
MyGUI.SetFont("cBLACK s12 w900","Arial")
myListBox := MyGui.Add("ListBox", "r5 vColorChoice", ["Email Feat","Email NEWS","Email Inter","Email Social"])
MyGui.AddButton("Default w80", "OK").OnEvent("Click", OK)     ;Call OK when clicked.
MyGUI.Show


OK(*)
{
MsgBox myListBox.Text
}
ste(phen|ve) kunkel
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: how to make a list box

24 May 2024, 15:58

Get rid of the OK button? You might also want to consider using a menu for this or something like this:

Code: Select all

#Requires AutoHotkey v2.0

g := Gui("-caption", "Select")
g.marginx := g.marginy := 0
g.setfont("s14")
mp1 := map("calc","calc.exe","notepad","notepad.exe","excel","excel.exe","word","winword.exe")
LB := g.Add("ListBox", "r" mp1.count, [mp1*])
LB.OnEvent("change",(*) => run(mp1[LB.text]))
g.OnEvent("escape", (*) => exitapp())
g.Show
14.3 & 1.3.7
sbrady19
Posts: 348
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

Re: how to make a list box

24 May 2024, 16:12

Dman...........you are DaMan

I put YOUR GUI inside MY gui
when I click a button, your gui opens
when I hit ESC, MY gui closes

1-how can I close YOUR gui only and not mine
2-how can I make it control click a button on MY gui when I click a word on your gui

thanks
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: how to make a list box

25 May 2024, 10:23

sbrady19 wrote: ...
I need a MsgBox that has 4 options, click on what you want and it runs code for that selection.
Lil help please.

Code: Select all

#Requires AutoHotkey v2.0
Title := "Type of Email"
Main := "Which kind of Email do you want to send?"
Content := "Select one option, please."
Selection := ["Email Features", "Email NEWS", "Email Interactive", "Email Social"]
Btn := 0
SelRB := RadioBox(Main, Selection, -1, Content, Title, &Btn)
MsgBox(SelRB ? "Your selection: " Selection[SelRB] : "No selection!")
ExitApp
; ======================================================================================================================
; Creates a task dialog querying the user to select one of several options.
; Parameters:
;     Main     -  a string to be used for the main instruction
;     Radios   -  an array of one or more radio button captions to be used for the selection
;     DefRadio -  the default radio button:
;                   -1 -  no default button
;                    0 -  the first button (default)
;                    n -  the button specified by n
;     Content  -  a string to be used for the dialog's primary content.
;                    Default: "" -> no content
;     Title    -  a string to be used for the task dialog title
;                    Default: "" -> the script's file name
;     BtnID    -  optional VarRef to receive the ID of the pressed common button
;                    0 -  no button (error)
;                    1 -  OK button
;                    2 -  CANCEL button
;  Return values:
;     Returns 0 if an error occurred, the user cancelled the dialog, or no radio button is selected.
;     Returns the index of the selected radio button on success.
; ----------------------------------------------------------------------------------------------------------------------
; TaskDialogIndirect -> https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-taskdialogindirect
; TASKDIALOGCONFIG   -> https://learn.microsoft.com/en-us/windows/win32/api/commctrl/ns-commctrl-taskdialogconfig
; TASKDIALOG_BUTTON  -> https://learn.microsoft.com/en-us/windows/win32/api/Commctrl/ns-commctrl-taskdialog_button
; TDF_SIZE_TO_CONTENT = 0x01000000, TDF_ALLOW_DIALOG_CANCELLATION = 8, TDF_NO_DEFAULT_RADIO_BUTTON = 0x4000
; TDCBF_OK_BUTTON = 1, TDCBF_CANCEL_BUTTON = 8
; ======================================================================================================================
RadioBox(Main, Radios, DefRadio := 0, Content := "", Title := "", &BtnID := 0) {
   Static TDBSize := 4 + A_PtrSize                 ; size of TASKDIALOG_BUTTON structure
   Static TDCSize := (4 * 8) + (A_PtrSize * 16)    ; size of TASKDIALOGCONFIG structure
   Static MinRID := 10                             ; minimum radio button ID
   BtnID := 0
   If (Type(Radios) != "Array") || !Radios.Length
      Throw TypeError("Expected an array but got " . Type(Radios), -1, "Radios")
   Local RBCnt := Radios.Length
   If !IsInteger(DefRadio)
      Throw TypeError("Expected an integer but got " . Type(DefRadio), -1, "DefRadio")
   If !(DefRadio <= RBCnt)
      Throw ValueError("DefRadio is invalid!" . Type(DefRadio), -1, DefRadio)
   DefRadio += MinRID
   If (Title = "")
      Title := A_ScriptName
   Local Flags := 0x01000000 | (DefRadio < MinRID ? 0x00004000 : 0) ; TDF_SIZE_TO_CONTENT, TDF_NO_DEFAULT_RADIO_BUTTON
   Local RBArr := [] ; needed to store the captions for the radio buttons
   Local RBBuf := Buffer(TDBSize * RBCnt, 0) ; C array of TASKDIALOG_BUTTON structures
   Local Addr := RBBuf.Ptr
   Local Buf := ""
   For I, V In Radios {
      Buf := Buffer((StrLen(V) + 1) * 2, 0)
      StrPut(V, Buf.Ptr)
      RBArr.Push(Buf)
      Addr := NumPut("Int", MinRID + I, "Ptr", Buf.Ptr, Addr)
   }
   ; TASKDIALOGCONFIG structure
   Local TDC := Buffer(TDCSize, 0)
   NumPut("UInt", TDCSize, TDC)                                                     ; cbSize
   NumPut("Ptr", A_ScriptHwnd, TDC, 4)                                              ; hwndParent
   NumPut("UInt", Flags, TDC, 4 + (A_PtrSize * 2))                                  ; dwFlags
   NumPut("UInt", 9, TDC, 8 + (A_PtrSize * 2))                                      ; dwCommonButtons
   NumPut("Ptr", StrPtr(Title), TDC, 12 + (A_PtrSize * 2))                          ; pszWindowTitle
   NumPut("Ptr", StrPtr(Main), TDC, 12 + (A_PtrSize * 4))                           ; pszMainInstruction
   Numput("Ptr", (Content = "") ? 0 : StrPtr(Content), TDC, 12 + (A_PtrSize * 5))   ; pszContent
   NumPut("Int", RBCnt, TDC, 20 + (A_PtrSize * 7))                                  ; cRadioButtons
   NumPut("Ptr", RBBuf.Ptr, TDC, 24 + (A_PtrSize * 7))                              ; pRadioButtons
   Numput("Int", (DefRadio > MinRID) ? DefRadio : 0, TDC, 24 + (A_PtrSize * 8))     ; nDefaultRadioButton
   Local RID := 0    ; ID of the selected radio button
   If !(RV := DllCall("Comctl32.dll\TaskDialogIndirect", "Ptr", TDC, "IntP", &BtnID, "IntP", &RID, "Ptr", 0, "UInt"))
      Return (RID = 0) ? 0 : !(BtnID = 1) ? 0 : (RID - MinRID)
   Throw OSError(RV)
}
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: how to make a list box

25 May 2024, 11:10

@just me
The code works correctly, but this part looks weird:

Code: Select all

   ; TASKDIALOGCONFIG structure
   Local TDC := Buffer(TDCSize, 0)
   NumPut("UInt", TDCSize, TDC)                                                     ; cbSize
   NumPut("Ptr", A_ScriptHwnd, TDC, 4)                                              ; hwndParent
   ...
Shouldn't A_ScriptHwnd be on a boundary multiple of A_PtrSize?
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: how to make a list box

26 May 2024, 04:13

Hi @teadrinker,

no. The TaskDialog section is one of the rare sections in Commctrl.h using non-default alignment.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: how to make a list box

26 May 2024, 04:37

just me wrote: non-default alignment
Hello!
Yes, I've already looked at the header file, but I didn't find an explicit indication there that non-default alignment is used. Is there any text that I could read that would make me realize that this is the right thing to do in this case?
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: how to make a list box

26 May 2024, 05:18

Hi, I read about packing and alinment on several places but currently I found juyt two:
pack pragma
Anybody who writes #pragma pack(1) may as well just wear a sign on their forehead that says “I hate RISC”

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Lpanatt and 54 guests