Hello,
How can we make sure if textbox is set to Number for input type is not letting user to leave the field blank, using the textbox with number automatically validates the numbers are added to the field which is a really good feature but how can we make sure default number value like 0.00 is always there if user clears the field?
I’ve tried creating a usercontrol but lost event is not firing.
Imports System
Imports Wisej.Web
Imports System.ComponentModel
Public Class ModifiedTextBox
Inherits Wisej.Web.TextBox
Private m_Intellisence As String
Private _Textboxtype As EnumTextboxType
Public Enum EnumTextboxType
Normal
NumbersOnly
CharactersOnly
DecimalOnly
End Enum
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property Intellisence() As String
Get
Return m_Intellisence
End Get
Set(ByVal value As String)
m_Intellisence = value
End Set
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property TextboxType() As EnumTextboxType
Get
Return _Textboxtype
End Get
Set(ByVal value As EnumTextboxType)
_Textboxtype = value
End Set
End Property
Protected Overrides Sub OnGotFocus(e As EventArgs)
MyBase.OnGotFocus(e)
Me.SelectAll()
End Sub
Protected Shadows Sub Leave(e As EventArgs)
MyBase.OnLeave(e)
Me.BackColor = Drawing.Color.White
‘ Basic keyword filter (note: not real security)
Dim txt = Me.Text.ToUpper()
If txt.Contains(“SELECT”) OrElse txt.Contains(“UPDATE”) OrElse
txt.Contains(“DELETE”) OrElse txt.Contains(“TRUNCATE”) OrElse
txt.Contains(“DROP”) Then
AlertBox.Show(“Some words are not allowed”)
Me.Text = “”
Return
End If
‘ Type-specific formatting
Select Case TextboxType
Case EnumTextboxType.DecimalOnly
Me.TextAlign = HorizontalAlignment.Right
Dim value As Double
If Double.TryParse(Me.Text, value) Then
Me.Text = value.ToString(“0.00”)
Else
Me.Text = “0.00”
End If
Case EnumTextboxType.NumbersOnly
Dim value As Integer
If Integer.TryParse(Me.Text, value) = False Then
Me.Text = “0”
End If
End Select
End Sub
Protected Shadows Sub KeyPress(e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
Select Case TextboxType
Case EnumTextboxType.NumbersOnly
If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
Case EnumTextboxType.DecimalOnly
If Not Char.IsControl(e.KeyChar) AndAlso
Not Char.IsDigit(e.KeyChar) AndAlso
e.KeyChar <> “.”c Then
e.Handled = True
End If
‘ Only one decimal point allowed
If e.KeyChar = “.”c AndAlso Me.Text.Contains(“.”) Then
e.Handled = True
End If
Case EnumTextboxType.CharactersOnly
If Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Select
End Sub
Public Sub New()
MyBase.New()
End Sub
End Class
You can’t stop characters from the server. Use the Filter property (see docs and api) or a javscript event handler. It’s much better if you attach a zip file with the sample and runnable code. I don’t think anyone would like to type code snippets for you.
Please login first to submit.
