English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This is some of my code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'clears everything
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox3.Text = TextBox1.Text * TextBox2.Text 'subtotal
If TextBox4.Text > 0 Then 'if the check box is checked adds electricity
TextBox5.Text = TextBox3.Text + TextBox4.Text
Else : TextBox5.Text = TextBox3.Text 'if not checked it skips electricity
End If
End Sub

This is the error
Conversion from string "" to type 'Double' is not valid
TextBox3.Text = TextBox1.Text * TextBox2.Text 'subtotal

What do I need to change?

Also what is a quick and easy way to display a textbox as currency like this: XX,XXX.XX

Last question how do I write the code to clear the checkbox for a reset button? I tried the ""

2007-10-13 12:21:19 · 4 answers · asked by Josh 1 in Computers & Internet Programming & Design

4 answers

The problem in the first part of the question is that you are trying to multiply and add strings as if they are numbers. You should "massage" the string to remove commas, dollar signs, etc. and convert the strings to doubles before performing the math.

To answer the next part use
Format (strVariable, "currency")

strVariable being the name of the variable you wish to display as currency.

For the last part, use Checkbox.Value

2007-10-13 12:27:45 · answer #1 · answered by Daniel T 5 · 0 0

I would copy converted textbox strings into variables with meaningful names. You have already made a Subrountine which initializes textboxes. Why not make a SUB which will read the textboxes, do the conversions and load variables


Private Sub ReadUserINputs()
myInteger = Cint(TextBox1.Text)
myDouble = Cdbl(TextBox2.Text)
myBoolean = Cbool(TextBox3.Text)
dblKWRate = Cdbl(TextBox4.Text)
dblKWused= Cdbl(TextBox5.Text)
End sub

Now when you click button 1 you fist call the ReadUserInputs to update the variables then process your equations

Dim dblAnswer as Double

ReadUserInputs
dblAnswer = dblKWRate * dblKWused

To display your answer as currency the easiest way is to use the format command

Me.TextBox1.Text = Format(answer, "Currency")

This uses the predefined currency format and its ouput is defined by the computers system settings. You can also use a custom format
Me.TextBox1.Text = Format(answer, "$#,##0.00;($#,##0.00)")


Use the checked property of a checkbox to either set or clear a check
Me.CheckBox1.Checked = False ' clear the check

2007-10-13 15:03:36 · answer #2 · answered by MarkG 7 · 0 0

TextBox4.Text isn't a number, that is yor whole problem here. What you need to do is find the conversion function that takes a string and returns 'Double'. The function declaration would look something like this, Double convertDoulbe(String). Look it up in your reference, I am not sure about the name here.

Good Luck, and happy coding.

2007-10-13 12:30:07 · answer #3 · answered by Anonymous · 0 0

?

2007-10-13 12:23:51 · answer #4 · answered by ***KREEM YUNG*** 1 · 0 1

fedest.com, questions and answers