TextBox Casting and Validation

By ahassan81 ·

I have got 4 texboxes which should handle Double type numbers. At the access database in the back-end, I created them as Text data type. The program needs to multiply those 4 values to get a desired number. My 2 questions are:

1. I am trying to do the multiplication on the change handle of every text box, but I get an error which states that "Cast from string"" to type 'Double' is not valid". I made sure that the initial value of the text box is not empty but I am not successful.

2. What is the best way to force the user to enter the numbers in a fraction format i.e. 0.XXXX. Because if a whole number is entered it would mess up the whole calculation.

Appreciate your help on this because I am new to VB.NET

Awaiting replies

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Re:TextBox Casting and Validation

by ahmedali_2020 In reply to TextBox Casting and Valid ...

Before Multiplication, you have to convert from string to double...
to do that use the function CDbl([Object as Expression]) for example:
Dim string1 as String
Dim double1 as Double
string1 = TextBox.Text
double1 = CDbl(string1)
'you do that with the rest of 3 textboxes..
and then do multiplication
dim total as double
total = double1 * double2 * double...

Hope that will answer your question....

Collapse -

Re:TextBox Casting and Validation

by ahassan81 In reply to Re:TextBox Casting and Va ...

Thanks Ahmed for your reply.
In fact, I had tried that before, but it didn't work. I still got the error which says that converting from string "" to double is not valid.

For some reason, the text box initial value will be empty no matter how I try to make it 0. Be it through the text property or in the database.

Anyways, I found out how to do it. I did it using the VAL() function and it worked fine.

Thanks again.

Related Discussions

Related Forums