Selectable DLabel

How can I make the text of a DLabel selectable and copyable?

I found this build in feature but it seems it doesn’t work



--[[---------------------------------------------------------
    OnMousePressed
-----------------------------------------------------------]]
function PANEL:OnMousePressed( mousecode )


    if ( self:GetDisabled() ) then return end
    
    if ( mousecode == MOUSE_LEFT && !dragndrop.IsDragging() && self.m_bDoubleClicking ) then
    
        if ( self.LastClickTime && SysTime() - self.LastClickTime < 0.2 ) then
        
            self:DoDoubleClickInternal()
            self:DoDoubleClick()
            return 
            
        end
        
        self.LastClickTime = SysTime()
    end
    
    -- If we're selectable and have shift held down then go up
    -- the parent until we find a selection canvas and start box selection
    if ( self:IsSelectable() && mousecode == MOUSE_LEFT ) then
    
        if ( input.IsShiftDown() ) then 
            return self:StartBoxSelection()
        end
        
    end
    
    self:MouseCapture( true )
    self.Depressed = true
    self:OnDepressed()
    self:InvalidateLayout()
    
    --
    -- Tell DragNDrop that we're down, and might start getting dragged!
    --
    self:DragMousePress( mousecode )


end


function PANEL:OnDepressed()


end


--[[---------------------------------------------------------
    OnMouseReleased
-----------------------------------------------------------]]
function PANEL:OnMouseReleased( mousecode )


    self:MouseCapture( false )
    
    if ( self:GetDisabled() ) then return end
    if ( !self.Depressed ) then return end
    
    self.Depressed = nil
    self:OnReleased()
    self:InvalidateLayout()
    --
    -- If we were being dragged then don't do the default behaviour!
    --
    if ( self:DragMouseRelease( mousecode ) ) then
        return
    end
    
    if ( self:IsSelectable() && mousecode == MOUSE_LEFT ) then
        
        local canvas = self:GetSelectionCanvas()
        if ( canvas ) then
            canvas:UnselectAll()
        end
        
    end
    
    if ( !self.Hovered ) then return end


    --
    -- For the purposes of these callbacks we want to 
    -- keep depressed true. This helps us out in controls
    -- like the checkbox in the properties dialog. Because
    -- the properties dialog will only manualloy change the value
    -- if IsEditing() is true - and the only way to work out if
    -- a label/button based control is editing is when it's depressed.
    --
    self.Depressed = true


    if ( mousecode == MOUSE_RIGHT ) then
        self:DoRightClick()
    end
    
    if ( mousecode == MOUSE_LEFT ) then    
        self:DoClickInternal()
        self:DoClick()
    end


    if ( mousecode == MOUSE_MIDDLE ) then
        self:DoMiddleClick()
    end


    self.Depressed = nil


end


If all else fails you could always make it into a text entry box.
I would go about this by first overriding the OnMousePressed function. If it’s MOUSE_RIGHT then I would open up a DermaMenu() with the options to copy the text to the clipboard. Upon clicking this action I would set the text to the clipboard. It’s all very easy once you get started.

I’m pretty sure the code you’ve referenced is for drag-and-drop selection of panels, not for selection of text.

I’d definitely just go with a DTextEntry since it has the functionality you need. Hide it’s background/border with SetDrawBorder()/SetDrawBackground() and disable entering extra text with yourTextEntry.OnKeyCodeTyped = function() end

That worked, thanks!