HKH1967
asked on
VB.NET textbox default value 0
How to make a textbox if empty to a value 0 (zero)
To add to jppinto's example, you may consider trimming the value before you test its length--in case the user entered all spaces.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ippinto:
i try it and it gives below error msg
Conversion from string "" to type 'Decimal' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastExceptio n: Conversion from string "" to type 'Decimal' is not valid.
Source Error:
Line 174:
Line 175: Protected Sub txtDocAmount_TextChanged(B yVal sender As Object, ByVal e As System.EventArgs) Handles txtDocAmount.TextChanged
Line 176: If CDec(txtAgentCommission.Te xt) = "" Then
Line 177: txtAgentCommission.Text = "0"
Line 178: End If
Source File: E:\Projects\NcbWork\Defaul t.aspx.vb Line: 176
i try it and it gives below error msg
Conversion from string "" to type 'Decimal' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastExceptio
Source Error:
Line 174:
Line 175: Protected Sub txtDocAmount_TextChanged(B
Line 176: If CDec(txtAgentCommission.Te
Line 177: txtAgentCommission.Text = "0"
Line 178: End If
Source File: E:\Projects\NcbWork\Defaul
let me know if I am wrong..
u want to make the textbox text as "0" when it is empy, right ?
why don u try this , its simple.
u want to make the textbox text as "0" when it is empy, right ?
why don u try this , its simple.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "".Trim Then
TextBox1.Text = "0"
End If
End Sub
@bilal_fazlani
Is it not a bit redundant to trim an empty string literal ; )
@HKH1967
Why are you using CDec? You are trying to compare a decimal to a string. That doesn't make sense. Take another look at what jppinto posted = )
Is it not a bit redundant to trim an empty string literal ; )
@HKH1967
Why are you using CDec? You are trying to compare a decimal to a string. That doesn't make sense. Take another look at what jppinto posted = )
ohhhh...yeah
i guess it should have been
If TextBox1.Text.trim = "" Then
OR simply
If TextBox1.Text = "" Then
i guess it should have been
If TextBox1.Text.trim = "" Then
OR simply
If TextBox1.Text = "" Then
ASKER
thanks
txtName.Text = "0"
End If