Using the ComboBox

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
westerw
Posts: 14
Joined: Tue Apr 09, 2013 7:33 pm

Using the ComboBox

Post by westerw » Thu Jun 06, 2013 1:26 pm

I have been going round-round looking for code samples on how to use the LC ComboBox with little success. Any links to code sources and/or projects -- much appreciated.

I am now able to put my data selections into the ComboBox and click a choice which changes the label of the ComboBox. But, I would like to implement a script to filter the choices and ensure that the user only types a valid selection from the list OR add a new entry to the list.

Looking for help on:
1) How can I iterate (repeat) through the text list from top-bottom and bottom-top?
2) How can I remove an entry from the list? This is needed to filter the list.
3) How can I determine when the user leaves the ComboBox?
4) When the user leaves the ComboBox, I want to check to be sure the Label is in the list.
If not, I plan to ask the user if this is a "new" entry.
If YES, then how can I add an entry in sorted order to the list?

Thank you for any help.
bill

Klaus
Posts: 12007
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Using the ComboBox

Post by Klaus » Thu Jun 06, 2013 1:55 pm

Hi Bill,

I don't think there is any special documentation for a "combobox".
Check the dictionary for some useful hints!

1.

Code: Select all

...
put the TEXT of btn "your combo box here" into tList
put the num of lines of tList into tNumOfLines
## TOP to BOTTOM
repeat with i = 1 to tNumOfLines
  ## do something with -> line i of tList
end repeat

## BOTTOM to TOP
repeat with k = tNumOfLines down to 1
 ## do your stuff here
end repeat
...
2.

Code: Select all

...
put the TEXT of btn "your combo box here" into tList
## delete line X of tList or whatever
set the text of btn "your combo box here" to tList
...
3. Since a COMBOBOX is kind of a hybrid button_field, it will receive FIELD messages like
"closefield" (user left the field and did change its content!) and "exitfield" (user left field but did NOT change the content.

4.

Code: Select all

...
put the TEXT of btn "your combo box here" into tList
put the LABEL of btn "your combo box here" into tEntry
set the wholematches to TRUE
put lineoffset(tEntry,tList) into tFound
if tFound = 0 then
   ## user entry NOT in list
   ## ask user...

   ## Add entry
   put CR & tEntry after tList
   ## Now sort again
   sort tList
   set the text of btn "your combo box here" to tList 
end if
...
Best

Klaus

westerw
Posts: 14
Joined: Tue Apr 09, 2013 7:33 pm

Re: Using the ComboBox

Post by westerw » Thu Jun 06, 2013 4:27 pm

Klaus

Almost there!!
I got a script to work for "trimming" the list based on the keystrokes entered.
I would like the list to remain open under the text entry area so the user can see the remaining choices.
How can I force a "click" on the drop-down arrow in my script.

FYI: script so far
on keyUp pKey
put the label of button "cbxTitle" into theValue
put length(theValue) into theValueLen
put the text of button "cbxTitle" into tList
put the num of lines of tList into tNumOfLines
--answer the label of button "cbxTitle" & ": :" & theValue & ": :" & theValueLen & ": :" & tNumOfLines
repeat with k = tNumOfLines down to 1
if (theValue <> char 1 to theValueLen of line k of tList) then
--answer "MATCH: " & k & ": :" & theValue & ": :" & line k of tList
delete line k of tList
end if
end repeat
put tList into button "cbxTitle"
set the label of button "cbxTitle" to theValue
end keyUp

Thank you!
bill

Klaus
Posts: 12007
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Using the ComboBox

Post by Klaus » Thu Jun 06, 2013 4:51 pm

HI Bill,
westerw wrote:...
I would like the list to remain open under the text entry area so the user can see the remaining choices.
How can I force a "click" on the drop-down arrow in my script.
Sorry, I have no idea. Maybe this is not possible at all?
You can also use FILTER (less typing :-)), which is IDEAL for these kind of things:

Code: Select all

on keyUp pKey
   put the label of button "cbxTitle" into theValue
   put the text of button "cbxTitle" into tList

   FILTER tList with (theValue & "'")
   put tList into button "cbxTitle"
   ## Does the label really change?
   ## set the label of button "cbxTitle" to theValue
