LBEX - some functions for ListBoxes

Post your working scripts, libraries and tools
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

LBEX - some functions for ListBoxes

02 Oct 2014, 04:20

As usual, the first release of a script will most likely contain bugs. So testers are welcome. ;)

Functions list

LBEX.ahk:

Code: [Select all] [Expand] [Download] (LBEX.ahk)GeSHi © Codebox Plus


:arrow: Short sample script by Joe_Glines_Joetazz.
:arrow: Additional function and example to auto-calculate tab stops by jballi (requires Fnt lib).

Always take a look at GitHub for new/fixed versions. I might forget to update the script in this post. ;)

:arrow: View on GitHub.
:arrow: Download from GitHub.
Last edited by just me on 14 Feb 2016, 12:16, edited 3 times in total.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
User avatar
BGM
Posts: 331
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: LBEX - some functions for ListBoxes

06 Nov 2014, 11:22

Just Me, is there a wrapper library for regular listview functions? If there was, they would operate on the hwnd handle instead of having to always make sure you set the proper listview as default before working on the control - which can be meddlesome when you have multiple listviews.
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

07 Nov 2014, 03:44

This topic applies to ListBoxes. ;)

No, but you can write your own wrapper. The simplest way might be something like this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

But it still might change the current default GUI.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
User avatar
BGM
Posts: 331
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: LBEX - some functions for ListBoxes

07 Nov 2014, 09:33

Just Me - oops, sorry - you're right, this topic does belong to listboxes. Didn't mean to hijack it.
Thanks for the snippet. I know how to write wrappers (I actually wrote a library for the treeview), I was just looking for some that were already done by the pros.

But thank you for the snippet samples. I'll put those to good use!
wdmodlin
Posts: 150
Joined: 16 Dec 2015, 02:42

LBEX_ItemFromPoint(HLB, X, Y)

14 Jan 2016, 14:38

This does not seem to work.
I tried it in a GuiContextMenu() function.

; Index := LBEX_ItemFromPoint(HLB,X,Y) & 0xFFFF

returns a value that changes with the position of the right click, but is too high. Clicking somewhere in the first iline returns 4 or 5, in the second line 6 or 7.

If i do an adhoc computation of the line as line := Floor((Y-30)/14)
then string := LBEX_GetText(HLB,line) works correctly.
Last edited by wdmodlin on 14 Jan 2016, 21:49, edited 1 time in total.
User avatar
Joe Glines
Posts: 372
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: LBEX - some functions for ListBoxes

14 Jan 2016, 21:42

Nice functionality... I don't do much with ListBoxes so I wrote a short example playing with a few of the functions. I don't normally use a handle so I thought I'd go ahead and post my simple example here for others that aren't used to it either.

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Add, ListBox, +HwndHLB vColorChoice h100 w100, Green||Blue
gui, show
Count:=LBEX_GetCount(HLB)
Sleep, 1000
LBEX_Add(HLB, "Added to " count " items")
Sleep, 1000
LBEX_DeleteAll(HLB)
Sleep, 1000
LBEX_Add(HLB, "Added this back")
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

15 Jan 2016, 03:14

@wdmodlin:
The passed coordinates have to be "relative to the upper-left corner of the client area of the list box.", are they? I'll post modified example using the current cursor coordinates in the related thread in "Ask For Help".

@Joe_Glines_Joetazz:
Thanks, I added a link to the OP.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jballi
Posts: 386
Joined: 29 Sep 2013, 17:34

Re: LBEX - some functions for ListBoxes

28 Jan 2016, 23:47

Great library. I haven't found much use for it yet but it's in my arsenal of tools.

Minor inconsistency. Tab stops for the ListBox control are measured in dialog template units. They are sometimes simply called "Dialog Units". The LBEX_SetTabStops function is taking the value for tab stops and then multiplying that value by 4. Dialog template units are usually calculated values. In order to use the function, the developer has to calculate the required dialog template units, then divide the value by 4 (usually will result in a real value) so that the function will have the desired value after multiplying it by 4. Not a big deal but I thought I would mention it.
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

