Garry's Mod Wiki

DTextEntry

Description

A form which may be used to display text the player is meant to select and copy or alternately allow them to enter some text of their own.

At least one of your DTextEntry's parents must either be an EditablePanel or derived from it (like a DFrame, for example), else it won't be able to focus and thus be unselectable.

You must also call Panel:MakePopup on said panel or the DTextEntry will not work.

View source

Parent

Derives methods, etc not listed on this page from TextEntry.

Implements

Implements or overrides the following hooks/methods. If you want to override these, you probably want to call the original function too.

Events

DTextEntry:OnChange()
Called by DTextEntry:OnTextChanged when the user modifies the text in the DTextEntry. You should override this function to define custom behavior when the DTextEntry text changes.
DTextEntry:OnEnter( string value )
Called whenever enter is pressed on a DTextEntry. DTextEntry:IsEditing will still return true in this callback!
DTextEntry:OnGetFocus()
Called whenever the DTextEntry gains focus.
DTextEntry:OnKeyCode( number keyCode )
Called from DTextEntry's TextEntry:OnKeyCodeTyped override whenever a valid character is typed while the text entry is focused.
DTextEntry:OnLoseFocus()
Called whenever the DTextEntry lose focus.
DTextEntry:OnValueChange( string value )
Called when the text changes of the DTextEntry are applied. (And set to the attached console variable, if one is given) See also DTextEntry:OnChange for a function that is called on every text change, even if the console variable is not updated. You should override this function to define custom behavior when the text changes. This method is called: When Enter is pressed after typing When DTextEntry:SetValue is used For every key typed - only if DTextEntry:SetUpdateOnType was set to true (default is false)

Methods

DTextEntry:AddHistory( string text )
Adds an entry to DTextEntry's history. See DTextEntry:SetHistoryEnabled.
boolean DTextEntry:AllowInput( string char )
Called whenever the value of the panel has been updated (whether by user input or otherwise). It allows you to determine whether a user can modify the TextEntry's text. By default, this only checks whether the panel disallows numeric characters, preventing it from being edited if the value contains any. This is actually an engine hook that only works on TextEntry derived elements. If you are looking for a way to modify character limits, see Panel:SetMaximumCharCount
boolean DTextEntry:CheckNumeric( string strValue )
Returns whether a string is numeric or not. Always returns false if the DTextEntry:SetNumeric is set to false.
table DTextEntry:GetAutoComplete( string inputText )
Called by the DTextEntry when a list of autocompletion options is requested. Meant to be overridden.
table DTextEntry:GetCursorColor()
Returns the cursor color of a DTextEntry.
boolean DTextEntry:GetDisabled()
We advise against using this. It may be changed or removed in a future update. Use Panel:IsEnabled instead. Returns whether the textbox is disabled. Use Panel:IsEnabled instead.
We advise against using this. It may be changed or removed in a future update. Alias of DTextEntry:GetPaintBackground. Use that instead. Whether the background is displayed or not.
boolean DTextEntry:GetDrawBorder()
We advise against using this. It may be changed or removed in a future update. Returns the value set by DTextEntry:SetDrawBorder.
Returns whether pressing Enter can cause the panel to lose focus. Note that a multiline DTextEntry cannot be escaped using the Enter key even when this function returns true.
number DTextEntry:GetFloat()
Returns the contents of the DTextEntry as a number.
Returns the highlight/text selection color of the text entry. If it was not overwritten, it will return the derma skin value. (colTextEntryTextHighlight)
Returns whether the history functionality of DTextEntry is enabled. See DTextEntry:AddHistory.
number DTextEntry:GetInt()
Similar to DTextEntry:GetFloat, but rounds the value to the nearest integer.
boolean DTextEntry:GetNumeric()
Returns whether only numeric characters (123456789. -) can be entered into the DTextEntry.
Whether the background is displayed or not
Return current color of panel placeholder
Returns the placeholder text set with DTextEntry:SetPlaceholderText.
Returns whether or not the panel accepts tab key.
table DTextEntry:GetTextColor()
Returns the text color of a DTextEntry.
Returns whether the DTextEntry is set to run DTextEntry:OnValueChange every time a character is typed or deleted or only when Enter is pressed.
boolean DTextEntry:IsEditing()
Returns whether this DTextEntry is being edited or not. (i. e. has focus)
DTextEntry:OnTextChanged( boolean noMenuRemoval )
This is used internally - although you're able to use it you probably shouldn't. Called internally when the text inside the DTextEntry changes. This is an implementation of TextEntry:OnTextChanged You should not override this function. Use DTextEntry:OnValueChange instead.
DTextEntry:OpenAutoComplete( table tab )
This is used internally - although you're able to use it you probably shouldn't. You really should be using DTextEntry:GetAutoComplete instead. Builds a DMenu for the DTextEntry based on the input table.
DTextEntry:SetCursorColor( table color )
Sets the cursor's color in DTextEntry (the blinking line).
DTextEntry:SetDisabled( boolean disabled )
We advise against using this. It may be changed or removed in a future update. Use Panel:SetEnabled instead. Disables input on a DTextEntry and greys it out visually. This differs from DTextEntry:SetEditable which doesn't visually change the textbox.
DTextEntry:SetDrawBackground( boolean show )
We advise against using this. It may be changed or removed in a future update. Alias of DTextEntry:SetPaintBackground. Use that instead.
DTextEntry:SetDrawBorder( boolean bool )
We advise against using this. It may be changed or removed in a future update. Does nothing.
DTextEntry:SetEditable( boolean enabled )
Disables Input on a DTextEntry. This differs from DTextEntry:SetDisabled - SetEditable will not affect the appearance of the textbox.
DTextEntry:SetEnterAllowed( boolean allowEnter )
Sets whether pressing the Enter key will cause the DTextEntry to lose focus or not, provided it is not multiline. This is true by default.
DTextEntry:SetFont( string font )
Changes the font of the DTextEntry.
DTextEntry:SetHighlightColor( table color )
Sets/overrides the default highlight/text selection color of the text entry.
DTextEntry:SetHistoryEnabled( boolean enable )
Enables or disables the history functionality of DTextEntry. This allows the player to scroll through history elements using up and down arrow keys. See DTextEntry:AddHistory.
DTextEntry:SetNumeric( boolean numericOnly )
Sets whether or not to decline non-numeric characters as input. Numeric characters are 1234567890. -
DTextEntry:SetPaintBackground( boolean show )
Sets whether to show the default background of the DTextEntry.
DTextEntry:SetPlaceholderColor( table color = Color(128, 128, 128) )
Allow you to set placeholder color.
DTextEntry:SetPlaceholderText( string text = "nil" )
Sets the placeholder text that will be shown while the text entry has no user text. The player will not need to delete the placeholder text if they decide to start typing.
DTextEntry:SetTabbingDisabled( boolean enabled )
Sets whether or not the panel accepts tab key. Disabling tab key prevents the panel from unfocusing by mouse, however, still works for focusing to other keyboard focus.
DTextEntry:SetTextColor( table color )
Sets the text color of the DTextEntry.
DTextEntry:SetUpdateOnType( boolean updateOnType )
Sets whether we should fire DTextEntry:OnValueChange every time we type or delete a character or only when Enter is pressed.
DTextEntry:SetValue( string text )
Sets the text of the DTextEntry and calls DTextEntry:OnValueChange. The text of the DTextEntry only changes if it's not currently being typed in. If you would rather set the text regardless, use Panel:SetText.
DTextEntry:UpdateConvarValue()
This is used internally - although you're able to use it you probably shouldn't. Used by DTextEntry:OnTextChanged, DTextEntry:OnEnter and DTextEntry:OnLoseFocus Updates the ConVar associated with the TextEntry to its new value.
DTextEntry:UpdateFromHistory()
This is used internally - although you're able to use it you probably shouldn't. Used internally to set text from the history.
DTextEntry:UpdateFromMenu()
This is used internally - although you're able to use it you probably shouldn't. Used internally to set text from the autocomplete menu.

