-
Oct 26th, 2012, 08:32 AM
#1
Thread Starter
Junior Member
Simple adding windows program
Hello guys,
I was programming this simple adding numbers program and I thought about developing it more and more
The First code is :
Code:
Dim number1, number2, sum As Integer
number1 = TextBox1.Text
number2 = TextBox2.Text
sum = number1 + number2
TextBox3.Text = sum
MessageBox.Show("Thank you for using me.")
Not that text box 3 is read only !
Now I want to make the program give an error message it the input in textbox1 and textbox2 was nothing or not integer (number)
How i do that
I tried :
Code:
if TextBox1.Text = "" then
MsgBox("ERROR")
end if
but I keep getting error that it cannot convert Integer to String !
Any Ideas ??
THANKS,
-
Nov 5th, 2012, 06:25 AM
#2
Re: Simple adding windows program
That check shouldn't cause your error so I suspect your problem is that your not exiting on an error state. Post your whole code.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Nov 16th, 2012, 11:47 PM
#3
Re: Simple adding windows program
Parsing the value in the text box works.
vb Code:
number1 = Integer.Parse(TextBox1.Text)
Edit:
Use it like so:
vb Code:
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim number1, number2, sum As Integer If Not TextBox1.Text = "" Then number1 = Integer.Parse(TextBox1.Text) number2 = TextBox2.Text sum = number1 + number2 TextBox3.Text = sum MessageBox.Show("Thank you for using me.") Else MessageBox.Show("ERROR") End If End Sub End Class
Last edited by Nightwalker83; Nov 16th, 2012 at 11:54 PM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 21st, 2012, 02:19 AM
#4
Re: Simple adding windows program
You should always check the user inputs. A proper validation should be done before doing the actual operation.
It's better to use Integer.TryParse() method to "try" converting the value from the TextBoxes to an Integer value.
Here's the example:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'~~~ Declaring the variables
Dim num1 As Integer
Dim num2 As Integer
Dim result As Integer
If Integer.TryParse(TextBox1.Text, num1) Then '~~~ we are trying to convert the value of 'TextBox1' to Integer value. If success, the value would be stored in 'num1' variable.
If Integer.TryParse(TextBox2.Text, num2) Then '~~~ Now, we are going after the second value. That is, trying to convert the value of 'TextBox2' to Integer value. If success, the value would be stored in 'num2' variable.
result = num1 + num2 '~~~ we got the two values. So, we are adding them and storing the result in the variable named 'result'
'~~~ Now displaying the result
MessageBox.Show(String.Format("{0} + {1} = {2}", num1.ToString, num2.ToString, result.ToString)) '~~~ We are going to display the result in a MessageBox. We are using String.Format() function to format the string with placeholders.
Else '~~~ if the conversion of value from "TextBox2" to Integer fails..
MessageBox.Show("Second number should be a valid integer !", "Error")
End If
Else '~~~ if the conversion of value from "TextBox1" to Integer fails..
MessageBox.Show("First number should be a valid integer !", "Error")
End If
End Sub
End Class
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|