Dgrid with Scroll Panel add buttons on the right

Hello guys. I’m trying to add a list of weapons. But for some reason when I add a button on a DGrid the button goes left (outside the Scroll Panel/DGrid).



local ScrWeapons = vgui.Create("DScrollPanel", WeaponMenu)
ScrWeapons:SetSize(150, 300)
ScrWeapons:SetPos(250, 10)

local GWeapons = vgui.Create("DGrid", ScrWeapons)
GWeapons:SetCols(1)
GWeapons:SetColWide(150)
GWeapons:SetSize(150, 300)

for k, v in pairs(Weapons) do
	local Button = vgui.Create("DButton")
	Button:SetText(v.Name)
	GWeapons:AddItem(Button)
end


Anyone can help me?

Could you provide a bit of code for the WeaponMenu? I tried to replicate the problem but I can’t since it goes too much to the right for me (because of ScrWeapons:SetPos(250, 10))

What part of the code do You want?

._.

Complete code:



local Weapon = {}
local TierView = 1

local WeaponMenu = vgui.Create("DFrame")
WeaponMenu:SetSize(550, 320)
--WeaponMenu:ShowCloseButton(false)
WeaponMenu:Center()
WeaponMenu:SetTitle("")
WeaponMenu:SetVisible(false)
WeaponMenu.OnPaint = function()
	draw.RoundedBox(0, 0, 0, 550, 320, Color(50, 50, 50))
	draw.RoundedBox(0, 2, 2, 546, 316, Color(200, 200, 200))
	draw.SimpleText(Weapon, "Trebuchet18", 120, 10, Color(255, 255, 255), TEXT_ALIGN_CENTER) 
end

local W = 230
local X = 10
local Y = 40
local PModel = vgui.Create("DPanel", WeaponMenu)
PModel:SetSize(W, W)
PModel:SetPos(X, Y)
PModel.OnPaint = function()
	draw.RoundedBox(0, 0, 0, X, Y, Color(50, 50, 50))
	draw.RoundedBox(0, 2, 2, X - 4, Y - 4, Color(220, 220, 220))
end

local VModel = vgui.Create("DModelPanel", PModel)
VModel:SetSize(W, W)

local BtBack = vgui.Create("DButton", WeaponMenu)
BtBack:SetText("<")
BtBack:SetSize(110, 26)
BtBack:SetPos(X, 280)
BtBack.DoClick = function()
	WeaponMenu:UpdateTier(-1, true)
end

local BtNext = vgui.Create("DButton", WeaponMenu)
BtNext:SetText(">")
BtNext:SetSize(110, 26)
BtNext:SetPos(X + 120, 280)
BtNext.DoClick = function()
	WeaponMenu:UpdateTier(1, true)
end

local ScrWeapons = vgui.Create("DScrollPanel", WeaponMenu)
ScrWeapons:SetSize(150, 300)
ScrWeapons:SetPos(250, 10)

local GWeapons = vgui.Create("DGrid", ScrWeapons)
GWeapons:SetCols(1)
GWeapons:SetColWide(150)
GWeapons:SetSize(150, 300)

local CheckSave = vgui.Create("DCheckBoxLabel", WeaponMenu)
CheckSave:SetSize(100, 26)
CheckSave:SetPos(450, 320)

function WeaponMenu:UpdateTier(Dir, Clock)
	print("Atualizando tier")
	if Clock then
		TierView = ((TierView + Dir + 4) % 4) + 1
	else
		TierView = Dir
	end
	
	for k, v in pairs(GWeapons:GetItems()) do
		GWeapons:RemoveItem(v)
	end
	local Weapons = GetWeapons()[TierView]
	PrintTable(GetWeapons())
	PrintTable(Weapons)
	for k, v in pairs(Weapons) do
		local Button = vgui.Create("DButton")
		Button:SetText(v.Name)
		print("Adicionando " .. v.Name)
		Button.DoClick = function()
			chat.AddText("Clicando no botão")
			net.Start("SendWeapon")
				net.WriteInt(TierView, 8)
				net.WriteInt(k, 8)
				net.WriteBool(CheckSave:GetChecked())
			net.SendToServer()
			if TierView == 4 then
				WeaponMenu:SetVisible(false)
			else
				WeaponMenu:UpdateTier(1, true)
			end
		end
		Button.OnCursorEntered = function()
			VModel:SetModel(v.Model)
		end
		GWeapons:AddItem(Button)
	end
	
	VModel:SetModel(Weapons[1].Model)
	
	WeaponMenu:SetVisible(true)
	WeaponMenu:MakePopup()
end


Instead of



ScrWeapons:SetPos(250, 10)


Try doing



ScrWeapons:SetPos(250, 40)


Since that makes it lower. Also, try replacing



local GWeapons = vgui.Create("DGrid", ScrWeapons)
GWeapons:SetCols(1)
GWeapons:SetColWide(150)
GWeapons:SetSize(150, 300)


With



local GWeapons = vgui.Create("DListLayout", ScrWeapons)
GWeapons:Dock( FILL )


Since it’s meant for lists like the one you’re trying to do, but note if you choose to replace that, you’ll have to do



GWeapons:Add(Button)


Instead of



GWeapons:AddItem(Button)


Also, don’t forget to use docking! It makes positioning things a whole lot easier!

Tried. Didn’t work =/

Can you send a picture of what the panel looks like for you? It looks fine to me.