Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Macro to add selection in Listbox

Hi

My listbox should always have at least the first entry and another one selected. I have written a macro but it is not really doing anything (but it gets triggered). Found it also a bit difficult to find information how to manipulate qlik objects :-), the API Guide does not contain examples.

Sub include_first()

' always include first element in selection

Set oLB_Cat = ActiveDocument.GetSheetObject("CAT")
Set oCell = oLB_Cat.GetCell(1,0)
oCell.State = 5
msgbox("Cell.State: " & oCell.State)

' if first element only is selected, select second too

If oLB_Cat.GetRowCount = 1 Then
Set oCell = oLB_Cat.GetCell(2,0)
oCell.State = 5
End If

End Sub

Thanks for your help, Juerg

1 Solution

Accepted Solutions
Not applicable
Author

I hope I understand what you're going for.

I set up a dummy app and gave it one field. I then set up a Trigger to fire on any Selection in that field. The macro will select the first value if it is not already selected, while keeping any other selections. Also, if the only thing selected if the first value, it also selects the second value. Here's the Sub:

Sub SelectFirst
set sel = ActiveDocument.Fields("Selections")

Set oLB_Cat = ActiveDocument.GetSheetObject("LB01")
Set oCell = oLB_Cat.GetCell(0,0)
Set oCell2 = oLB_Cat.GetCell(1,0)

selected = ActiveDocument.Variables("vSelected").GetContent.String

' To keep current selections
' and select first
If Not InStr(selected, oCell.Text) Then
sel.ToggleSelect oCell.Text
End If

' Select first only
' sel.Select oCell.Text
If sel.GetSelectedValues.Count = 1 Then
sel.ToggleSelect oCell2.Text
End If
End Sub


I've also attached the sample app.

View solution in original post

1 Reply
Not applicable
Author

I hope I understand what you're going for.

I set up a dummy app and gave it one field. I then set up a Trigger to fire on any Selection in that field. The macro will select the first value if it is not already selected, while keeping any other selections. Also, if the only thing selected if the first value, it also selects the second value. Here's the Sub:

Sub SelectFirst
set sel = ActiveDocument.Fields("Selections")

Set oLB_Cat = ActiveDocument.GetSheetObject("LB01")
Set oCell = oLB_Cat.GetCell(0,0)
Set oCell2 = oLB_Cat.GetCell(1,0)

selected = ActiveDocument.Variables("vSelected").GetContent.String

' To keep current selections
' and select first
If Not InStr(selected, oCell.Text) Then
sel.ToggleSelect oCell.Text
End If

' Select first only
' sel.Select oCell.Text
If sel.GetSelectedValues.Count = 1 Then
sel.ToggleSelect oCell2.Text
End If
End Sub


I've also attached the sample app.