Modifying IDL Combobox Widgets
QUESTION: I have an editable combobox widget, with the drop-down filled with standard values. But I would like to be able to add the user-supplied value to this list, so it is available to the user subsequently. Is there a way to do this?
ANSWER: You can use the GET_VALUE and SET_VALUE keywords to Widget_Control for this purpose. You get the current list of selections as a string array from the combobox widget, add your new value to the list, then set the list back as the new value of the combobox. Here is an example program that allows you to add different animal names to a combobox widget. Any animal that the user types in the widget is added to the combobox list, if it is not currently already there.
PRO ComboWidgetTest_Event, event IF event.index EQ -1 THEN BEGIN Widget_Control, event.id, GET_VALUE=currentList i = Where(StrUpCase(currentList) EQ StrUpCase(event.str), count) IF count EQ 0 THEN BEGIN newList = [currentList, event.str] Widget_Control, event.id, SET_VALUE=newList Widget_Control, event.id, SET_COMBOBOX_SELECT=N_Elements(newList)-1 ENDIF ENDIF END ;----------------------------------------------------------------------- PRO ComboWidgetTest tlb = Widget_Base(/COLUMN, XOFFSET=150, YOFFSET=150) combo = Widget_Combobox(tlb, VALUE=['dog', 'cow', 'coyote'], $ /EDIT, SCR_XSIZE=100) Widget_Control, tlb, /REALIZE XManager, 'combowidgettest', tlb, /NO_BLOCK END ;-----------------------------------------------------------------------
QUESTION: Well, sure, but what I was really hoping for is to put a value into the edit box of combobox widget, without changing the the values in the drop-down list. And ideas about that?
ANSWER: Oh, my goodness. Well, we just need to exercise a bit more creativity. How about this program modification. In this new program, you can pass in a value that you would like to see in the combobox widget, but it doesn't persist in the pull-down menu.
PRO ComboWidgetTest2_Event, event Widget_Control, event.id, GET_UVALUE=defaultList IF event.index EQ -1 THEN BEGIN i = Where(StrUpCase(defaultList) EQ StrUpCase(event.str), count) IF count EQ 0 THEN BEGIN newList = [defaultList, event.str] Widget_Control, event.id, SET_VALUE=newList Widget_Control, event.id, SET_COMBOBOX_SELECT=N_Elements(newList)-1 ENDIF ENDIF ELSE BEGIN IF event.index EQ N_Elements(defaultList) THEN RETURN Widget_Control, event.id, SET_VALUE=defaultList Widget_Control, event.id, SET_COMBOBOX_SELECT=event.index ENDELSE END ;----------------------------------------------------------------------- PRO ComboWidgetTest2, newValue IF N_Elements(newValue) EQ 0 THEN newValue = 'pig' tlb = Widget_Base(/COLUMN, XOFFSET=150, YOFFSET=150) defaultValues = ['dog', 'cow', 'coyote'] combo = Widget_Combobox(tlb, VALUE=[defaultValues, newValue], $ /EDIT, SCR_XSIZE=100, UVALUE=defaultValues) Widget_Control, tlb, /REALIZE Widget_Control, combo, Set_Combobox_Select = 3 XManager, 'combowidgettest2', tlb, /NO_BLOCK END ;-----------------------------------------------------------------------
Version of IDL used to prepare this article: IDL 7.0.3.
Copyright © 2008 David W. Fanning
Last Updated 15 Nov 2008