10 Replies - 3639 Views - Last Post: 02 February 2012 - 01:35 PM Rate Topic: -----

#1 sheepboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-January 12

Problem with calculating in VB

Posted 24 January 2012 - 07:27 AM

Hi everyone, i am having some trouble, I have several buttons in my form and they each need to add, subtract, divide and multiply certain values together but i seem to be having problems with this. I didnt expect anything to go wrong and its probably a rookie mistake, but take my first calculation for example; a user must enter a value in a textbox (txtDays) and and also when one of my various radio buttons are pressed a value shows in a label (lblDailyHireCharge). I now need a way of multiplying these values to give me my answer shown in a label. I have done this by adding a button when which pressed multiplies the txtDays value with the lblDailyHireCharge and then shows it in the label, however it keeps on showing "0" when i click it. Below is my code, i am basically saying that the value of both of those figures is intDuration and that lblDuration is where i want it to go when the button is clicked. Thankyou for any help, i have a few of these similar calculations i need to solve but i can't unless i can figure out what im doing wrong here :/

Private Sub btnDuration_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDuration.Click

        Dim intdays As Integer
        Dim intDailyHireCharge As Integer
        Dim intDuration As Integer
        

        intdays = Val(txtDays.Text)
        intDailyHireCharge = Val(lblDailyHireCharge.Text)

        intDuration = (txtDays.Text * lblDailyHireCharge.Text)

        lblDuration.Text = intDuration



    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with calculating in VB

#2 ash.burlaczenko  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 4
  • Joined: 24-January 12

Re: Problem with calculating in VB

Posted 24 January 2012 - 07:59 AM

Maybe this line

intDuration = (txtDays.Text * lblDailyHireCharge.Text)



should use the variables you created

intDuration = (intDays * intDailyHireCharge)



Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Problem with calculating in VB

Posted 24 January 2012 - 08:21 AM

I tried out your little program using your own code but it works fine. I think the problem is with how you're assigning the value associated with the radio button to the lblDailyHireCharge label. how are you doing that? provide the code.

This is how I did btw..

the UI - I just put together the controls how you have mentioned in the question

Posted Image

the code -


Public Class Form1

    Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click

        lblDailyHireCharge.Text = RadioButton1.Text

    End Sub

    Private Sub RadioButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.Click

        lblDailyHireCharge.Text = RadioButton2.Text

    End Sub

    Private Sub RadioButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.Click

        lblDailyHireCharge.Text = RadioButton3.Text

    End Sub

    Private Sub btnDuration_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDuration.Click

        Dim intdays As Integer
        Dim intDailyHireCharge As Integer
        Dim intDuration As Integer


        intdays = Val(txtDays.Text)
        intDailyHireCharge = Val(lblDailyHireCharge.Text)

        intDuration = (txtDays.Text) * (lblDailyHireCharge.Text)

        lblDuration.Text = intDuration

    End Sub
End Class

This post has been edited by nK0de: 24 January 2012 - 08:22 AM

Was This Post Helpful? 1
  • +
  • -

#4 sheepboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-January 12

Re: Problem with calculating in VB

Posted 24 January 2012 - 08:49 AM

Thanks for the replies! The code worked for the first set of radio buttons (im using 8) but when i get to my second set of radio buttons or even if I just click the button without selecting anything it gives me the following error; "conversion from string "45" to type 'double' is not valid" It works fine for the first 4 radio buttons though so I don't understand why its messing up. I've never had this error before. The code below is the code for my radio buttons, the purpose of my form is for me to be able to work out prices for a car letting service and this calculation in particular will allow me to times the number of days by the daily charge for the duration charge. As you can see the different radio buttons refer to the type of car, the first set is for the UK and the second is for cars in Ireland (hence the currency difference).

'When a radio button is checked and the button is pressed the values will be shown
        If rb1.Checked = True Then lblDailyHireCharge.Text = "29"
        If rb2.Checked = True Then lblDailyHireCharge.Text = "43"
        If rb3.Checked = True Then lblDailyHireCharge.Text = "39"
        If rb4.Checked = True Then lblDailyHireCharge.Text = "80"

        'The Eire values will also be shown if radio buttons from the Eire groupbox are clicked
        If rb5.Checked = True Then lblDailyHireCharge.Text = "45"
        If rb6.Checked = True Then lblDailyHireCharge.Text = "64"
        If rb7.Checked = True Then lblDailyHireCharge.Text = "59"
        If rb8.Checked = True Then lblDailyHireCharge.Text = "120"

Was This Post Helpful? 0
  • +
  • -

#5 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Problem with calculating in VB

Posted 24 January 2012 - 09:02 AM

You're getting that error because you're trying to do calculations with a string, not a number. You have to remove the currency symbol. Just keep the currency value only.

£29 - since the currency symbol is not a number, the compiler take the whole thing as a string. just remove it and use only 29

This post has been edited by nK0de: 24 January 2012 - 09:12 AM

Was This Post Helpful? 1
  • +
  • -

#6 sheepboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-January 12

Re: Problem with calculating in VB

Posted 24 January 2012 - 09:15 AM

Ahh that worked than you very much! :) For some reason my next calculation wont display at all though :S The code im trying to manipulate is as follows:

I need to display 5% of the duration charge and my first thoughts was this;

If cbDiscounts.Checked Then lblDiscounts.Text = lblDuration.Text * 0.05 / 100

Was This Post Helpful? 0
  • +
  • -

#7 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Problem with calculating in VB

Posted 24 January 2012 - 09:35 AM

no problem.
Well, there's nothing wrong with it. The problem must be with where you have put that code line.. post the whole code block for the button click event.
Was This Post Helpful? 1
  • +
  • -

#8 sheepboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-January 12

Re: Problem with calculating in VB

Posted 24 January 2012 - 09:36 AM

Actually it was my fault its seemed to sort itself out now i shall test it and hopefully that will be it for that, ill post back with the results now :)
Was This Post Helpful? 0
  • +
  • -

#9 sheepboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-January 12

Re: Problem with calculating in VB

Posted 24 January 2012 - 09:56 AM

It all works thanks for all the help! The only improvement I could make is making the total at the end a few less decimal places because for example one of my results is 153.3453.3454

preferably I would like this to be only to two decimal places, so 153.34

Im trying to find the solution everywhere but when i have this hopefully i am all done :P
Was This Post Helpful? 0
  • +
  • -

#10 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 284
  • Joined: 22-August 11

Re: Problem with calculating in VB

Posted 02 February 2012 - 01:21 PM

Couldn't you do something like

lblDuration.Text = intDuration.tostring("c2")



c2 sets it to currency with 2 decimal places
Was This Post Helpful? 0
  • +
  • -

#11 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Problem with calculating in VB

Posted 02 February 2012 - 01:35 PM

Don't forget to properly convert your values to the proper data types.

Preferable methods include the .TryParse function, and Parse to properly cast them.

This post has been edited by trevster344: 02 February 2012 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1