How to add up total point using checkboxes?

yuzhen

Member
Joined
May 13, 2009
Messages
12
Programming Experience
Beginner
Hi,

I'm currently doing a sch proj.

I would like to know how do I add points to the checkboxes and how do I add the checked boxes up to get the overall points?

Below I have an attachment on roughly how I wan it to works in case my words are not that clear.

Pls Help

Best Regards,
Dawn.Lee:confused:
 

Attachments

  • GCS.doc
    36.5 KB · Views: 30
You can use a control's ".tag" property to assign any kind of object to a control. Might simply be a "number" or a complex class.
If you only need numbers, you can assign 1, 2, etc to the control at design time.
Create a single event handler to handle all CB's checkedchanged event and in that event add (if checked) or subtract (if unchecked) the value of tag (CInt(ctype(sender, checkbox).tag)) to your overall score.
 
Say for instance, your first check box is named:
E_4
E for the section, 4 for the value of points.

Do a check when the checkbox is checked, to add or subtract points from a global variable: totalScore.

Example:
VB.NET:
Public Class Form1
    Dim totalValue As Integer 'Declare my main variable
    Private Sub E_4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles E_4.CheckedChanged
        If E_4.Checked = True Then 'If my check box is checked
            totalValue += 4 ' add 4 to the total count
        ElseIf E_4.Checked = False Then 'If my checkbox isn't checked
            totalValue -= 4 ' take away 4
        End If
        total.Text = totalValue ' set my totals label to the variable.
    End Sub
End Class
 
now i manage to add up the points from different checkboxes but load in form. but how do i make everything to work in tab instead of working in form?

below is the codes but works in form.

Public Class Trial

Private GCS_Value As Integer = 0

Private Sub Trial_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

lblGCSTotal.Text = CStr(GCS_Value)

End Sub

Private Sub cbxEyes4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

cbxEyes1.Checked = False
cbxEyes2.Checked = False
cbxEyes3.Checked = False
cbxEyes4.Checked = True
update_GCSTotal()

End Sub

Private Sub cbxEyes3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

.
.
.
.
.
.
.
.


End Sub

Sub update_GCSTotal()
GCS_Value = 0
Dim oCtrl As Control

For Each oCtrl In tblEyes.Controls
Try
If CType(oCtrl, CheckBox).Name.Contains("Eyes") And CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If
Catch ex As Exception

End Try

Next

For Each oCtrl In tblVerbal.Controls
Try
If CType(oCtrl, CheckBox).Name.Contains("Verbal") And CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If
Catch ex As Exception

End Try

Next

For Each oCtrl In tblMotor.Controls
Try
If CType(oCtrl, CheckBox).Name.Contains("Motor") And CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If
Catch ex As Exception

End Try

Next

lblGCSTotal.Text = CStr(GCS_Value)

End Sub
End Class


Thank You

Best Regards
Dawn.Lee
 
That's a kind of round about way of getting the total -- you'd be better off using picoflop's suggestion:

VB.NET:
Private Sub HandleAllCheckBoxes(ByVal sender As Object, ByVal e As EventArgs) Handles _ 
chkEyes1.CheckedChanged, chkEyes2.CheckedChanged, .... , _ 
chkVerbal1.CheckedChanged, ....., _ 
chkMotor1.CheckedChanged, ....., chkMotor6.CheckedChanged

     Dim tempBox As CheckBox = DirectCast(sender, CheckBox)
     
     If tempBox.Checked
           'if the box is checked then add it to the total
           GCS_Value += CInt(tempBox.Text)
     Else
           'if the box is unchecked then subtract it from the total
           GCS_Value -= CInt(tempBox.Text)
     End If
     
     'update the label with the latest total
     lblGCSTotal.Text  = GCS_Value.ToString()
End Sub

You only need this one event to handle all the check boxes on the form. If you are making multiple tabs on a form, you could use the .Tag property to tell which tab it is coming from or even the naming structure of your CheckBox controls could tell you which tab it is from. Then in this event you would just have to do an additional check for the tab and then update the correct label.
 
thks thks. I had try out the codes and they wrks fine. but isit possible to make it such a way that only 1 checkbox is being checked in 1 table. As there is actually 3 tables. 1table for eyes checkboxes, 1table for verbal checkboxes and 1table for motor checkboxes. each table is only allow to check 1 checkbox. so how can i edit it?

btw i oso dn understand the

use the Tag property to tell which tab it is coming from or even the naming structure of your CheckBox controls could tell you which tab it is from. Then in this event you would just have to do an additional check for the tab and then update the correct label.

The 3 tables are all in 1 tab page. Tag property is to put the tabpage's name or tabcontrol's name? and how to do additional check for the tab and then update the correct label.
 
