Hi all,<br><br>I only did a quick search, but couldn't find an example of how to use a combo-field%, in particular how to change the values it displays in its drop-down list. I worked one up and post it here in case it is useful to others.<br>
<br>The combo-field% is extended (sub-classed) with the method update-choices, that is passed the list of strings to use for the labels of the new menu items that replace the old (existing) menu items. For the sake of this example, the list of strings to use for the choices are stored in a limited depth stack. Values are stored to the top of the stack from the text entry field of the combo-field% via the callback for the 'add' button%. <br>
<br>I would appreciate any comments as to improvements that I could make. In particular I'm uneasy about removing all the items from the popup menu and then adding a whole new set of items. I feel like I should be reusing existing menu items rather than doing all that creating and destroying.<br>
<br>Cheers,<br><br>Kieron.<br><br>****<br><br>#lang racket/base<br><br>(require racket/gui)<br><br>(define max-items 10)<br>(define max-item-index (sub1 max-items))<br> <br>(define pop-item-to-top? #t)<br>(define allow-duplicate-items? #f)<br>
<br>; model a small stack of strings, 10 deep, new items are pushed at the front <br>(define small-stack <br> (new (class object%<br> (super-new)<br> <br> (define my-list '())<br> <br>
(define/public (clear)<br> (set! my-list '()))<br> <br> (define/public (add-item i)<br> (if pop-item-to-top?<br> ; if item is in the stack, remove it from the stack and push a new copy to the top<br>
(let ([new-list (remove i my-list string=?)])<br> (set! my-list (cons i (take new-list (min (length new-list) max-item-index)))))<br> ; if no duplicates allowed then only add item to stack if not already in the stack<br>
(when (or allow-duplicate-items?<br> (not (memf (lambda (arg) (string=? arg i)) my-list))) ; not already in the list<br> (set! my-list (cons i (take my-list (min (length my-list) max-item-index)))))<br>
))<br> <br> (define/public (get-list)<br> my-list)<br> )))<br><br>(define f <br> (new frame% <br> [label "Test combo-field%"] <br> [min-width 400] <br> [min-height 300]<br>
))<br><br>(define combo-field <br> (new (class combo-field%<br> (super-new)<br> <br> (inherit get-menu append)<br> <br> (define/public (update-choices choice-list)<br> ; remove all the old items<br>
(map<br> (lambda (i)<br> (send i delete))<br> (send (get-menu) get-items))<br> ; set up the menu with all new items<br> (map<br> (lambda (choice-label)<br>
(append choice-label))<br> choice-list)<br> (void)<br> ))<br> [parent f]<br> [label "combo-field%"] <br> [choices (send small-stack get-list)]<br> [stretchable-width #t] <br>
[stretchable-height #f]<br> ))<br><br>(define add-button <br> (new button% <br> [label "add"] <br> [parent f] <br> [callback (lambda (r e) <br> (send small-stack add-item (send combo-field get-value))<br>
(send combo-field update-choices (send small-stack get-list))<br> )]<br> ))<br><br>(define clear-button <br> (new button% <br> [label "clear"] <br> [parent f] <br> [callback (lambda (r e) <br>
(send small-stack clear)<br> (send combo-field update-choices (send small-stack get-list))<br> )]<br> ))<br><br>(send f show #t)<br><br>