VGUI buttons

I’m making a little menu and I wanted the color buttons to change what the menu displays (Like, click the button and different labels/buttons appear) but I have no idea how to go about it, I’ve tried panels but I can’t get those to work for some reason.

Current code:


local function menu()

    local base = vgui.Create( "DFrame" )
    base:SetSize( 360, 355 )
    base:SetTitle( "Super cool derma" )
    base:SetVisible( true )
    base:SetDraggable( true )
    base:ShowCloseButton( true )
    base:MakePopup()
    base:Center()

    function base:Paint( w, h )
        draw.RoundedBox( 0, 1, 1, w-2, h-2, Color( 65, 65, 65, 200 ) )
        surface.SetDrawColor( 6, 150, 150, 255 )
        surface.DrawOutlinedRect( 1, 1, w-2, h-2 )

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.DrawOutlinedRect( 100, 35, w-108, h-43 )
    end

    local Button1 = vgui.Create( "DColorButton", base )
    Button1:SetPos( 8, 35 )
    Button1:SetSize( 85, 24 )
    Button1:Paint( 20, 30 )
    Button1:SetText( "         Button1" )
    Button1:SetColor( Color( 6, 110, 160 ) )

    local Button2 = vgui.Create( "DColorButton", base )
    Button2:SetPos( 8, 75 )
    Button2:SetSize( 85, 24 )
    Button2:Paint( 20, 30 )
    Button2:SetText( "         Button2" )
    Button2:SetColor( Color( 6, 110, 160 ) )

    local Button3 = vgui.Create( "DColorButton", base )
    Button3:SetPos( 8, 115 )
    Button3:SetSize( 85, 24 )
    Button3:Paint( 20, 30 )
    Button3:SetText( "        Button3" )
    Button3:SetColor( Color( 6, 110, 160 ) )

    local Button4 = vgui.Create( "DColorButton", base )
    Button4:SetPos( 8, 155 )
    Button4:SetSize( 85, 24 )
    Button4:Paint( 20, 30 )
    Button4:SetText( "        Button4" )
    Button4:SetColor( Color( 6, 110, 160 ) )

end

concommand.Add( "openmenu", menu )

Thanks in advance.

Im not really sure on what you’re meaning by this, but I think this is what you mean:

lets say we click button 4:



    local Button4 = vgui.Create( "DColorButton", base )
    Button4:SetPos( 8, 155 )
    Button4:SetSize( 85, 24 )
    Button4:Paint( 20, 30 )
    Button4:SetText( "        Button4" )
    Button4:SetColor( Color( 6, 110, 160 ) )
    function Button4:DoClick()
     local Button5 = vgui.Create( "DColorButton", base )
         Button5:SetPos( 8, 155 )
         Button5:SetSize( 85, 24 )
         Button5:Paint( 20, 30 )
         Button5:SetText( "        Button4" )
         Button5:SetColor( Color( 6, 110, 160 ) )
    end    



so when you click the button, another one appears.

Another way you could do it is instead of creating new VGUI elements, you reposition the current ones (also setting the ones that are not necessary invisible) and changing the values (text/color/etc).

In a way, yes, this is what I’m looking for. But how do I make it dissapear when I click another button? Because what I’m doing is I want to be able to click button1, then labels and checkboxes appear, then go to button 2 and different things appear. But nothing from button1 would stay.

Edit: I got the buttons to display things down, but can’t get them to clear on a different button click so they overlap and generally look bad.



function --whateverthenewbuttonis:DoClick()

     --YourCheckBoxVariable:Remove()


end


idk, will have to see your code to be exact (this is a guess of what you mightve done)

Ahh, so with this


    local Button1 = vgui.Create( "DColorButton", base )
    Button1:SetPos( 8, 35 )
    Button1:SetSize( 85, 24 )
    Button1:Paint( 20, 30 )
    Button1:SetText( "         Button1" )
    Button1:SetColor( Color( 6, 110, 160 ) )
        function Button1:DoClick()
            local GodCheck = vgui.Create( "DCheckBoxLabel", base )
            GodCheck:SetPos( 110, 43 ) -- +50 each checkbox for good spacing
            GodCheck:SetText( "God Mode" )
            GodCheck:SetConVar( "sbox_godmode" )
            GodCheck:SizeToContents()
        end

    local Button2 = vgui.Create( "DColorButton", base )
    Button2:SetPos( 8, 75 )
    Button2:SetSize( 85, 24 )
    Button2:Paint( 20, 30 )
    Button2:SetText( "         Button2" )
    Button2:SetColor( Color( 6, 110, 160 ) )
        function Button2:DoClick()
            local GodCheck2 = vgui.Create( "DCheckBoxLabel", base )
            GodCheck2:SetPos( 110, 63 )
            GodCheck2:SetText( "Cow Worship" )
            GodCheck2:SetConVar( "sbox_godmode" )
            GodCheck2:SizeToContents()
        end


I just put (Say to remove the god option when I click b2) GodCheck:Remove()?

Edit: Hm, guess not gives me the error



[ERROR] lua/derma.lua:51: attempt to index global 'GodCheck' (a nil value)
  1. DoClick - lua/derma.lua:51
   2. unknown - lua/vgui/dlabel.lua:206


You are creating the variable GodCheck inside of your button:DoClick() as a local variable. that means that you can’t access it outside of that function.
Try something like this:
[lua]
local GodCheck2
function Button2:DoClick()
GodCheck2 = vgui.Create( “DCheckBoxLabel”, base )
GodCheck2:SetPos( 110, 63 )
GodCheck2:SetText( “Cow Worship” )
GodCheck2:SetConVar( “sbox_godmode” )
GodCheck2:SizeToContents()
end
[/lua]
By declaring it as local ABOVE and OUTSIDE the DoClick function, you change the limit of its use from WITHIN the DoClick function to anywhere below that declaration in the entire file.
This will allow you to call GodCheck2:Remove() to remove the button.

“Attempt to index global ‘blah’ (a nil value)” means you are trying to use a variable which does not exist from wherever you are trying to get it at.

There are two reasons that local variables exist by the way: To prevent overwriting other variables and to speed up the code a little bit.

Damn, you beat me to it! What bobble said should work, making it global.

Not trying to remove the buttons, but the checkboxes themselves when I click a button.

And when I do that it gives this error:



[ERROR] lua/derma.lua:48: attempt to index upvalue 'GodCheck' (a nil value)
  1. DoClick - lua/derma.lua:48
   2. unknown - lua/vgui/dlabel.lua:206


Where did you define GodCheck, and how?

http://pastebin.com/Rzz4rVH4 urrhh, yeah I’ll just give you this to see for yourself.

So basically, in the button section before the buttons. And how you said it should be.

I should mention I tried other locations, like outside the menu function and before each button

Oh I see. You don’t have a default.
Basically, you’re removing the OPPOSITE one that you created.
So when you click the first button, it creates the first one and removes the second one, and the second button creates the second one and removes the first one.

The problem is you don’t have a second one to remove when you click the first one.

This can be solved easily:
[lua]
if IsValid(GodCheck) then
GodCheck:Remove()
end
[/lua]
[lua]
if IsValid(GodCheck2) then
GodCheck2:Remove()
end
[/lua]
That way you don’t remove something that doesn’t exist yet.

Hm, it doesn’t spit out any errors but it doesn’t work either. Just keeps the checkboxlabels there like it would if I didn’t have this code.

Edit: OMG OMG I figured it out… I had the local GodCheck = vgui.Create and when I remove the local it worked… wow.