Yes, instead of using checkboxes, use radiobuttons inside a groupbox for each of the different groups: eyes, verbal and motor. Within a groupbox, only one radio button can be checked at a time.
 
thks but they requires me to use checkbox instead of radio button.

I have the below codes:
VB.NET:
Public Class Page_2

    Private GCS_Value As Integer = 0

    Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lbl_Total.Text = CStr(GCS_Value)

    End Sub

    Private Sub cbEyes4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes4.MouseClick

        cbEyes1.Checked = False
        cbEyes2.Checked = False
        cbEyes3.Checked = False
        cbEyes4.Checked = True
        update_GCSTotal()

    End Sub

    Private Sub cbEyes3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes3.MouseClick

        cbEyes1.Checked = False
        cbEyes2.Checked = False
        cbEyes3.Checked = True
        cbEyes4.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbEyes2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes2.MouseClick

        cbEyes1.Checked = False
        cbEyes2.Checked = True
        cbEyes3.Checked = False
        cbEyes4.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbEyes1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes1.MouseClick

        cbEyes1.Checked = True
        cbEyes2.Checked = False
        cbEyes3.Checked = False
        cbEyes4.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbVerbal5_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal5.MouseClick

        cbVerbal1.Checked = False
        cbVerbal2.Checked = False
        cbVerbal3.Checked = False
        cbVerbal4.Checked = False
        cbVerbal5.Checked = True
        update_GCSTotal()

    End Sub

    Private Sub cbVerbal4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal4.MouseClick

        cbVerbal1.Checked = False
        cbVerbal2.Checked = False
        cbVerbal3.Checked = False
        cbVerbal4.Checked = True
        cbVerbal5.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbVerbal3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal3.MouseClick

        cbVerbal1.Checked = False
        cbVerbal2.Checked = False
        cbVerbal3.Checked = True
        cbVerbal4.Checked = False
        cbVerbal5.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbVerbal2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal2.MouseClick

        cbVerbal1.Checked = False
        cbVerbal2.Checked = True
        cbVerbal3.Checked = False
        cbVerbal4.Checked = False
        cbVerbal5.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbVerbal1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal1.MouseClick

        cbVerbal1.Checked = True
        cbVerbal2.Checked = False
        cbVerbal3.Checked = False
        cbVerbal4.Checked = False
        cbVerbal5.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbMotor6_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor6.MouseClick

        cbMotor1.Checked = False
        cbMotor2.Checked = False
        cbMotor3.Checked = False
        cbMotor4.Checked = False
        cbMotor5.Checked = False
        cbMotor6.Checked = True
        update_GCSTotal()

    End Sub

    Private Sub cbMotor5_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor5.MouseClick

        cbMotor1.Checked = False
        cbMotor2.Checked = False
        cbMotor3.Checked = False
        cbMotor4.Checked = False
        cbMotor5.Checked = True
        cbMotor6.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbMotor4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor4.MouseClick

        cbMotor1.Checked = False
        cbMotor2.Checked = False
        cbMotor3.Checked = False
        cbMotor4.Checked = True
        cbMotor5.Checked = False
        cbMotor6.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbMotor3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor3.MouseClick

        cbMotor1.Checked = False
        cbMotor2.Checked = False
        cbMotor3.Checked = True
        cbMotor4.Checked = False
        cbMotor5.Checked = False
        cbMotor6.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbMotor2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor2.MouseClick

        cbMotor1.Checked = False
        cbMotor2.Checked = True
        cbMotor3.Checked = False
        cbMotor4.Checked = False
        cbMotor5.Checked = False
        cbMotor6.Checked = False
        update_GCSTotal()

    End Sub

    Private Sub cbMotor1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbMotor1.MouseClick

        cbMotor1.Checked = True
        cbMotor2.Checked = False
        cbMotor3.Checked = False
        cbMotor4.Checked = False
        cbMotor5.Checked = False
        cbMotor6.Checked = False
        update_GCSTotal()

    End Sub

    Sub update_GCSTotal()

        GCS_Value = 0

        If Me.cbEyes1.Checked Then
            GCS_Value += CInt(Me.cbEyes1.Text)
        End If

        If Me.cbEyes2.Checked Then
            GCS_Value += CInt(Me.cbEyes2.Text)
        End If

        If Me.cbEyes3.Checked Then
            GCS_Value += CInt(Me.cbEyes3.Text)
        End If

        If Me.cbEyes4.Checked Then
            GCS_Value += CInt(Me.cbEyes4.Text)
        End If

        If Me.cbVerbal1.Checked Then
            GCS_Value += CInt(Me.cbVerbal1.Text)
        End If

        If Me.cbVerbal2.Checked Then
            GCS_Value += CInt(Me.cbVerbal2.Text)
        End If

        If Me.cbVerbal3.Checked Then
            GCS_Value += CInt(Me.cbVerbal3.Text)
        End If

        If Me.cbVerbal4.Checked Then
            GCS_Value += CInt(Me.cbVerbal4.Text)
        End If

        If Me.cbVerbal5.Checked Then
            GCS_Value += CInt(Me.cbVerbal5.Text)
        End If

        If Me.cbMotor1.Checked Then
            GCS_Value += CInt(Me.cbMotor1.Text)
        End If

        If Me.cbMotor2.Checked Then
            GCS_Value += CInt(Me.cbMotor2.Text)
        End If

        If Me.cbMotor3.Checked Then
            GCS_Value += CInt(Me.cbMotor3.Text)
        End If

        If Me.cbMotor4.Checked Then
            GCS_Value += CInt(Me.cbMotor4.Text)
        End If

        If Me.cbMotor5.Checked Then
            GCS_Value += CInt(Me.cbMotor5.Text)
        End If

        If Me.cbMotor6.Checked Then
            GCS_Value += CInt(Me.cbMotor6.Text)
        End If

        lbl_Total.Text = CStr(GCS_Value)

    End Sub
