Automatically open a combobox Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Automatically open a combobox

Post by vsub » 30 Nov 2021, 13:54

When you start typing
For example I start typing "File" and the combobox list will open showing all of the items starting with "File"

Code: Select all

List =
(
File1
File6
File5
File2
File4
Folder1
Folder5
Folder6
Folder7
Folder9
)


Gui, Add, Combobox, x2 y9 w1080 h20 gSearch vSearch r20
Gui, Show, x343 y175 h30 w1089,Gui
Return

Search:
Gui,Submit,NoHide
If StrLen(Search) < 2
Return
Else
{
Loop,Parse,List,`n
{
If (SubStr(A_LoopField,1,StrLen(Search)) = Search )
GuiControl,,ComboBox1,% A_LoopField "|"
}
}
Return
Also how can I delete the list so while I am typing it will recreate it after each letter to make the list contains only items that start with what I typed(the whole word)

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

Re: Automatically open a combobox

Post by mikeyww » 30 Nov 2021, 16:03

Code: Select all

List =
(
File1
File6
File5
File2
File4
Folder1
Folder5
Folder6
Folder7
Folder9
)
Sort, List
default := "|" StrReplace(List, "`n", "|")
Gui, Add, Combobox, x2 y9 w1080 h20 gSearch vSearch r20
Gui, Show, x343 y175 h30 w1089,Gui
Search:
Gui, Submit, NoHide
If (Search = "") {
 GuiControl,, Search, %default%
 Return
} Else filter := ""
For each, line in StrSplit(list, "`n")
 filter .= line ~= "i)^" Search ? "|" line : ""
GuiControl,, Search, % filter > "" ? filter : "|"
GuiControl, Text, Edit1, %Search%
Send {End}
Return

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Automatically open a combobox

Post by vsub » 30 Nov 2021, 16:38

Thanks,this works(the showing only the correct items part)but it does not automatically open the list
If I open it manually and I start typing,the list height will still stays as if there is 20 items
Or if I open it when there is only one result,it will stay at 1 visible if if there is more when I type something else
I cannot select items by pressing down
I cannot type something infront of some text I already typed

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

Re: Automatically open a combobox

Post by mikeyww » 30 Nov 2021, 17:39

You may have to play around with the code and see what is possible. You could probably send a key sequence to drop down the list. I found that when I changed the list, the edit control was reset, so that is why I had to update it again. There are probably various approaches to get your "wish list" to happen.

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

Re: Automatically open a combobox

Post by teadrinker » 30 Nov 2021, 19:47

Like this?

Code: Select all

List =
(
File1
File6
File5
File2
File4
Folder1
Folder5
Folder6
Folder7
Folder9
)
Loop, parse, List, `n, `r
   _List .= (A_Index = 1 ? "" : "|") . A_LoopField
Gui, Add, Combobox, w200, % _List
Gui, Show
OnMessage( 0x111, Func("WM_COMMAND").Bind("|" . _List) )
Return

WM_COMMAND(list, wp, lp) {
   static CB_GETCOMBOBOXINFO := 0x0164
        , CB_SHOWDROPDOWN    := 0x014F
        , CB_SETMINVISIBLE   := 0x1701
        , CBN_EDITCHANGE := 5
        , EM_SETSEL := 0xB1, hEdit := 0
        
   if (wp >> 16 != CBN_EDITCHANGE)
      Return
   
   hCombo := lp
   if !hEdit {
      VarSetCapacity(COMBOBOXINFO, size := 40 + A_PtrSize*3)
      NumPut(size, COMBOBOXINFO)
      SendMessage, CB_GETCOMBOBOXINFO,, &COMBOBOXINFO,, ahk_id %hCombo%
      hEdit := NumGet(COMBOBOXINFO, 40 + A_PtrSize)
   }
   GuiControlGet, text,, %hCombo%
   _list := RegExReplace(list, "i)\|(?!\Q" . text . "\E)[^|]+")
   GuiControl,, %hCombo%, % _list = "" ? "|" : _list
   if !(_list = "" || StrLen(text) < 2 || list ~= "i)\|\Q" . text . "\E(\||$)")
      SendMessage, CB_SHOWDROPDOWN, true,,, ahk_id %hCombo%
   RegExReplace(_list, "\|", "|", count)
   SendMessage, CB_SETMINVISIBLE, count = 0 ? 1 : count,,, ahk_id %hCombo%
   GuiControl, Text, %hEdit%, % text
   SendMessage, EM_SETSEL, -2, -1,, ahk_id %hEdit%
}

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

Re: Automatically open a combobox

Post by mikeyww » 30 Nov 2021, 21:56

Much better indeed!

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Automatically open a combobox

Post by vsub » 01 Dec 2021, 10:35

Much better indeed(I barely understand half of it :lol: )
There are some problems tho

1.If the list variable is very long one,it will cover the whole height of the screen(can I limit it to maybe 20 if there is more than 20 items...adding r20 makes the list too long when there is less than 20 items)
2.When the list is visible,the mouse cursor becomes invisible while hovering over the gui window(to click on the X button)
3.When I start typing,it will not display anything unless I type at least 2 letters and that's fine(that's how I want it)but when I press backspace once(one letter left),it display all of the items that start with that letter and when I press backspace again,it displays all of the items in a very long list
Can it be do so if there is less than 2 letters to clear the list and close the ddl(or maybe close the ddl only)?

