4 Replies - 712 Views - Last Post: 22 February 2016 - 04:36 AM Rate Topic: -----

#1 veridiux   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 16

Could use some help with a calculator project.

Posted 21 February 2016 - 03:51 PM

Alright, so I'm taking a VB class in school and was assigned a small calculator. I'm only a few weeks into my programming class so I'm sure there will be a ton of non-efficient code and errors. Alright so the first problem is the calculator crashes when it stores too much in the lblDisplay_mini. The error is "Conversion from string "" to type 'Double' is not valid." The second problem is I'm trying to take the lblDisplay_mini and add it to the main Display.Text. Here is an example of what I'm talking about. I would like to be able to type 89 + 98 - 37 and it to be able to store the data. Right now when I try something like that it only uses the last value.

I hope I wrote this in a non-confusing way, I'm very new to programming and just not sure what I'm doing or what to research to get the result I'm after.

Option Explicit On
Option Infer Off
Option Strict On


Public Class Advanced_Calculator
    'Declare
    Dim n1 As Double
    Dim n2 As Double
    Dim n3 As Double
    'Dim n4 As Double  Come back and look at this...
    Dim n5 As Int32
    Dim op As String
    Dim result As Double


    'This section sends all numeric clicks to the lblDisplay label
    Private Sub btnNum__Click(sender As Object, e As EventArgs) Handles btnNum_7.Click, btnNum_9.Click, btnNum_8.Click, btnNum_6.Click, btnNum_5.Click, btnNum_4.Click, btnNum_3.Click, btnNum_2.Click, btnNum_1.Click, btnNum_0.Click
        Dim b_num As Button = CType(sender, Button)
        If lblDisplay.Text = "0" Then
            lblDisplay.Text = b_num.Text
        Else
            lblDisplay.Text = lblDisplay.Text + b_num.Text
        End If
    End Sub
    'Clear button
    Private Sub btn_C_Click(sender As Object, e As EventArgs) Handles btn_C.Click
        lblDisplay.Text = "0"
        lblDisplay_mini.Text = ""
    End Sub
    'Clear entry button "needs work"
    Private Sub btn_Ce_Click(sender As Object, e As EventArgs) Handles btn_Ce.Click
        lblDisplay.Text = "0"
        lblDisplay_mini.Text = ""
    End Sub
    'Controls the decimal point
    Private Sub btn_Decimal_Click(sender As Object, e As EventArgs) Handles btn_Decimal.Click
        If Not (lblDisplay.Text.Contains(".")) Then
            lblDisplay.Text += "."
        End If
    End Sub

    'This section sends all the clicks from the mathematical symbols to the lblDisplay label
    Private Sub btn_math_Click(sender As Object, e As EventArgs) Handles btn_Add.Click, btn_Subtract.Click, btn_Multiply.Click, btn_Mod.Click, btn_Exp.Click, btn_Divide.Click
        Dim b_op As Button = CType(sender, Button)
        n2 = CDbl(lblDisplay.Text)
        lblDisplay_mini.Text += lblDisplay.Text
        lblDisplay.Text = ""
        op = b_op.Text
        lblDisplay_mini.Text = lblDisplay_mini.Text + " " + op
    End Sub


    'This section controls the equal button and all the mathematical symbols
    Private Sub btn_Equal_Click(sender As Object, e As EventArgs) Handles btn_Equal.Click
        n3 = CDbl(lblDisplay.Text)
        If op = "+" Then
            n1 = n2 + n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        ElseIf op = "-" Then
            n1 = n2 - n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        ElseIf op = "" Then
            n1 = n2 * n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        ElseIf op = "" Then
            n1 = n2 / n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        ElseIf op = "Mod" Then
            n1 = n2 Mod n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        ElseIf op = "Exp" Then
            n1 = n2 ^ n3
            lblDisplay.Text = CType(n1, String)
            lblDisplay_mini.Text = ""
        End If
    End Sub

    'This section is to convert numbers to binary
    Private Sub btn_Bin_Click(sender As Object, e As EventArgs) Handles btn_Bin.Click
        If Int32.TryParse(lblDisplay.Text, n5) Then
            lblDisplay.Text = Convert.ToString(n5, 2)
        Else
            lblDisplay.Text = ""
        End If
    End Sub

    'Presets for calculator inital startup
    Private Sub Advanced_Calculator_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.Height = 320
        Me.Width = 268
        lblDisplay.Height = 54
        lblDisplay.Width = 227
        'Loads the cBox_main items on startup
        cBox_main.Items.Add("Pressure Conversion")
        cBox_main.Items.Add("Temperature Conversion")
        cBox_main.Items.Add("Length Conversion")
        cBox_main.Items.Add("Weight Conversion")
        cBox_main.Items.Add("Volume Conversion")


    End Sub
    'Presets for standard under the view menu
    Private Sub StandardToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StandardToolStripMenuItem.Click
        Me.Height = 320
        Me.Width = 268
        lblDisplay.Height = 54
        lblDisplay.Width = 227
    End Sub
    'Presets for scientific under the view menu
    Private Sub ScientificToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ScientificToolStripMenuItem.Click
        Me.Height = 320
        Me.Width = 397
        lblDisplay.Height = 54
        lblDisplay.Width = 227
    End Sub
    'Presets for unit conversion under the view menu
    Private Sub UnitConversionToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnitConversionToolStripMenuItem.Click
        Me.Height = 426
        Me.Width = 397
        lblDisplay.Height = 54
        lblDisplay.Width = 227
    End Sub
    'This section shows the items generated and cleared for cBox_choice
    Private Sub cBox_main_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cBox_main.SelectedIndexChanged
        If cBox_main.Text = ("Pressure Conversion") Then
            cBox_choice.Items.Clear()
            cBox_choice.Items.Add("Bar to Kg/cm")
            cBox_choice.Items.Add("Bar to Psi")
            cBox_choice.Items.Add("KG/cm to Bar")
            cBox_choice.Items.Add("KG/cm to Psi")
            cBox_choice.Items.Add("Psi to Bar")
            cBox_choice.Items.Add("Psi to KG/cm")
        ElseIf cBox_main.Text = ("Temperature Conversion") Then
            cBox_choice.Items.Clear()
            cBox_choice.Items.Add("Fahrenheit to Celsius")
            cBox_choice.Items.Add("Celsius to Fahrenheit")
        ElseIf cBox_main.Text = ("Length Conversion") Then
            cBox_choice.Items.Clear()
            'Millimeters to
            cBox_choice.Items.Add("Millimeters to Centimeters")
            cBox_choice.Items.Add("Millimeters to Miles")
            cBox_choice.Items.Add("Millimeters to Inches")
            cBox_choice.Items.Add("Millimeters to Meter")
            cBox_choice.Items.Add("Millimeters to Kilometer")
            cBox_choice.Items.Add("Millimeters to Feet")
            cBox_choice.Items.Add("Millimeters to Yards")
            'Centimeters to
            cBox_choice.Items.Add("Centimeters to Millimeters")
            cBox_choice.Items.Add("Centimeters to Miles")
            cBox_choice.Items.Add("Centimeters to Inches")
            cBox_choice.Items.Add("Centimeters to Meter")
            cBox_choice.Items.Add("Centimeters to Kilometers")
            cBox_choice.Items.Add("Centimeters to Feet")
            cBox_choice.Items.Add("Centimeters to Yards")
            'Miles to
            cBox_choice.Items.Add("Miles to Millimeters")
            cBox_choice.Items.Add("Miles to Centimeters")
            cBox_choice.Items.Add("Miles to Inches")
            cBox_choice.Items.Add("Miles to Meter")
            cBox_choice.Items.Add("Miles to Kilometers")
            cBox_choice.Items.Add("Miles to Feet")
            cBox_choice.Items.Add("Miles to Yards")
            'Inches to
            cBox_choice.Items.Add("Inches to Millimeters")
            cBox_choice.Items.Add("Inches to Centimeters")
            cBox_choice.Items.Add("Inches to Miles")
            cBox_choice.Items.Add("Inches to Meter")
            cBox_choice.Items.Add("Inches to Kilometers")
            cBox_choice.Items.Add("Inches to Feet")
            cBox_choice.Items.Add("Inches to Yards")
            'Meter to
            cBox_choice.Items.Add("Meter to Millimeters")
            cBox_choice.Items.Add("Meter to Centimeters")
            cBox_choice.Items.Add("Meter to Miles")
            cBox_choice.Items.Add("Meter to Inches")
            cBox_choice.Items.Add("Meter to Kilometers")
            cBox_choice.Items.Add("Meter to Feet")
            cBox_choice.Items.Add("Meter to Yards")
            'Kilometers to
            cBox_choice.Items.Add("Kilometers to Millimeters")
            cBox_choice.Items.Add("Kilometers to Centimeters")
            cBox_choice.Items.Add("Kilometers to Miles")
            cBox_choice.Items.Add("Kilometers to Inches")
            cBox_choice.Items.Add("Kilometers to Meter")
            cBox_choice.Items.Add("Kilometers to Feet")
            cBox_choice.Items.Add("Kilometers to Yards")
            'Feet to
            cBox_choice.Items.Add("Feet to Millimeters")
            cBox_choice.Items.Add("Feet to Centimeters")
            cBox_choice.Items.Add("Feet to Miles")
            cBox_choice.Items.Add("Feet to Inches")
            cBox_choice.Items.Add("Feet to Meter")
            cBox_choice.Items.Add("Feet to Kilometers")
            cBox_choice.Items.Add("Feet to Yards")
            'Yards to
            cBox_choice.Items.Add("Yards to Millimeters")
            cBox_choice.Items.Add("Yards to Centimeters")
            cBox_choice.Items.Add("Yards to Miles")
            cBox_choice.Items.Add("Yards to Inches")
            cBox_choice.Items.Add("Yards to Meter")
            cBox_choice.Items.Add("Yards to Kilometers")
            cBox_choice.Items.Add("Yards to Feet")
        ElseIf cBox_main.Text = ("Weight Conversion") Then
            cBox_choice.Items.Clear()
            'Metric Tons to
            cBox_choice.Items.Add("Metric Tons to Kilograms")
            cBox_choice.Items.Add("Metric Tons to Pounds")
            cBox_choice.Items.Add("Metric Tons to Oz")
            cBox_choice.Items.Add("Metric Tons to Grams")
            cBox_choice.Items.Add("Metric Tons to Tons")
            'Kilograms to
            cBox_choice.Items.Add("Kilograms to Metric Tons")
            cBox_choice.Items.Add("Kilograms to Pounds")
            cBox_choice.Items.Add("Kilograms to Oz")
            cBox_choice.Items.Add("Kilograms to Grams")
            cBox_choice.Items.Add("Kilograms to Tons")
            'Pounds to
            cBox_choice.Items.Add("Pounds to Metric Tons")
            cBox_choice.Items.Add("Pounds to Kilograms")
            cBox_choice.Items.Add("Pounds to Oz")
            cBox_choice.Items.Add("Pounds to Grams")
            cBox_choice.Items.Add("Pounds to Tons")
            'Oz to
            cBox_choice.Items.Add("Oz to Metric Tons")
            cBox_choice.Items.Add("Oz to Kilograms")
            cBox_choice.Items.Add("Oz to Pounds")
            cBox_choice.Items.Add("Oz to Grams")
            cBox_choice.Items.Add("Oz to Tons")
            'Grams to
            cBox_choice.Items.Add("Grams to Metric Tons")
            cBox_choice.Items.Add("Grams to Kilograms")
            cBox_choice.Items.Add("Grams to Pounds")
            cBox_choice.Items.Add("Grams to Oz")
            cBox_choice.Items.Add("Grams to Tons")
            'Tons to
            cBox_choice.Items.Add("Tons to metric Tons")
            cBox_choice.Items.Add("Tons to Kilograms")
            cBox_choice.Items.Add("Tons to Pounds")
            cBox_choice.Items.Add("Tons to Oz")
            cBox_choice.Items.Add("Tons to Grams")
        ElseIf cBox_main.Text = ("Volume Conversion") Then
            cBox_choice.Items.Clear()
            'Milliliters to
            cBox_choice.Items.Add("Milliliters to Liters")
            cBox_choice.Items.Add("Milliliters to Fluid Ounces")
            cBox_choice.Items.Add("Milliliters to Pints")
            cBox_choice.Items.Add("Milliliters to Cups")
            cBox_choice.Items.Add("Milliliters to Quarts")
            cBox_choice.Items.Add("Milliliters to Gallons")
            'Liters to
            cBox_choice.Items.Add("Liters to Milliliters")
            cBox_choice.Items.Add("Liters to Fluid Ounces")
            cBox_choice.Items.Add("Liters to Pints")
            cBox_choice.Items.Add("Liters to Cups")
            cBox_choice.Items.Add("Liters to Quarts")
            cBox_choice.Items.Add("Liters to Gallons")
            'Fluid Ounces to
            cBox_choice.Items.Add("Fluid Ounces to Milliliters")
            cBox_choice.Items.Add("Fluid Ounces to Liters")
            cBox_choice.Items.Add("Fluid Ounces to Pints")
            cBox_choice.Items.Add("Fluid Ounces to Cups")
            cBox_choice.Items.Add("Fluid Ounces to Quarts")
            cBox_choice.Items.Add("Fluid Ounces to Gallons")
            'Pints to
            cBox_choice.Items.Add("Pints to Milliliters")
            cBox_choice.Items.Add("Pints to Liters")
            cBox_choice.Items.Add("Pints to Fluid Ounces")
            cBox_choice.Items.Add("Pints to Cups")
            cBox_choice.Items.Add("Pints to Quarts")
            cBox_choice.Items.Add("Pints to Gallons")
            'Cups to
            cBox_choice.Items.Add("Cups to Milliliters")
            cBox_choice.Items.Add("Cups to Liters")
            cBox_choice.Items.Add("Cups to Fluid Ounces")
            cBox_choice.Items.Add("Cups to Pints")
            cBox_choice.Items.Add("Cups to Quarts")
            cBox_choice.Items.Add("Cups to Gallons")
            'Quarts to
            cBox_choice.Items.Add("Quarts to Milliliters")
            cBox_choice.Items.Add("Quarts to Liters")
            cBox_choice.Items.Add("Quarts to Fluid Ounces")
            cBox_choice.Items.Add("Quarts to Pints")
            cBox_choice.Items.Add("Quarts to Cups")
            cBox_choice.Items.Add("Quarts to Gallons")
            'gallons to
            cBox_choice.Items.Add("Gallons to Milliliters")
            cBox_choice.Items.Add("Gallons to Liters")
            cBox_choice.Items.Add("Gallons to Fluid Ounces")
            cBox_choice.Items.Add("Gallons to Pints")
            cBox_choice.Items.Add("Gallons to Cups")
            cBox_choice.Items.Add("Gallons to Quarts")
        End If
    End Sub
    'this section controls the calculate button in the measurement section
    Private Sub btnMeasure_calc_Click(sender As Object, e As EventArgs) Handles btnMeasure_calc.Click
        Dim convert As Double
        If cBox_choice.Text = "Fahrenheit to Celsius" Then
            convert = 5 / 9 * (CInt(txtMeasure_input.Text) - 32)
            lblMeasure_output.Text = convert & " Celsius"
        ElseIf cBox_choice.Text = "Celsius to Fahrenheit" Then
            convert = (9 / 5 * CDbl(txtMeasure_input.Text)) + 32
            lblMeasure_output.Text = (convert) & " Fahrenheit"
        End If
    End Sub
    'This section is used to clear the measurement output and input screens
    Private Sub btnMeasure_clear_Click(sender As Object, e As EventArgs) Handles btnMeasure_clear.Click
        txtMeasure_input.Text = ""
        lblMeasure_output.Text = ""
    End Sub


