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

I need help with this program:
design five checkboxes with the names Check1, Check2,...
and when clicked let a textbox display $1 for Check1, $2 for Check and so on. Then use a command button and when clicked add up all the values of the checked and display it in Text2.text
* I got everything except the adding up part. I dont know what the code would be for it to add up the checkboxes clicked. Pls i really need help with that part. 10 pts for a good answer

2007-02-19 03:23:55 · 4 answers · asked by Kino K 1 in Computers & Internet Programming & Design

4 answers

I'm not very familiar with the VB syntax and i don't have it installed on my computer, but it should go something like this:



dim sum as integer
sum = 0

if Check1.Checked then
sum = sum + 1
end if

if Check2.Checked then
sum = sum + 2
end if

if Check3.Checked then
sum = sum + 3
end if

if Check4.Checked then
sum = sum + 4
end if

if Check5.Checked then
sum = sum + 5
end if

Text2.text = sum

2007-02-19 03:42:11 · answer #1 · answered by void7x 2 · 0 0

What you need to do is write a routine that will look for the value of Check1.Checked, Check2.Checked, etc...

Something like this:

Dim sngTotal As Single = 0

If Check1.Checked then
sngTotal = sngTotal + CSng(Check1.Text)
End if

If Check2.Checked then
sngTotal = sngTotal + CSng(Check2.Text)
End if

(and repeat this for as many checkboxes as you have)

I think your problem is that you're not evaluating whether or not the Checkbox is checked. The Checkbox.Checked property is a boolean value (True or False).

Hope this helps!

UPDATE: The answers below are good, but they assume all of your checkbox values are integers. If you ever use anything other than whole dollars (like $1.25 or something like that), you'll need to use Single Precision instead of Integers.

2007-02-19 11:31:49 · answer #2 · answered by Scotty Doesnt Know 7 · 0 0

I think Scott is on the right path. A few things to keep in mind. When evaluating booleans, there is no need to test whether a boolean value is equal to true (i.e. if true = true). Simply use the result of the test. For example, "If CheckBox1.Checked" is saying "If true". It's redundant to write "If CheckBox1.Checked = True" since that's like saying "If True = True" which, of course, it does.

Trying to turn the text of CheckBox1 into a single is potentially a problem if the text of the checkbox is anything other than simply numeric. You can either put a numeric value in the "Tag" property of the check box, or, if you have an array of checkboxes, use the index of the checkbox in the array as the basis for getting the "single" value when adding.

2007-02-19 11:46:49 · answer #3 · answered by Gene 3 · 0 0

The following code will add up the values in two textboxes and display it in the third text box. The code doesnt handle exceptions so you need to create yours.



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

Dim num1 As Integer
Dim num2 As Integer
num1 = TextBox1.Text
num2 = TextBox2.Text

TextBox3.Text = num1 + num2
End Sub

The same applies for the check boxes, on the click event on each of the checkboxes update the textboxes for example "textbox1.text = 1

then add the values up in button click event.

2007-02-19 11:43:11 · answer #4 · answered by AJ 1 · 0 0

fedest.com, questions and answers