|
-
Jan 3rd, 2009, 08:34 AM
#1
Thread Starter
New Member
[2008] cant do a calaculation unless all my checkboxes are ticked
Hi there my program is a simple adding calculation.
I have some products that when ticked ask for a input box that ask for the quanity of the type needed.
The user then selects the calculate button to calculate the price of the selections
My program works fine when every item is selected (via tick boxes)
but crashes with the error "Conversion from string "" to type 'Double' is not valid. " when a selection is not ticked here is my code
i have been told that my conversion procedure is outdated but this is what my lecturer has taught us and his marking requirments need to keep using this old method
Code:
Public Class Form1
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
'declare variables
Dim network As Double
Dim network_num As Double
Dim network_total As Double
'Make Text boxes Visable when ticked
textbox_network_no.Visible = True
TextBox_netcost.Visible = True
TextBox_networksub.Visible = True
'Error trapping
Try
'Inout box displayed to ask how much network cards are needed
network = InputBox("How Many 100Mbit Network Cards Would You Like?")
Catch ex As Exception
'simple message
MsgBox("sorry could not accept the input")
'repeat error message
network = InputBox("How Many 100Mbit Network Cards Would You Like?")
End Try
'set network_num to 35
network_num = CDbl(35)
'display network num in textbox
TextBox_netcost.Text = CDbl(network_num)
'display ammount ordered in textbox
textbox_network_no.Text = CDbl(network)
'calculation for network_total
network_total = network * network_num
TextBox_networksub.Text = CDbl(network_total)
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
Dim hdd As Double
Dim hdd_num As Double
Dim hdd_total As Double
'Make Text boxes Visable when ticked
textbox_hdd_no.Visible = True
TextBox_hddcost.Visible = True
TextBox_hddsub.Visible = True
'Error trapping
Try
'Inout box displayed to ask how much network cards are needed
hdd = InputBox("How Many Hard Drives Would You Like?")
Catch ex As Exception
'simple message
MsgBox("sorry could not accept the input")
'repeat error message
hdd = InputBox("How Many Hard Drives Would You Like?")
End Try
'set hdd_num to 120
hdd_num = CDbl(120)
'display hdd num in textbox
TextBox_hddcost.Text = CDbl(hdd_num)
'display ammount ordered in textbox
textbox_hdd_no.Text = CDbl(hdd)
'calculation for hdd_total
hdd_total = hdd * hdd_num
TextBox_hddsub.Text = CDbl(hdd_total)
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
Dim MOT As Double
Dim MOT_num As Double
Dim MOT_total As Double
'Make Text boxes Visable when ticked
textbox_MOT_no.Visible = True
TextBox_MOTcost.Visible = True
TextBox_MOTsub.Visible = True
'Error trapping
Try
'Inout box displayed to ask how much MOT are needed
MOT = InputBox("How MOT Health check Would You Like?")
Catch ex As Exception
'simple message
MsgBox("sorry could not accept the input")
'repeat error message
MOT = InputBox("How MOT Health check Would You Like?")
End Try
'set MOT_num to 39
MOT_num = CDbl(39)
'display MOT num in textbox
TextBox_MOTcost.Text = CDbl(MOT_num)
'display ammount ordered in textbox
textbox_MOT_no.Text = CDbl(MOT)
'calculation for MOT_total
MOT_total = MOT * MOT_num
TextBox_MOTsub.Text = CDbl(MOT_total)
End Sub
Private Sub Btn_calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_calculate.Click
Dim MOT_total As Double
Dim hdd_total As Double
Dim network_total As Double
Dim total As Double
MOT_total = CDbl(TextBox_MOTsub.Text)
hdd_total = CDbl(TextBox_hddsub.Text)
network_total = CDbl(TextBox_networksub.Text)
total = MOT_total + hdd_total + network_total
TextBox_total.Text = CDbl(total)
End Sub
End Class
thank you.
Im new to VB so id imagine ther are 1000000 mistakes
-
Jan 3rd, 2009, 08:50 AM
#2
Re: [2008] cant do a calaculation unless all my checkboxes are ticked
is this http://social.msdn.microsoft.com/For...1-247f7215039c the same question?
you should inform your instructor that they need to teach current practices. have your instructor sign up here and we will explain why.
-
Jan 3rd, 2009, 08:59 AM
#3
Thread Starter
New Member
Re: [2008] cant do a calaculation unless all my checkboxes are ticked
yup that is me.
Ethier way i cant even get that method working ethier!
is there another solution then using the tickboxes? becuase the program actualy works but only when all my tick boxes are ticked
-
Jan 3rd, 2009, 09:42 AM
#4
Re: [2008] cant do a calaculation unless all my checkboxes are ticked
Your calculation assumes that all your TextBoxes contain a value that is a valid Double, but some TextBoxes will be empty if some CheckBoxes are not checked. That is exactly why you should not be doing things like this:
Code:
MOT_total = CDbl(TextBox_MOTsub.Text)
hdd_total = CDbl(TextBox_hddsub.Text)
network_total = CDbl(TextBox_networksub.Text)
That is just assuming that all those Text properties can be converted to Double, which is not always the case. You should be using Double.TryParse to convert strings that you aren't 100% sure can be converted. Either that or don't enable your Calculate Button until you ARE 100% sure the input is all valid.
This is the sort of thing your teacher should be teaching you.
-
Jan 3rd, 2009, 09:45 AM
#5
Thread Starter
New Member
Re: [2008] cant do a calaculation unless all my checkboxes are ticked
 Originally Posted by jmcilhinney
Your calculation assumes that all your TextBoxes contain a value that is a valid Double, but some TextBoxes will be empty if some CheckBoxes are not checked. That is exactly why you should not be doing things like this:
Code:
MOT_total = CDbl(TextBox_MOTsub.Text)
hdd_total = CDbl(TextBox_hddsub.Text)
network_total = CDbl(TextBox_networksub.Text)
That is just assuming that all those Text properties can be converted to Double, which is not always the case. You should be using Double.TryParse to convert strings that you aren't 100% sure can be converted. Either that or don't enable your Calculate Button until you ARE 100% sure the input is all valid.
This is the sort of thing your teacher should be teaching you.
arghh ok that makes alot more sense then.
is the syntax of tryphase the same as using cdbl syntax? i tried it out but my syntax was wrong
-
Jan 3rd, 2009, 11:31 AM
#6
Re: [2008] cant do a calaculation unless all my checkboxes are ticked
examples were given at your MSDN link that showed how to use TryParse. i think i also made the suggestion that you use Decimal instead of Double for currency type applications. if you aren't reading responses to your post don't expect people to keep repeating themselves.
another thing, this looks like a mis-use of checkboxes, you aren't checking to see if they are checked or not. a common use of checkboxes is a non-exclusive list of options.
if this is really the kind of thing you are being taught i feel sorry for you.
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
|