end keyUp
Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5600
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Using the ComboBox

Post by jacque » Thu Jun 06, 2013 7:02 pm

It's a hack, but try this in the combobox button script:

Code: Select all

on keydown pkey -- set up the filtered content before the list is displayed
  put the cText of me into tText -- grab the original list
  put the label of me & pkey into tLabel
  filter tText with (tLabel & "*")
  lock screen
  put tText into me
  click at (the right of me - 5,item 2 of the loc of me)
  focus on me
  set the label of me to tLabel
  select after me
  unlock screen
end keydown
The button must have the complete list stored as a custom property, which I've named "cText" here. Some keys, like delete, won't trigger unless the list is closed (type Enter to close it.) You could probably experiment with rawKeyDown to fix that.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

westerw
Posts: 14
Joined: Tue Apr 09, 2013 7:33 pm

Re: Using the ComboBox

Post by westerw » Wed Jun 12, 2013 4:06 pm

Klaus wrote:HI Bill,
westerw wrote:...
I would like the list to remain open under the text entry area so the user can see the remaining choices.
How can I force a "click" on the drop-down arrow in my script.
Sorry, I have no idea. Maybe this is not possible at all?
You can also use FILTER (less typing :-)), which is IDEAL for these kind of things:

Code: Select all

on keyUp pKey
   put the label of button "cbxTitle" into theValue
   put the text of button "cbxTitle" into tList

   FILTER tList with (theValue & "'")
   put tList into button "cbxTitle"
   ## Does the label really change?
   ## set the label of button "cbxTitle" to theValue
end keyUp
Best

Klaus

Klaus

I couldn't get the ComboBox to work successfully for what I was trying. So I constructed a Text Entry Field ("txtTitle") with a related Scrolling Field ("lbxTitle") and got the following script to work. Only thing I haven't figured out is how to have LC move its focus from one object to the next "automatically". In Visual Basic, the programmer can set up a "TAB" pattern from one object to the next.

My test stack has three objects: txtTitle, lbxTitle, and Button1

SCRIPT:

local aList

on openField
set the visible of field "lbxTitle" to false
put "" into the field "txtTitle"
end openField

on tabKey
set the visible of field "lbxTitle" to false
put the num of lines of aList into theCount
if (theCount < 1) then
answer "NEW"
end if
focus on button "Button1"
end tabKey

on returnInField
set the visible of field "lbxTitle" to false
put the num of lines of aList into theCount
if (theCount < 1) then
answer "NEW"
end if
focus on button "Button1"
end returnInField

on keyUp pKey
put the field "txtTitle" into theValue
put length(theValue) into theLen
put the txtTitleData of field "lbxTitle" into aList
put the num of lines of aList into theCount

repeat with k = theCount down to 1
if (char 1 to theLen of line k of aList <> theValue) then
delete line k of aList
end if
end repeat

put aList into the field "lbxTitle"
put the num of lines of aList into theCount
set the visible of field "lbxTitle" to true
if (theCount < 1) then
set the visible of field "lbxTitle" to false
else
if (theCount < 10) then
set the height of field "lbxTitle" to 18 * theCount
else
set the height of field "lbxTitle" to 180
end if
end if
set the top of field "lbxTitle" to the bottom of field "txtTitle"
select after text of field "txtTitle"

end keyUp



Thanks for the help.
bill

MaxV
Posts: 1577
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Using the ComboBox

Post by MaxV » Thu Apr 11, 2019 1:00 am

jacque wrote:
Thu Jun 06, 2013 7:02 pm
It's a hack, but try this in the combobox button script:

Code: Select all

on keydown pkey -- set up the filtered content before the list is displayed
  put the cText of me into tText -- grab the original list
  put the label of me & pkey into tLabel
  filter tText with (tLabel & "*")
  lock screen
  put tText into me
  click at (the right of me - 5,item 2 of the loc of me)
  focus on me
  set the label of me to tLabel
  select after me
  unlock screen
end keydown
Found another way to control with keyboard:

SET THE MENUMODE OF BUTTON "myCombobox" TO "OPTION"
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7018
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using the ComboBox

Post by dunbarx » Thu Apr 11, 2019 1:46 am

Westerw.

You are in good hands.