Thanks for the help @mikeyww,@teadrinker

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

Re: Automatically open a combobox  Topic is solved

Post by teadrinker » 01 Dec 2021, 12:20

It's easy to correct the first and the third, but I don't know how to fix the second.

Code: Select all

WM_COMMAND(list, wp, lp) {
   static CB_GETCOMBOBOXINFO := 0x0164
        , CB_SHOWDROPDOWN    := 0x014F
        , CB_SETMINVISIBLE   := 0x1701
        , CBN_EDITCHANGE := 5
        , EM_SETSEL := 0xB1, hEdit := 0
        
   if (wp >> 16 != CBN_EDITCHANGE)
      Return
   
   hCombo := lp
   if !hEdit {
      VarSetCapacity(COMBOBOXINFO, size := 40 + A_PtrSize*3)
      NumPut(size, COMBOBOXINFO)
      SendMessage, CB_GETCOMBOBOXINFO,, &COMBOBOXINFO,, ahk_id %hCombo%
      hEdit := NumGet(COMBOBOXINFO, 40 + A_PtrSize)
   }
   GuiControlGet, text,, %hCombo%
   _list := RegExReplace(list, "i)\|(?!\Q" . text . "\E)[^|]+")
   GuiControl,, %hCombo%, % _list = "" ? "|" : _list
   bool := !(_list = "" || StrLen(text) < 2 || list ~= "i)\|\Q" . text . "\E(\||$)")
   SendMessage, CB_SHOWDROPDOWN, bool,,, ahk_id %hCombo%
   RegExReplace(_list, "\|", "|", count)
   SendMessage, CB_SETMINVISIBLE, count = 0 ? 1 : count > 20 ? 20 : count,,, ahk_id %hCombo%
   GuiControl, Text, %hEdit%, % text
   SendMessage, EM_SETSEL, -2, -1,, ahk_id %hEdit%
}

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Automatically open a combobox

Post by vsub » 01 Dec 2021, 12:52

Thanks

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

Re: Automatically open a combobox

Post by teadrinker » 01 Dec 2021, 12:57

Seems like I found a solution:

Code: Select all

List =
(
File1
File6
File5
File2
File4
Folder1
Folder5
Folder6
Folder7
Folder9
)
Loop, parse, List, `n, `r
   _List .= (A_Index = 1 ? "" : "|") . A_LoopField
Gui, Add, Combobox, w200, % _List
Gui, Show
OnMessage( 0x111, Func("WM_COMMAND").Bind(CBN_EDITCHANGE := 5, "|" . _List) )
Return

WM_COMMAND(CBN_EDITCHANGE, list, wp, lp) {
   static CB_GETCOMBOBOXINFO := 0x0164
        , CB_SETMINVISIBLE   := 0x1701
        , CB_SHOWDROPDOWN    := 0x014F
        , WM_SETCURSOR       := 0x0020
        , EM_SETSEL          := 0x00B1
        , hEdit := 0
        
   if (wp >> 16 != CBN_EDITCHANGE)
      Return
   
   hCombo := lp
   if !hEdit {
      VarSetCapacity(COMBOBOXINFO, size := 40 + A_PtrSize*3)
      NumPut(size, COMBOBOXINFO)
      SendMessage, CB_GETCOMBOBOXINFO,, &COMBOBOXINFO,, ahk_id %hCombo%
      hEdit := NumGet(COMBOBOXINFO, 40 + A_PtrSize)
   }
   GuiControlGet, text,, %hCombo%
   _list := RegExReplace(list, "i)\|(?!\Q" . text . "\E)[^|]+")
   GuiControl,, %hCombo%, % _list = "" ? "|" : _list
   bool := !(_list = "" || StrLen(text) < 2 || list ~= "i)\|\Q" . text . "\E(\||$)")
   SendMessage, CB_SHOWDROPDOWN, bool,,, ahk_id %hCombo%
   RegExReplace(_list, "\|", "|", count)
   SendMessage, CB_SETMINVISIBLE, count = 0 ? 1 : count > 20 ? 20 : count,,, ahk_id %hCombo%
   GuiControl, Text, %hEdit%, % text
   SendMessage, EM_SETSEL, -2, -1,, ahk_id %hEdit%
   SendMessage, WM_SETCURSOR,,,, ahk_id %hEdit%
}

Post Reply

Return to “Ask for Help (v1)”