Garry's Mod Wiki

DComboBox

Description

A field with multiple selectable values.

View source

Parent

Derives methods, etc not listed on this page from DButton.

Implements

Implements or overrides the following hooks/methods. If you want to override these, you probably want to call the original function too.

Events

DComboBox:OnMenuOpened( Panel menu )
Called when the player opens the dropdown menu.
DComboBox:OnSelect( number index, string value, any data )
Called when an option in the combo box is selected. This function does nothing by itself, you're supposed to overwrite it.

Methods

number DComboBox:AddChoice( string value, any data = nil, boolean select = false, string icon = "nil" )
Adds a choice to the combo box.
DComboBox:AddSpacer()
Adds a spacer below the currently last item in the drop down. Recommended to use with DComboBox:SetSortItems set to false.
DComboBox:CheckConVarChanges()
This is used internally - although you're able to use it you probably shouldn't. Ran every frame to update the value of this panel to the value of the associated convar. See Panel:SetConvar.
DComboBox:ChooseOption( string value, number index )
Selects a combo box option by its index and changes the text displayed at the top of the combo box.
DComboBox:ChooseOptionID( number index )
Selects an option within a combo box based on its table index.
DComboBox:Clear()
Clears the combo box's text value, choices, and data values.
DComboBox:CloseMenu()
Closes the combo box menu. Called when the combo box is clicked while open.
any DComboBox:GetOptionData( number index )
Returns an option's data based on the given index.
string DComboBox:GetOptionText( number index )
Returns an option's text based on the given index.
Returns an option's text based on the given data.
string, any DComboBox:GetSelected()
Returns the currently selected option's text and data
number DComboBox:GetSelectedID()
Returns the index (ID) of the currently selected option.
boolean DComboBox:GetSortItems()
Returns an whether the items in the dropdown will be alphabetically sorted or not. See DComboBox:SetSortItems.
boolean DComboBox:IsMenuOpen()
Returns whether or not the combo box's menu is opened.
DComboBox:OpenMenu()
Opens the combo box drop down menu. Called when the combo box is clicked.
string, any DComboBox:RemoveChoice( number index )
Removes a choice added with DComboBox:AddChoice
DComboBox:SetSortItems( boolean sort )
Sets whether or not the items should be sorted alphabetically in the dropdown menu of the DComboBox. If set to false, items will appear in the order they were added by DComboBox:AddChoice calls. This is enabled by default.
DComboBox:SetValue( string value )
Sets the text shown in the combo box when the menu is not collapsed.

Example

Creates a Combo Box.

local frame = vgui.Create( "DFrame" ) frame:SetSize( 300, 250 ) frame:Center() frame:MakePopup() local DComboBox = vgui.Create( "DComboBox", frame ) DComboBox:SetPos( 5, 30 ) DComboBox:SetSize( 100, 20 ) DComboBox:SetValue( "options" ) DComboBox:AddChoice( "option A" ) DComboBox:AddChoice( "option B" ) DComboBox:AddChoice( "option C" ) DComboBox.OnSelect = function( self, index, value ) print( value .. " was selected at index " .. index ) end
Output:
option B was selected at index 2

Example

Create a Combo Box with all players.

local comboBox = vgui.Create( "DComboBox" ) comboBox:SetPos( 5, 5 ) comboBox:SetSize( 100, 20 ) comboBox:SetValue( "All Players" ) comboBox.OnSelect = function( _, _, value ) print( value .. " was selected!" ) end for _, v in ipairs( player.GetAll() ) do comboBox:AddChoice( v:Name() ) end

preview from derma_controls concmd

DComboBox.gif