Example

Creates a text entry form that allows you to print messages into the local player's chat window.

concommand.Add( "test_textentry", function(ply) local frame = vgui.Create( "DFrame" ) frame:SetSize( 400, 200 ) frame:Center() frame:MakePopup() local TextEntry = vgui.Create( "DTextEntry", frame ) -- create the form as a child of frame TextEntry:Dock( TOP ) TextEntry.OnEnter = function( self ) chat.AddText( self:GetValue() ) -- print the textentry text as a chat message end local TextEntryPH = vgui.Create( "DTextEntry", frame ) TextEntryPH:Dock( TOP ) TextEntryPH:DockMargin( 0, 5, 0, 0 ) TextEntryPH:SetPlaceholderText( "I am a placeholder" ) TextEntryPH.OnEnter = function( self ) chat.AddText( self:GetValue() ) end end )
Output:
image.png

Example

Creates a text entry with a browse button.

-- Create a new frame (window) local frame = vgui.Create( "DFrame" ) frame:SetSize( 300, 300 ) -- Set the size of the frame frame:Center() -- Center the frame on the screen frame:MakePopup() -- Make the frame appear in front of other windows and accept user input -- Create a text entry field within the frame local tEntry = vgui.Create("DTextEntry", frame) tEntry:Dock(TOP) -- Dock the text entry at the top of the frame -- Create a new button within the text entry field local browseBtn = vgui.Create("DButton", tEntry) browseBtn:SetText("...") -- Set the text of the button browseBtn:SetWidth(20) -- Set the width of the button browseBtn:Dock(RIGHT) -- Dock the button to the right side of the text entry field -- Function to be called when the browse button is clicked -- Can be a reference or anonymous (like in this case) browseBtn.DoClick = function() -- Code to open a browser for certain file types should go here end
Output:
tentry_w_browse.png

(For instructions on how to make the file browser, see DFileBrowser)