29 Jan 2016, 02:15

Hi jballi,

the description in the MSDN is somewhat unclear about the units. The units in the array passed toLBEX_SetTabStops() are character positions. And the following example is working here:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jballi
Posts: 386
Joined: 29 Sep 2013, 17:34

Re: LBEX - some functions for ListBoxes

29 Jan 2016, 03:28

Not quite. A dialog template unit is a unit of measurement. It is based on the width of the average character of the font. Specifically, the average width of the characters in this string: "ABC..XYZabc...xyz". Your example happens to work but only by luck. One small change and a tab stop of 4 doesn't work anymore. For example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


If we change the 4 (16 dialog template units) to 4.75 (19 dialog template units), then the example works again.

Dialog template units are just smaller units of average character units. The documentation refers to average characters units (more correctly known as dialog base units) but the LB_SETTABSTOPS message uses dialog template units. Since the message uses it, then the function should use it as the parameter value.

Thank you for your consideration.
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

29 Jan 2016, 05:36

I think the keyword here is average character width. All tabs based on an average character width will meet character positions exactly only for fixed-width fonts. Your value 4.75 won't work in all cases, too. Should I change the parameters description to 'avarage character positions'?
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jballi
Posts: 386
Joined: 29 Sep 2013, 17:34

Re: LBEX - some functions for ListBoxes

29 Jan 2016, 06:52

just me wrote:I think the keyword here is average character width.

I think "average character width" is a good mnemonic for understanding how the value is measured but as your example shows, it's misleading to use it as form of measurement. It's also too large a unit of measure.

just me wrote:Your value 4.75 won't work in all cases, too.

Yes, exactly. This should be measured value. Sure, you can do it with trial and error but the value goes to the crapper as soon as the font changes or the text in the column changes.

just me wrote:Should I change the parameters description to 'avarage character positions'?

I think you know my opinion on that one. Dialog Template Unit or DTU. That is the unit of measurement that the message uses.

One last pitch for dialog template units. The Edit control and Rich Edit control also use dialog template units to measure tab stops.

Them be my thoughts.
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

29 Jan 2016, 10:15

ListBox Options wrote:Tn: The letter T may be used to set tab stops, which can be used to format the text into columns. If the letter T is not used, tab stops are set at every 32 dialog units (the width of each "dialog unit" is determined by the operating system). If the letter T is used only once, tab stops are set at every n units across the entire width of the control. For example, Gui, Add, ListBox, vMyListBox t64 would double the default distance between tab stops. To have custom tab stops, specify the letter T multiple times as in the following example: Gui, Add, ListBox, vMyListBox t8 t16 t32 t64 t128. One tab stop is set for each of the absolute column positions in the list, up to a maximum of 50 tab stops.

Source
I still remember my confusion when I read this 'description' the first time:
... (the width of each "dialog unit" is determined by the operating system) ...
did'nt tell me anything.

The same happened when I tried to set tab stops within an edit control with a fixed-width font. I had to do it with trial and error until i finally noticed that 12 dialog units are exactly 3 characters in this case. So for me 'character position' is more descriptive than 'dialog units', and to explain 'dialog units' more detailed, you have to use the term 'average character width' anyway. But I'll change the parameter description to

Code: [Select all] [Download] GeSHi © Codebox Plus

; Parameters:     TabArray -  Array of integers or floating point numbers containing the tab stops as character
; positions according to the average character width of the used font.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jballi
Posts: 386
Joined: 29 Sep 2013, 17:34

Re: LBEX - some functions for ListBoxes

31 Jan 2016, 19:16

I wrote an add-on function for the LBEX library that will automatically set the tab stops for the ListBox control. It requires the LBEX and Fnt libraries. I hope that someone finds it useful.

