DComboBox/ChooseOptionID

From Garry's Mod
Jump to: navigation, search
 DComboBox:ChooseOptionID( )

Contents

DescriptionPanelFunc

Selects an option within a combo box based on its table index.

Arguments

number index

Selects the option with given index.

Examples

Example

A simple combo box menu which gives choices for a favorite lunch meal, including a non-preference choice which randomly chooses an option.

BGPanel = vgui.Create("DPanel")
BGPanel:SetPos(20, 20)
BGPanel:SetSize(200, 100)

-- Text output
local lbl = vgui.Create("DLabel", BGPanel)
lbl:SetPos(10, 80)
lbl:SetSize(180, 20)
lbl:SetDark(true)
lbl:SetText("You choose...")

-- Combo box
local cbox = vgui.Create("DComboBox", BGPanel)
cbox:SetPos(5, 5)
cbox:SetSize(190, 20)

cbox:SetValue("What's your favorite lunch meal?")

-- Choices
cbox:AddChoice("BBQ Chicken")
cbox:AddChoice("Fish and Chips")
cbox:AddChoice("Pizza")
cbox:AddChoice("Potato Salad")
cbox:AddChoice("Roast Beef Sandwich")
cbox:AddChoice("Spaghetti")

-- No preference: data is set to -1
cbox:AddChoice("I don't have a favorite.", -1)

function cbox:OnSelect(index, value, data)

	-- No preference? Choose a random choice
	if(data == -1) then				
		self:ChooseOptionID(math.random(1, 6))
		
	-- Otherwise update the text label with our choice
	else
		lbl:SetText("You choose "..value..".")
	end
	
end

Output:



DComboBox ChooseOptionID example1.gif
Personal tools
Navigation