End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Could use some help with a calculator project.

#2 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 241
  • Joined: 13-November 13

Re: Could use some help with a calculator project.

Posted 21 February 2016 - 11:59 PM

Why don't you just type calculator vb.net into the search field at the top of the page and study some of the search results? There are many different solutions and you will learn a lot.
Was This Post Helpful? 0
  • +
  • -

#3 PeterH   User is offline

  • D.I.C Regular

Reputation: 60
  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: Could use some help with a calculator project.

Posted 22 February 2016 - 03:50 AM

The message suggests you are trying to convert a string value to a double, but that is failing. You should be running this code under the debugger, and this will tell you exactly are you getting this message, and at what line?, if not, see the debugging tutorial by Andrewsw. My guess is you are attempting to convert the input e.g. 10 + 20 + 30 from the lblDisplay.Text to a double, which will not work, this will need to split into its component parts first.
Was This Post Helpful? 1
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: Could use some help with a calculator project.

Posted 22 February 2016 - 04:36 AM

"Conversion from string "" to type 'Double' is not valid."

This error indicates that you are trying to convert an empty String "" to a Double type number. This error is caused by using the CDbl() or Convert.ToDouble functions on a String or Text property of a control which is a String type too, when there is nothing in it.

I would take PeterH`s tip and learn to debug your code. There is a link below to andrewsw`s debugging tutorial.

Debugging Express
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Could use some help with a calculator project.

Posted 22 February 2016 - 04:36 AM

The debugging tutorial mentioned is (Ed: also) linked in my signature below. Take this and you will be able to step through your code and discover what is happening. This is especially important if you haven't been running and testing your code as you progressed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1