Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › VB 2008 nub needs help!!!
New Posts  All Forums:Forum Nav:

VB 2008 nub needs help!!!

post #1 of 6
Thread Starter 
Ok so I am taking intro to VB and have to make a very simple calculator (see attachment) but I have been given no instructions and don't understand it at all. Basically you input a number in "first number" and "second number then choose the operation and on the bottom it displays the formula and answer. Also if you change either number in the text box's the formula is supposed to go away. All I have been able to figure out is how to make it look like it is supposed to but it has no code atm. Please help I just cant figure this out.
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
post #2 of 6
Take the numbers from both the textboxes, then put them into integers and have the sum, product, etc. put into a sum variable then just output a concatenated string using the three variables into the textbox for the formula.

What you what to make the calculation is a Event Handler for the the button click.

Below is an example of the button click handler for the addition button. I encourage you not to just copy and paste but to write your own. I have written some comments so you know what each line is doing.

Code:
'Addition Button Handler
    Private Sub AdditionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdditionButton.Click
        'Declare Variable for First Number and take what is in The First Number TextBox 
        'and Parse it into an Integer Value and place it into the variable FirstNumber
        Dim FirstNumber As Integer = Integer.Parse(FirstNumberTextBox.Text)
        'Same as the First Number but now for the second
        Dim SecondNumber As Integer = Integer.Parse(SecondNumberTextBox.Text)
        Dim Sum As Integer  'Declare a variable for the Sum

        Sum = FirstNumber + SecondNumber    'Add the First and Second Number and put it into the Sum variable

        FormulaTextBox.Text = (FirstNumber & " + " & SecondNumber & " = " & Sum).ToString()
        'Output into the Formula TextBox the concatenated string of The FirstNumber with a + then the second 
        'Number and an equals sign then the sum
    End Sub
As for the textbox containing the formula to be cleared when the text is changed, you will want to have a TextChanged event handler for either each textbox or just one that handles both textboxes that clears the text in the Formula TextBox
Edited by -Inferno - 6/6/11 at 10:16pm
post #3 of 6
If you can't figure it out, or need some help I have included all the code that you need to make it work. I hope you give it a try on your own before looking at it. You won't learn unless you do it yourself.

Good Luck, hope it helps.



Code:
Public Class SimpleCalc
    Private Sub AdditionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdditionButton.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstNumberTextBox.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondNumberTextBox.Text)
        Dim Sum As Integer

        Sum = FirstNumber + SecondNumber

        FormulaTextBox.Text = (FirstNumber & " + " & SecondNumber & " = " & Sum).toString()
    End Sub

    Private Sub SubtractionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubtractionButton.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstNumberTextBox.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondNumberTextBox.Text)
        Dim Sum As Integer

        Sum = FirstNumber - SecondNumber

        FormulaTextBox.Text = (FirstNumber & " - " & SecondNumber & " = " & Sum).ToString()
    End Sub

    Private Sub MultiplicationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiplicationButton.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstNumberTextBox.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondNumberTextBox.Text)
        Dim Sum As Integer

        Sum = FirstNumber * SecondNumber

        FormulaTextBox.Text = (FirstNumber & " x " & SecondNumber & " = " & Sum).ToString()
    End Sub

    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstNumberTextBox.TextChanged, SecondNumberTextBox.TextChanged
        FormulaTextBox.Clear()
    End Sub
End Class

Edited by -Inferno - 6/6/11 at 10:17pm
post #4 of 6
Thread Starter 
Thank you so much! that is exactly what I need, something to look at so that I can figure out how it is working
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
post #5 of 6
Thread Starter 
Ok so here is the code that I am currently using. It runs great with no problems. A very big thanks!!!

Code:
Public Class Form1



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstN.TextChanged
        Output.Text = ""


    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstN.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondN.Text)
        Dim Sum As Integer

        Sum = FirstNumber + SecondNumber

        Output.Text = (FirstNumber & " + " & SecondNumber & " = " & Sum).ToString()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstN.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondN.Text)
        Dim Sum As Integer

        Sum = FirstNumber - SecondNumber

        Output.Text = (FirstNumber & " - " & SecondNumber & " = " & Sum).ToString()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim FirstNumber As Integer = Integer.Parse(FirstN.Text)
        Dim SecondNumber As Integer = Integer.Parse(SecondN.Text)
        Dim Sum As Integer

        Sum = FirstNumber / SecondNumber

        Output.Text = (FirstNumber & " / " & SecondNumber & " = " & Sum).ToString()

    End Sub

    Private Sub SecondN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SecondN.TextChanged
        Output.Text = ""
    End Sub
End Class
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
Black beauty
(15 items)
 
  
CPUMotherboardGraphicsRAM
5820k asus x99-a Gtx 770 G.skill ripjaws 4 
Hard DriveHard DriveHard DriveCooling
samsung 840 evo  WD black WD black Custom water 320 rad 
OSMonitorKeyboardPower
Windows 7 Asus 21.5 led x3 Logitech g110 Corsair 850TX 
CaseMouseMouse Pad
technofront HWD Steelseries Xai WOWPAD 
  hide details  
Reply
post #6 of 6
Looks good.

Just a suggestion, you should make your whole GUI first and name everything with a meaningful name. Then go onto the code, I see in your code you have like TextBox1 and Button3, etc. It's hard to know what things are. Also try to remove code when it is not being used like the first event handler.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › VB 2008 nub needs help!!!