Can Anyone help me on check boxes?

Hi

I have a program with 6 check boxes, all with different values ie:-

chk1 = 300
chk2 = 150 etc

What i need to do is at the click of a calculate button, i have to gernerate the total in a text box, as in if only 1 check box is checked or all 6 are checked, then the total adds up in the text Box.

Or if i deselect the smoe of the check boxes and then push calculate, then the total will be minus the unchecked box, or boxes.

If anyone can help me on this code, i would be most gratefull

thanx Gary

Comments

  • Hello Gary,
    Dont get what exactly you want. Is it that at the first click it should calculate the sum of all the values and in the next click it should subtract the value of unchecked boxes. Or is it that just you have add if its checked and subtract if its unchecked???

    If it the second case you can use IF ELSE condition else you can you a CASE SELECT structure.

    Kshitij.
  • Lets say that you store the value of each checkbox as its text. Here's a method to add each one up. It uses this keyword:
    [pre]
    iif(boolean statement,Object to return if true,Object to return if false) as object.[/pre]

    This is semi suedo code as I haven't worked with checkboxes fora while and can't remember their exact properties.
    [code]
    'If you had checkboxes chk1 to chk6
    Function addup() as integer
    return iif(chk1.checked,chk1.value,0)+iif(chk2.checked,chk2.value,0)+
    iif(chk3.checked,chk3.value,0)+iif(chk4.checked,chk4.value,0)+
    iif(chk5.checked,chk5.value,0)+iif(chk6.checked,chk6.value,0)
    End Function
    [/code]
    If they were in an array it would be much easier to add more checkboxes, as you would use a for loop and only have to change the upper bound of the loop.

    Relman

    : Hi
    :
    : I have a program with 6 check boxes, all with different values ie:-
    :
    : chk1 = 300
    : chk2 = 150 etc
    :
    : What i need to do is at the click of a calculate button, i have to gernerate the total in a text box, as in if only 1 check box is checked or all 6 are checked, then the total adds up in the text Box.
    :
    : Or if i deselect the smoe of the check boxes and then push calculate, then the total will be minus the unchecked box, or boxes.
    :
    : If anyone can help me on this code, i would be most gratefull
    :
    : thanx Gary
    :

  • : Hello Gary,
    : Dont get what exactly you want. Is it that at the first click it should calculate the sum of all the values and in the next click it should subtract the value of unchecked boxes. Or is it that just you have add if its checked and subtract if its unchecked???
    :
    : If it the second case you can use IF ELSE condition else you can you a CASE SELECT structure.
    :
    : Kshitij.
    :
    Hi Kshitij

    yes what i want to do is have six check boxes and when any or all are checked and i push calculate, then the total ads up in the textbox. All six check boxes have different values.

    So the textbox will either just dispay one check box value or how many that are checked

    Gary
  • : Lets say that you store the value of each checkbox as its text. Here's a method to add each one up. It uses this keyword:
    : [pre]
    : iif(boolean statement,Object to return if true,Object to return if false) as object.[/pre]
    :
    : This is semi suedo code as I haven't worked with checkboxes fora while and can't remember their exact properties.
    : [code]
    : 'If you had checkboxes chk1 to chk6
    : Function addup() as integer
    : return iif(chk1.checked,chk1.value,0)+iif(chk2.checked,chk2.value,0)+
    : iif(chk3.checked,chk3.value,0)+iif(chk4.checked,chk4.value,0)+
    : iif(chk5.checked,chk5.value,0)+iif(chk6.checked,chk6.value,0)
    : End Function
    : [/code]
    : If they were in an array it would be much easier to add more checkboxes, as you would use a for loop and only have to change the upper bound of the loop.
    :
    : Relman
    :
    : : Hi
    : :
    : : I have a program with 6 check boxes, all with different values ie:-
    : :
    : : chk1 = 300
    : : chk2 = 150 etc
    : :
    : : What i need to do is at the click of a calculate button, i have to gernerate the total in a text box, as in if only 1 check box is checked or all 6 are checked, then the total adds up in the text Box.
    : :
    : : Or if i deselect the smoe of the check boxes and then push calculate, then the total will be minus the unchecked box, or boxes.
    : :
    : : If anyone can help me on this code, i would be most gratefull
    : :
    : : thanx Gary
    : :
    :
    :
    Hi Gary,
    I have just joined this forum less than a week ago, I hope my suggestion doesn't come in too late. I feel that there is a more generic way in tackling your problem. Please consider the codes shown below:

    Private Sub btnCalculate_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCalculate.Click

    Dim intLoop As Integer
    Dim intVal As Integer

    ' Loop through all Controls on form
    For intLoop = 0 To Me.Controls.Count - 1

    ' Find the type of control needed
    If Me.Controls(intLoop).GetType Is _
    GetType(System.Windows.Forms.CheckBox) Then

    ' Instantiate an instance variable of the same type
    Dim chkControl As System.Windows.Forms.CheckBox

    ' Assign reference to the new instance variable with the control
    chkControl = Me.Controls(intLoop)

    ' Check for Checked state of the instance variable
    If chkControl.Checked Then
    ' Perform the required calculation
    intVal += Val(Me.Controls(intLoop).Text)
    End If
    End If
    Next

    ' Just display the calculated value
    Me.txtSum.Text = CStr(intVal)

    End Sub


    Try it out, I hope it help.
    Albert

    (P/S Thanks kainsworth for helping me in making that first step by answering my post on 2004-10-13 "Clear Textbox controls with for each loop")
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories