Detecting a Listbox Header Column number


When I click Listbox Header, how can I detect the column number?

I can’t work it out.

I’ve tried;
Code :
LISTBOX GET CELL POSITION(*;$t_ObjectName;$l_Column;$l_Row)
$l_Column and $l_Row are both zero

Code :
OBJECT Get name(Object current)
And inferring the column number from the Column Name, but this returns the Name of the Listbox, not the Column.

The only way I’ve found is to put an Object Method in each column and hard code the column number there. But, this doesn’t work for generic programming, and breaks if I change the order of the columns either in Design or at Runtime.

It seems like it should be easy, but I’m stuck. Any ideas anyone?

In case it makes a difference, the Listbox is displaying a Collection.

Thanks in advance for any help.

Try this in the listbox method in (Form event=On Header Click)

ARRAY TEXT($HeaderNames;0)
ARRAY POINTER($HeaderVars;0)
ARRAY BOOLEAN($ColsVisible;0)
LISTBOX GET ARRAYS(*;$LBName;$ColNames;$HeaderNames;$ColVars;$HeaderVars;$ColsVisible;$Styles)

$pos:=Find in array($HeaderVars;Self)
$text:=OBJECT Get title(Self->)

Note that $pos can be a different number than the column that appears to be clicked on if a column before the clicked column is invisible. Then you’d need to use $ColsVisible.

Hi Peter,
does it help if you have the column name?

Keith, that’s it. Self is the key.

Arnaud, yes I have column names, which is what I’m using to get the Collection property bound to the column. The hard part was getting the column number.

It seems all unnecessarily complicated. Surely the clicked column number is something you’d want to know about. I think I’ll put in a Feature Request.

Thanks for the help.

1 Like

In case it helps anyone in the future, here’s a method I wrote to do this. It returns an Object which contains more than just the Column number.

<code 4D>
//Method LISTBOX get clicked header col

C_OBJECT($0) //Object containing Column, ColumnName, HeaderName
C_TEXT($1) //Listbox Name
C_POINTER($2) //Optional. Pointer to the Header var. Default = Self. NOTE Self is only valid if this method is called during an “On Header Clicked” Event


$0:=New object
If (Count parameters<2)
End if

ARRAY TEXT($at_ColNames;0)
ARRAY TEXT($at_ColNames;0)
ARRAY POINTER($ap_ColVars;0)
ARRAY POINTER($ap_HeaderVars;0)
ARRAY BOOLEAN($ab_ColVisible;0)
ARRAY POINTER($ap_Styles;0)

LISTBOX GET ARRAYS(*;$t_ListboxName;$at_ColNames;$at_HeaderNames;$ap_ColVars;$ap_HeaderVars;$ab_ColVisible;$ap_Styles)
$0.Column:=Find in array($ap_HeaderVars;$p_HeaderVar)

If ($0.Column>0)
End if

</code 4D>

Slight correction to the code in my previous message.

Line 21 should be;
Code :
ARRAY TEXT($at_HeaderNames;0)


The column number are now returned by the Form event command and so more… You can find more details on the 4D blog.

Download 4D v18 R2 and find all the 4D v18 R2 new features on our web site.


1 Like

Like in docu is written.
Must only place in listbox-method (not in all headers/columns)
Form event.column gives you the column-position in listbox
during on-header-click.
Bildschirmfoto 2020-05-05 um 16.22.02

Vanessa’s post gave me an idea of a generic method for getting info from the clicked Listbox Header.

Here is the method for v18R2:

If (Count parameters>=1)
	$event_o:=FORM Event
	If ($event_o.description="On Header Click")
		Case of 
			: ($toGet="name")
			: ($toGet="number")
			: ($toGet="title")
				$0:=OBJECT Get title(*;$event_o.headerName)
			: ($toGet="pointer")
				$0:=OBJECT Get pointer(Object named;$event_o.headerName)
		End case 
	End if 
End if 

The use of C_VARIANT (in 18R2) allows me to return any type of data.

Here are an example calls to this method from the Object method of a Listbox.

$headerName:=clickedListboxHeader ("name")
$headerNumber:=clickedListboxHeader ("number")
$headerTitle:=clickedListboxHeader ("title")
$headerPtr:=clickedListboxHeader ("pointer")

Hope it is useful for someone.


Excellent news. Thanks Vanessa.