Code: [Select all] [Expand] [Download] (LBEX_AutoSetTabStops.ahk)GeSHi © Codebox Plus



Example of use:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

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

Re: LBEX - some functions for ListBoxes

01 Feb 2016, 02:40

Nice! I will not add it to the LBEX lib because it requires an additional library, but I added a link in the OT.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jnsm
Posts: 11
Joined: 27 Nov 2015, 06:49

Re: LBEX - some functions for ListBoxes

18 Oct 2016, 04:53

Looks very promising, thank you. Can this code be used to search inside big listboxes populated by delimited txt files?

I've been trying to make it work with following script:

Code: [Select all] [Download] GeSHi © Codebox Plus

ListBox_Text = ;initialize empty variable
Loop, Read, accounts.txt
{
StringSplit, listout, A_LoopReadLine, `,
ListBox_Text .= listout1 "`t" "`t" listout2 "`t" "`t" listout3 "`t" "`t" "`t" listout4 "`t" listout5 "|" ;append a line of text
}

I've used LBEX_Find, but don't understand how it works - I want to make a few keystrokes of an account I am searching for and the result should be focus on the correct line in listbox.

Code: [Select all] [Download] GeSHi © Codebox Plus

Start:
InputBox, srch, What are you looking for?, Type the searchstring
LBEX_Find(listout, %srch%, Index := 0)
;LBEX_Find(listout, %srch%, A_LoopReadLine)
;LBEX_Find(HLB, %srch%, Index := 0)
MsgBox, %LB_FINDSTRING%
Return
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

18 Oct 2016, 05:29

The first parameter of all LBEX functions is the handle to the ListBox. You can easily get it, e.g.

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Add, Listbox, hwndHLB ...


The built-in search used for LB_FINDSTRING and LB_FINDSTRINGEXACT messages sent by LBEX_Find() and LBEX_FindExact() will only find items beginning with the search string (Find) or exactly matching the search string (FindExact).

Also, both functions will only return the index of the item on success. If you want to select the item you have to call LBEX_SetCurSel() passing the returned index.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.
jnsm
Posts: 11
Joined: 27 Nov 2015, 06:49

Re: LBEX - some functions for ListBoxes

19 Oct 2016, 07:32

Thank you very much just me!

I have corrected the handle that I misused for the first time. I didn't paste this line before:

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Add, ListBox, x6 y10 w355 h370 vMyListBox gDbleClick, %ListBox_Text%

If I understand correctly, the handle is vMyListBox and I don't need to pass the third parameter to function?

The thing giving trouble me is that I can't seem to find how the function returns the index which I should pass to LBEX_SetCurSel()..

I tried different combinations but I don't know how do I call the returned result and pass it further?

Code: [Select all] [Download] GeSHi © Codebox Plus

Find:
InputBox, srch, What are you looking for?, Type the searchstring
LBEX_Find(vMyListBox, %srch%)
LBEX_SetCurSel(vMyListBox, Index)
Return

or

Code: [Select all] [Download] GeSHi © Codebox Plus

Test := LBEX_Find(vMyListBox, BEGP)
MsgBox % Test


I hope I am not to annoying with my noobish questions. Wishing you great days!
just me
Posts: 3954
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LBEX - some functions for ListBoxes

19 Oct 2016, 08:20

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Add, ListBox, x6 y10 w355 h370 vMyListBox hwndListBoxHandle gDbleClick, %ListBox_Text%
vMyListBox assigns the variable MyListBox to the control. hwndListBoxHandle stores the handle of the control in the variable ListBoxHandle. This variable can be passed as the first parameter of the LBEX functions.

Code: [Select all] [Download] GeSHi © Codebox Plus

LBEX_Find(vMyListBox, %srch%)
Function calls are expressions. You must not enclose variable names in %-signs in this case. Also, the name of the control variable is MyListBox (w/o the leading v), but you cannot use it here.
Except as noted otherwise all of my code is tested on Win 10 Pro (x64) using the current AHK version.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 13 guests