Simple adding windows program-VBForums
Results 1 to 4 of 4

Thread: Simple adding windows program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    27

    Question 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,

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,326

    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.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,908

    Re: Simple adding windows program

    Parsing the value in the text box works.

    vb Code:
    1. number1 = Integer.Parse(TextBox1.Text)

    Edit:

    Use it like so:

    vb Code:
    1. Public Class Form1
    2.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    3.         Dim number1, number2, sum As Integer
    4.         If Not TextBox1.Text = "" Then
    5.             number1 = Integer.Parse(TextBox1.Text)
    6.             number2 = TextBox2.Text
    7.             sum = number1 + number2
    8.             TextBox3.Text = sum
    9.             MessageBox.Show("Thank you for using me.")
    10.         Else
    11.              MessageBox.Show("ERROR")
    12.         End If
    13.     End Sub
    14. End Class
    Last edited by Nightwalker83; Nov 16th, 2012 at 10: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.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,573

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.  
    5.         '~~~ Declaring the variables
    6.         Dim num1 As Integer
    7.         Dim num2 As Integer
    8.         Dim result As Integer
    9.  
    10.         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.
    11.             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.
    12.                 result = num1 + num2    '~~~ we got the two values. So, we are adding them and storing the result in the variable named 'result'
    13.  
    14.                 '~~~ Now displaying the result
    15.                 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.
    16.             Else    '~~~ if the conversion of value from "TextBox2" to Integer fails..
    17.                 MessageBox.Show("Second number should be a valid integer !", "Error")
    18.             End If
    19.         Else    '~~~ if the conversion of value from "TextBox1" to Integer fails..
    20.             MessageBox.Show("First number should be a valid integer !", "Error")
    21.         End If
    22.  
    23.     End Sub
    24. 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, VB6, Photoshop...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.