Dynamically adding Derma items

What I want to do is create a function that adds a button to my Derma menu that can be pressed, does something when pressed, and removes itself.

function OpenInventory()

local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( ScrW()-500,ScrH()-700)
DermaPanel:SetSize( 400, 600 )
DermaPanel:SetTitle( "Backpack" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:ShowCloseButton( true )

DermaList = vgui.Create( "DPanelList", DermaPanel )
DermaList:SetPos( 20,50 )
DermaList:SetSize( 360, 500 )
DermaList:SetSpacing( 1 ) -- Spacing between items
DermaList:EnableHorizontal( true ) -- Only vertical items
DermaList:EnableVerticalScrollbar( true ) -- Allow scrollbar if you exceed the Y axis


function AddItem( ent, desc )
local model = ent:GetModel()
table.insert( inventory, model )
local Icon = vgui.Create( “SpawnIcon” )
Icon:SetModel( model )
Icon:SetToolTip( desc )
Icon.DoClick = function()
LocalPlayer():ConCommand( "inven_dropitem "…model )
DermaList:AddItem( Icon )

All the code I have so far, what I’m uncertain about is can I add an item to a derma panel in a different function? And how can I remove the item in the table that I added when the button is pressed? Also how do I remove the button when it’s pressed? I’ve looked on the wiki and at some other people’s code and none of them seem to have anything very helpful for me. This is all in cl_init for a gamemode I’m working on. Some working examples and explanations of them would be very nice.

One way to do it is to add an argument to the function that includes the list you’re adding it to.

function AddItem( DermaList, ent, desc )

And just pass the panel you want to add to whenever you want to add something.

I don’t know what you mean there.
And how can I remove the button when it gets clicked?

To pass an argument means to give it to the function when you use it. So basically, when using AddItem the first argument to the function would be the list you want to add it to.

For your other thing,

function btn:DoClick() self:SetVisible(false) end