Changing the color of a button in a DScrollPanel list? (noob question)

Oi m8s… need some help here. How would I go about changing the color of the DButtons I created here everytime I clicked on that button?
All of the buttons are in a DScrollPanel by the way. Thanks in advance… :smile::smile:


 
--cl_init
	for k, v in pairs(panel1table) do
		local name = v
		v = vgui.Create( "DButton", DScrollPanel1 )
		v:SetFont( "hud_exo18" )
		v:SetText( "["..k.."]".. name )
		v:SetTextColor( Color( 0, 0, 0, 255 ) )
		v:SetPos( 0, k * 45 - 45 )
		v:SetSize( 330, 45 )
		v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( Something like v.colorselected or something? ) ) end
		v.DoClick = function( ply )

		end
	end


[editline]1st October 2016[/editline]

Now that I think about it, this should have probably gone in the “Problem that don’t need their own thread” thread. :ohno: Sorry about that to those who are anal about that sort of thing

What you’re looking for is BUTTON.Paint = function(self) code end)

[editline]2nd October 2016[/editline]

Sorry, didn’t read your code properly, you could check if the specific button is selected by storing it on itself I.e DoClick then store that specific button, then if that specific button == true then just paint, else do paint if not clicked.


 
--cl_init
	for k, v in pairs(panel1table) do
		local name = v
		v = vgui.Create( "DButton", DScrollPanel1 )
		v:SetFont( "hud_exo18" )
		v:SetText( "["..k.."]".. name )
		v:SetTextColor( Color( 0, 0, 0, 255 ) )
		v:SetPos( 0, k * 45 - 45 )
		v:SetSize( 330, 45 )
		v.DoClick = function( ply )
                         v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color(10,10,10,250) ) end
		end
	end


Wrong, he wants to change the color with an click.

If you want them to change their colors only on click

DButton:IsDown


DButton.Paint = function(self, w, h)
   if self:IsDown() then
      draw.RoundedBox(0, 0, 0, w, h, color_white)
   else
      draw.RoundedBox(0, 0, 0, w, h, color_black)
   end
end

Or this, if you want to change colors on each click



DButton.colour = color_white
DButton.DoClick = function(self)
   self.colour = ColorRand()
end
DButton.Paint = function(self, w, h)
   draw.RoundedBox(0, 0, 0, w, h, self.colour)
end

Apologies for the super late reply, haven’t really been messing with GLua much. :pudge: I know that the code you gave me there changes colors of each button, but how would I go about setting the other colors while changing the current one when DoClick() is ran. (v in this instance) So far, I have tried using a for loop to loop through the table and set all of the v.Colours to white, but it doesn’t seem to work.



			local O = Color(255, 150, 250, 255)
			local OT = Color(255, 150, 250, 0)
			
			for k, v in pairs(panel1table) do
				local vtable = {} -- !(NEW)
				local name = v
				local selected = nil
				v = vgui.Create( "DButton", DScrollPanel1 )
				v.Colour = OT
				v:SetFont( "hud_exo18" )
				v:SetText( "["..k.."]".. string.upper(string.TrimLeft( name, "cw_" )) )
				v:SetTextColor( Color( 0, 0, 0, 255 ) )
				v:SetPos( 0, k * 45 - 45 )
				v:SetSize( 330, 45 )
				for i=1, #panel1table do 
					table.insert( vtable, v ) 
				end 
				v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, self.Colour ) end 
				v.DoClick = function( self )
					primarySelected = name
					selected = self
					self.Colour = O
					for i=1, #vtable do
						if !selected == vtable* then -- if not equal to self then run thru and turn everything into white
							vtable*.Colour = OT
						end
					end
				end
			end


[editline]9th October 2016[/editline]

I think I am inserting the same DButton into the table(vtable) multiple times(however much #panel1table is to be exact)…

[editline]9th October 2016[/editline]

Yea, that is definitely what seems to be happening :goodjob: