There are two textboxes, TextBox 1 and TextBox 2. So the price of 1 value of Textbox 1 is $5 while Textbox 2's price of 1 value is $10. So if I put 4 in Textbox 1, then it'll be multiplied to $5 so $20. But the problem here is that there's only one Textbox to show the Total Amount :( (Sorry, I'm just a young fellow who is new to VB. teaching myself :(Doing basics :( )

Whenever the user changes either text box just update the total amount to
$5 x (number in box 1) + $10 x (number in box 2)

You can achieve this by simply using the 'IF' statement. Try this out.

 REM: Check if not both TextBox1 and TextBox2 are empty.
 If TextBox1.Text IsNot Nothing Then
     REM: TextBox1 is not empty so we know we can use this control.
     Dim Result As Double
     Result = 5 * TextBox1.Text
     TextBoxAnswer.Text = Result
 Else
     If TextBox2.Text IsNot Nothing Then
         REM: TextBox2 is not empty so we know we will have to use it.
         Dim Result As Double
         Result = 10 * TextBox2.Text
         TextBoxAnswer.Text = Result
     Else
         REM: Here is to check just in case you have entered values in both TextBox1 and TextBox2.
         If TextBox1.Text IsNot Nothing And TextBox2.Text IsNot Nothing Then
             Dim Result As Double
             Result = (5 * TextBox1.Text) + (10 * TextBox2.Text)
             TextBoxAnswer.Text = Result
         Else
             MsgBox("Please enter at least one value on one or both fields")
         End If
     End If
 End If

This will also prevent some Null exception as it will only use the desired calculation based on which texts is not null. But this will not handle the conversion from string to integer or double exception you will have to first check if the value entered is only digit.

Edited 1 Year Ago by Reverend Jim: Corrected code formatting

Ow I forgot to state that you can instead of declaring a variable multiple times, you could only declare it once as a Global Variable, or Local Variable your choice.

You never gave a name for the name of the result textbox. Problem with trying to help is that there are so many different ways to give you what you ask. ASP.Net allows us to create new instances of page objects (textbox for example) which you can then apply properties. One result might look something like:

Dim tbAnswer as TextBox
tbAnswer.Text = (TextBox1.Text * 5) + (TextBox2.Text *10)

TextBox.Text values are strings and should be tested/converted to numeric before trying a calculation. Either that or the operation should be done within a Try/Catch block.

TextBoxTotal.Text is the Textbox for the Total Amount Sorry ^^ Anyway, thank you so much! I'm learning a lot :D

This article has been dead for over six months. Start a new discussion instead.