Garry's Mod Wiki

Panel:Dock

  Panel:Dock( number dockType )

Description

Sets the dock type for the panel, making the panel "dock" in a certain direction, modifying it's position and size.

You can set the inner spacing of a panel's docking using Panel:DockPadding, which will affect docked child panels, and you can set the outer spacing of a panel's docking using Panel:DockMargin, which affects how docked siblings are positioned/sized.

You may need to use Panel:SetZPos to ensure child panels (DTextEntry) stay in a specific order.

After using this function, if you want to get the correct panel's bounds (position, size), use Panel:InvalidateParent (use true as argument if you need to update immediately)

Arguments

1 number dockType
Dock type using DOCK enum.

Example

Example docking including DockMargin. Provided by Walrus Viking in this Facepunch post.

local f = vgui.Create( "DFrame" ) f:SetTitle( "Dock Test" ) f:SetSize( 256, 256 ) f:Center() f:MakePopup() local p = vgui.Create( "DPanel", f ) p:Dock( FILL ) p:DockMargin( 0, 0, 0, 0 ) for i = 0, 10, 1 do local l = vgui.Create( "DLabel", p ) l:Dock( TOP ) l:DockMargin( 4, 0, 0, 0 ) -- shift to the right l:SetColor( color_black ) l:SetText( "Hi! I'm a label!" ) end
Output:

Example

Example showing how multiple docked elements behave.

local frame = vgui.Create("DFrame") frame:SetSize(600, 300) frame:SetTitle("Docking Demonstration") frame:Center() frame:MakePopup(true) local panel = vgui.Create("DPanel", frame) --Create a panel on the left panel:SetSize(300, 0) --Height doesn't matter since we're docking it to the left anyways panel:Dock(LEFT) local fill = vgui.Create("DButton", panel) --Create a button and dock it fill:SetText("FILL") fill:Dock(FILL) local left = vgui.Create("DButton", panel) left:SetText("LEFT") left:Dock(LEFT) local right = vgui.Create("DButton", panel) right:SetText("RIGHT") right:Dock(RIGHT) local top = vgui.Create("DButton", panel) top:SetText("TOP") top:Dock(TOP) local bottom = vgui.Create("DButton", panel) bottom:SetText("BOTTOM") bottom:Dock(BOTTOM) local panel = vgui.Create("DPanel", frame) --Do the same thing on the right, but this time with top and bottom before left and right panel:SetSize(300, 0) panel:Dock(RIGHT) local fill = vgui.Create("DButton", panel) fill:SetText("FILL") fill:Dock(FILL) local top = vgui.Create("DButton", panel) top:SetText("TOP") top:Dock(TOP) local bottom = vgui.Create("DButton", panel) bottom:SetText("BOTTOM") bottom:Dock(BOTTOM) local left = vgui.Create("DButton", panel) left:SetText("LEFT") left:Dock(LEFT) local right = vgui.Create("DButton", panel) right:SetText("RIGHT") right:Dock(RIGHT)
Output: