Is there any way to limit the amount of characters that can be inputted into a DTextEntry?
There doesn't appear to be a function available for doing so, but it seems you would be able to override PANEL.OnValueChanged. From there you can restrict the amount of characters by changing the value if it goes above the maximum amount.
This is what I use:
[lua]
local PANEL = vgui.Create("DTextEntry")
PANEL:SetWide(200)
PANEL:SetPos(x,y+15)
PANEL.MaxChars = 5
PANEL.OnTextChanged = function(self)
local txt = self:GetValue()
local amt = string.len(txt)
if amt > self.MaxChars then
self:SetText(self.OldText)
self:SetValue(self.OldText)
else
self.OldText = txt
end
end
[/lua]
Only issue with this way is it brings the text input position back to the beginning, but it works and people will notice they cannot type more than the amount specified.
[QUOTE=brandonj4;38996524]This is what I use:
[lua]
local PANEL = vgui.Create("DTextEntry")
PANEL:SetWide(200)
PANEL:SetPos(x,y+15)
PANEL.MaxChars = 5
PANEL.OnTextChanged = function(self)
local txt = self:GetValue()
local amt = string.len(txt)
if amt > self.MaxChars then
self:SetText(self.OldText)
self:SetValue(self.OldText)
else
self.OldText = txt
end
end
[/lua]
Only issue with this way is it brings the text input position back to the beginning, but it works and people will notice they cannot type more than the amount specified.[/QUOTE]
Thank you :D
I shall test this.
Would be neat if we could just return false in order to deny the text change.
[QUOTE=brandonj4;38996524]This is what I use:
[lua]
local PANEL = vgui.Create("DTextEntry")
PANEL:SetWide(200)
PANEL:SetPos(x,y+15)
PANEL.MaxChars = 5
PANEL.OnTextChanged = function(self)
local txt = self:GetValue()
local amt = string.len(txt)
if amt > self.MaxChars then
self:SetText(self.OldText)
self:SetValue(self.OldText)
else
self.OldText = txt
end
end
[/lua]
Only issue with this way is it brings the text input position back to the beginning, but it works and people will notice they cannot type more than the amount specified.[/QUOTE]
You can set the caret pos of the text entry.
DTextEntry:SetCaretPos(position)
Sorry, you need to Log In to post a reply to this thread.