?
Solved

VB.NET textbox default value 0

Posted on 2011-10-26
8
Medium Priority
?
878 Views
Last Modified: 2012-05-12
How to make a textbox if empty to a value 0 (zero)
0
Comment
Question by:HKH1967
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 37033892
If Len(txtName.Text) = 0 Then
    txtName.Text = "0"
End If

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37033960
To add to jppinto's example, you may consider trimming the value before you test its length--in case the user entered all spaces.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37034030
If you are using this in a calculation then you may want to use the IIF statement

Dim result As Integer = Integer.Parse(IIF(String.IsNullOrEmpty(txtA.Text), 0, txtA.Text)) + Integer.Parse(IIF(String.IsNullOrEmpty(txtB.Text), 0, txtB.Text))
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:HKH1967
ID: 37036234
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.InvalidCastException: Conversion from string "" to type 'Decimal' is not valid.

Source Error:


Line 174:
Line 175:    Protected Sub txtDocAmount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDocAmount.TextChanged
Line 176:        If CDec(txtAgentCommission.Text) = "" Then
Line 177:            txtAgentCommission.Text = "0"
Line 178:        End If
 

Source File: E:\Projects\NcbWork\Default.aspx.vb    Line: 176

0
 

Expert Comment

by:bilal_fazlani
ID: 37036816
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.

 
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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37037472
@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  = )
0
 

Expert Comment

by:bilal_fazlani
ID: 37037721
ohhhh...yeah
i guess it should have been

   If TextBox1.Text.trim  = "" Then

OR simply

   If TextBox1.Text = "" Then
0
 

Author Closing Comment

by:HKH1967
ID: 37038927
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question