End Class

It is currently able to check 1 box in a table and also able to do the calculation.

May I know how can I edit so that the checked boxes (checkbox) and the total (label) will be save into my SQL Database?
 
This will help with your connection string - ConnectionStrings.com - Forgot that connection string? Get it here!

and pretty much any google search for sql insert into will get you any help you need for building an insert statement.

VB.NET:
    Private Sub SaveToDatabase()

        'You will need to know your Connection String to the database
        Using sqlCon As New System.Data.SqlClient.SqlConnection(SqlConnectionString)
            'open the connection to the database
            sqlCon.Open()

            'You will need to make your Insert Into statement 
            Using sqlCmd As New System.Data.SqlClient.SqlCommand(SqlInsertStatement, sqlCon)
                sqlCmd.ExecuteNonQuery()
            End Using
        End Using

    End Sub
 
How do I make multi checkboxes selection and save into mySQL database?

Below is the attachment on how it looks like. and the whole table actually have 3 individual that merge to have that bigger table.

Thks.
 

Attachments

  • blood investigation.JPG
    blood investigation.JPG
    39.4 KB · Views: 29
How is your table set up? What are the fields?

Ideally you would have a database field that corresponds to a checkbox preferrably of the datatype (MySql) tinyint and the name of the field corresponds to the name of the checkbox in some way.

For example a database field: fldCXR corresponds to chkCXR in the form.

In that field a 1 would represent checked and 0 would represent unchecked. Then in your code where you build your insert/update statement you can loop through the forms controls and for each checkbox add it to a sqlstatement.

VB.NET:
   Dim SqlStatement As String = ""

        For Each ctl As Control In Me.Controls
            If ctl.GetType.Name.ToUpper = "CHECKBOX" Then
                'set field name
                SqlStatement &= ("fld" & DirectCast(ctl, CheckBox).Name.Substring(3) & " = ")
                'set the value of the field
                SqlStatement &= Convert.ToInt32(DirectCast(ctl, CheckBox).Checked)

            End If
        Next

If your fields don't correspond to the checkboxes by name or by using the Tag property of the checkbox then you are pretty much going to have to add them indvidually to a SQL statement.
 
Ok. Simple: Parameterized Query.

Make your MySQL table have...33 Boolean Columns. The query will rely on your CheckBox.checked for inserting either true or false values (1 or 0). Add another column for other information such as Name, Date, Age, etc so you can identify the patient.
 
I have 3 tables in my application so can I put all the checkboxes in different table into 1 mysql table? and for the location part, i will mostly be instead an drop down list.

Add another column for other information such as Name, Date, Age, etc so you can identify the patient.

I'm putting the ID table into mysql table too.

but how my codes goes about to have those checked checkboxes to save into mysql table?
 
I have yet to create mysql table for this application but I will create in such a way like what formlesstree4 have suggest. 33 Boolean Columns but I cant figure out how to put in the location column as this location column is link only with the procedure table.

Ideally you would have a database field that corresponds to a checkbox preferrably of the datatype (MySql) tinyint and the name of the field corresponds to the name of the checkbox in some way.

For example a database field: fldCXR corresponds to chkCXR in the form.

Do u mean the database field mus be only meant for a particaular checkbox?
 

Attachments

  • blood investigation.JPG
    blood investigation.JPG
    50.5 KB · Views: 24
Back
Top