Please place your handlers inside the handler tags (</>).

Craig

Greg O.
Posts: 21
Joined: Wed Jun 18, 2014 4:02 am
Location: Peyton, Colorado

Re: Using the ComboBox

Post by Greg O. » Sat May 18, 2019 5:39 pm

I created a solution some time ago to fix this situation. I can't swear to its elegance, and it doesn't look like a combo box, but it does make a search field with a list of existing values that match.

Code: Select all

####################
/*
Autocomplete text

Two fields: search and results
The search field is an ordinary text field 
The results field is a text field with list behavior turned on  -->"fldNames"

This will search a list and show the items of that list that start
with the text in the search field.
*/
####################


on keyDown pKey
   --this does not include special keys such as return
   --enter , or backspace
   Lock screen
   Put the text of me into ltText
   Put pKey after ltText
   Put ltText into me
   --put the cursor at the end of the entered text
   Select after char -1 of me
end keyDown

On keyUp 
   --filter the list of possible entries based on the 
   --text of the search field
   DoFilter
end keyUp

on openField
   --show the search list on clicking into the search field 
   --provided it is not empty
   If the text of me is not empty then
      DoFilter
   End if
   Pass openField
end openField


On closeField
   --if the field is empty and the mouse is not inside the key list field
   --hide the key list field
   --otherwise the user clicked inside the keylist
   If the Text of me is empty \
         AND the mouseLoc is not within the rect of fld "fldNames"  then
      Hide fld "fldNames" 
      --clear all fields and diable the "Clear" button
      ClearRecord
      disable btn "btnClear"
   End if
   if the mouseLoc is not within the rect of fld "fldNames" then
      Hide fld "fldNames"
   End if
   --passing the Close field message allows the default text behavior to work.
   Pass closeField
end closeField

On exitField
   --if the field is empty and the mouse is not inside the key list field
   --hide the key list field
   --otherwise the user clicked inside the keylist
   If the text of me is empty \
         AND the mouseLoc is not within the rect of fld "fldNames" then
      Hide fld "fldNames"
      ClearRecord
   End if
   if the mouseLoc is not within the rect of fld "fldNames" then
      Hide fld "fldNames"
   End if
   Pass exitField
end exitField

on rawKeyUp pKey
   --on backspace
   --this updates the results as the text changes
   if pKey is 65288 then
      DoFilter
   End if
   Pass rawKeyUp   
end rawKeyUp


On DoFilter
   ###############################
   /*
   get and filter the names of active clergy members
   
   */
   ##############################
   
   Local ltPreList
   Local tSQL
   Local ltList
   Local ltFilter
   
   Lock screen
   --this will prevent the possible entries from being edited on accident
   --build SELECT query for the auto complete
   [complete list of entries]
    ###################
   /*
   i got the data from a sqlite database but a custom
   property with the entire list would work just as well.
   */
   ###################
   --parse the information in prelist
   --item del is tab for returned info
  
   
   --sort the list
   sort lines of ltList ascending by item 1 of each
   
   Put the uText of fld "fldSearchName" & "*" into ltFilter  --uText
   --filter the list
   Filter ltList with ltFilter
   --load the fldNames box
   Put ltList into fld "fldNames"
   Show fld "fldNames"
   --put the cursor at the end of the entered text
   Select after char -1 of me
   --keep the fldNames box fitting the text
   --maximum height of the fldNames box is 130
   If the formattedheight of fld "fldNames" < 130 then
      Set the height of fld "fldNames" to the formattedHeight of fld "fldNames"
      Set the vScrollBar of fld "fldNames" to false
   Else
      Set the height of fld "fldNames" to 130
      Set the vScrollBar of fld "fldNames" to true
   End if
   --make sure the fldNames box still aligns correctly
   set the top of fld "fldNames" to the bottom of me +1
   Set the left of fld "fldNames" to the left of me
end DoFilter

--in the list display field script  --"fldNames" as referenced in the code above
on mouseUp
   --put the clicked line into the search field
   --then hide the search results box
   select the selectedLine
   set the uText of fld "fldSearchName" to the selectedText 
   <do stuff>
   put empty into me
   Hide me 
end MouseUp

Post Reply

Return to “Talking LiveCode”