Hi,

i have 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

Recommended Answers

All 21 Replies

This continues from the thread "How to add up total point?"

First, I have to correct my answer about detecting control's type. Here's how it should be done

If TypeOf (oCtrl) Is CheckBox Then
  If CType(oCtrl, CheckBox).Checked Then
    
  End If
End If

You have to make sure that you don't refer to a property (Checked in this case) that may not exist in some other control type.

but how do i make everything to work in tab instead of working in form?

You didn't specify the problem so I'll guess that you put each GCS section in it's own tab page, right?

Each tab page has its own controls collection. Now you have to loop each tab page's control collection and sum up the points

Sub update_GCSTotal()
  GCS_Value = 0
  Dim oCtrl As Control

  For Each oCtrl In TabControl1.TabPages.Item(0).Controls ' First tab, eyes
    Try
      If TypeOf (oCtrl) Is CheckBox Then
        If CType(oCtrl, CheckBox).Name.Contains("Eyes") AndAlso CType(oCtrl, CheckBox).Checked Then
          GCS_Value += CInt(oCtrl.Text)
        End If
      End If
    Catch ex As Exception

    End Try
  Next

  For Each oCtrl In TabControl1.TabPages.Item(1).Controls ' Second tab, verbal
    Try
      If TypeOf (oCtrl) Is CheckBox Then
        If CType(oCtrl, CheckBox).Name.Contains("Verbal") AndAlso CType(oCtrl, CheckBox).Checked Then
          GCS_Value += CInt(oCtrl.Text)
        End If
      End If
    Catch ex As Exception

    End Try
  Next
  
  ' Third tab page is TabControl1.TabPages.Item(2)

  lblGCSTotal.Text = CStr(GCS_Value)

End Sub

If you don't have any other check boxes in tab control (no name check needed) and you don't need any sub-sums of the GCS section, you can make the code shorter

Dim oTabPage As TabPage

GCS_Value = 0
For Each oTabPage In TabControl1.TabPages
  For Each oCtrl In oTabPage.Controls
    Try
      If TypeOf (oCtrl) Is CheckBox Then
        If CType(oCtrl, CheckBox).Checked Then
          GCS_Value += CInt(oCtrl.Text)
        End If
      End If
    Catch ex As Exception

    End Try
  Next
Next

This code gets all check boxes from every tab page in a tab control and sums up the points (from Text property).

Sorry for that I didn't specific my question. I'm suppose to put the whole GCS table into 1 tab and i will need tab 2 to insert some kind of pictures. So do I do as below or i can shorter the codes:


Sub update_GCSTotal()
GCS_Value = 0
Dim oCtrl As Control

For Each oCtrl In TabControl1.TabPages.Item(0).Controls
Try
If TypeOf (oCtrl) Is CheckBox Then
If CType(oCtrl, CheckBox).Name.Contains("Eyes") AndAlso CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If
End If
Catch ex As Exception

End Try
Next

For Each oCtrl In TabControl1.TabPages.Item(0).Controls
Try
If TypeOf (oCtrl) Is CheckBox Then
If CType(oCtrl, CheckBox).Name.Contains("Verbal") AndAlso CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If
End If
Catch ex As Exception

End Try
Next

' Third tab page is TabControl1.TabPages.Item(0)

lblGCSTotal.Text = CStr(GCS_Value)

End Sub

You can also use a single loop

For Each oCtrl In TabControl1.TabPages.Item(0).Controls
  Try
    If TypeOf (oCtrl) Is CheckBox Then
      If (CType(oCtrl, CheckBox).Name.Contains("Eyes") OrElse CType(oCtrl, CheckBox).Name.Contains("Verbal")) AndAlso CType(oCtrl, CheckBox).Checked Then
        GCS_Value += CInt(oCtrl.Text)
      End If
    End If
  Catch ex As Exception

  End Try
Next

but I don't suggest that. You won't gain anything useful from it. It just makes the code hard to read and maintain.

Your current code looks fine and it should work (I didn't test it).

the codes doesn't seem working. do i need to declare the anything for the tab in public class? the table has turn into a way that i can check everything n there is no total display on label

do i need to declare the anything for the tab in public class?

I would say no even without seeing the whole code.

Have you re-checked Name and Text properties of the check boxes. Contains function may be case sensitive (didn't check that). Or use CType(oCtrl, CheckBox).Name.ToUpper.Contains("EYES") so the case doesn't matter.

Finally, use the debugger i.e. set a breakpoint before For Each loop and step through it. Without knowing how familiar you're with debugging, I may explain obvious things :)
- to set a breakpoint, move the cursor to a line where you want the execution to stop and press F9 (press F9 again to remove the breakpoint)
- to step through the code, press F8. It executes the code line by line
Before debugging, check from VB's menu Build -> Configuration Manager... that the "Active solution configuration" is Debug. Final build should be done with the Release option.

While you step through the code (in For Each loop) you'll see if the counter is increased. If it gets increased, you haven't assigned the sum value to label. If the code never "hits" the line which increments the counter, there's something wrong with the naming of the Name property or you haven't checked any of the check boxes. Notice also that you're looping tab page with index zero. Are you sure that your check boxes are on that tab page?

These are the lines that wasnt being "hits" when doing debugging. I tink that is the problem y the total sum is not being show after i hav checked each checkbox in each table:

If CType(oCtrl, CheckBox).Name.ToUpper.Contains("VERBAL") AndAlso CType(oCtrl, CheckBox).Checked Then
GCS_Value += CInt(oCtrl.Text)
End If

If the code never "hits" the line which increments the counter, there's something wrong with the naming of the Name property or you haven't checked any of the check boxes.

What does it got to do with the name property as my above not "hit" code doesnt seem to have any name that is given by me except for "VERBAL"?

Notice also that you're looping tab page with index zero. Are you sure that your check boxes are on that tab page?

The checkboxes are physically in the tab page in design view but how do i check whether the checkboxes are in the tab page anot?
and how do i knw or check wat index to put? indicated somewhere? :?:

the checkbox is in Private Sub cbxMotor1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbxMotor1.MouseClick
Is that the problem? :-/

Best Regards,
Dawn :icon_smile:

Here's a shorter version

GCS_Value = 0
Dim oCtrl As Control
Dim oTabPage As TabPage

' Loop all tab pages
For Each oTabPage In TabControl1.TabPages
  ' Loop every control in the current tab page
  For Each oCtrl In oTabPage.Controls
    Try
      ' Is this a check box?
      If TypeOf (oCtrl) Is CheckBox Then
        ' It's a check box. Is it checked?
        If CType(oCtrl, CheckBox).Checked Then
          ' Yes, it's checked. Convert Text property to integer and sum it up
          Try
            GCS_Value += CInt(oCtrl.Text)
          Catch ex As Exception
            ' oCtrl.Text wasn't an integer value
          End Try
        End If
      End If
    Catch ex As Exception

    End Try
  Next
Next
lblGCSTotal.Text = CStr(GCS_Value)

It works fine if you don't have any other check boxes expect GCS boxes. If it does, inner Try...Catch handles "incorrect" check boxes.

But see the attachment too. It shows how the check box properties (Name and Text) should be set. And that seems to be the key point why the code doesn't work as expected. Ok, the code above should partly solve that (Text property (properties) has to be still set correctly).

i hav checked each checkbox in each table

Table??? Do you have a table control (TableLayoutPanel) in the tab page?

These are the lines that wasn't being "hits" when doing debugging.

None of the check boxes has "verbal" in Name property OR isn't checked.

What does it got to do with the name property as my above not "hit" code doesnt seem to have any name that is given by me except for "VERBAL"?

You have to name the check boxes "Verbal1, Verbal2,..." and "Eyes1,...." i.e. Name property mustn't have it's default naming Checkbox1, Checkbox2,...

The checkboxes are physically in the tab page in design view but how do i check whether the checkboxes are in the tab page anot?
and how do i knw or check wat index to put? indicated somewhere?

Most likely they are on the tab page with index zero, see the attachment how to check it from the tab control's properties.

the checkbox is in Private Sub cbxMotor1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbxMotor1.MouseClick
Is that the problem?

No. That's just a handler for the cbxMotor1 control.

I hav tried the codes but while typing, the Dim oTabPage As TabPage have some type error which i don't y?:?:

Table??? Do you have a table control (TableLayoutPanel) in the tab page?

Yaya, the checkboxes is in the table. attachment below is the example of my layout.

You have to name the check boxes "Verbal1, Verbal2,..." and "Eyes1,...." i.e. Name property mustn't have it's default naming Checkbox1, Checkbox2,...

They all already properly name as "cbxEyes4, cbxEyes3,cbxEyes2,cbxEyes1" and "cbxVerbal5,......" and "cbxMotor6, .........."

Dim oTabPage As TabPage

Don't type, copy/paste the code (press first "Toggle Plain Text" link) or lines from this web page. You have some typing error, TabPage control exists in every .NET version.

Yaya, the checkboxes is in the table

TabControl and TableLayoutPanel are examples of so called container controls i.e. you can put other controls "inside" of them. When you have a TableLayoutPanel in a TabControl, which is in a Form control, you get a hierarchy of the controls. Form's control collection has a TabControl but it doesn't "know" that the TabControl contains a TableLayoutPanel. Likewise TabControl "has" a TableLayoutPanel, but it doesn't know anything about checkboxes which are "owned" by the TableLayoutPanel. This was the point where things went wrong :D

So, you have to get the check boxes from the table's control collection by loop it.

GCS_Value = 0
Dim oCtrl As Control
Dim oTabPage As TabPage ' This tab page contains the table (layoutpanel)
Dim oTable As TableLayoutPanel ' Checkboxes are in this table

' Get first the correct tab page. This page contains TableLayoutPanel
oTabPage = TabControl1.TabPages.Item(0)
' Get the table assuming only one table (in this tab page). Otherwise, no For loop and hardcode the name of the table
oTable = Nothing
For Each oCtrl In oTabPage.Controls
  ' Is this a table?
  If TypeOf (oCtrl) Is TableLayoutPanel Then
    oTable = CType(oCtrl, TableLayoutPanel) ' Found a table
  End If
Next

' Check that the table was found
If oTable Is Nothing Then
  ' No table, exit
  lblGCSTotal.Text = "N/A"
  Exit Sub ' Can't continue
End If

' Now we have the table. Loop the TABLE'S control collection
For Each oCtrl In oTable.Controls
  Try
    ' Is this a check box?
    If TypeOf (oCtrl) Is CheckBox Then
      ' It's a check box. Is it checked?
      If CType(oCtrl, CheckBox).Checked Then
        ' Yes, it's checked. Convert Text property to integer and sum it up
        Try
          GCS_Value += CInt(oCtrl.Text)
        Catch ex As Exception
          ' oCtrl.Text wasn't an integer value
        End Try
      End If
    End If
  Catch ex As Exception

  End Try
Next

lblGCSTotal.Text = CStr(GCS_Value)

Now you should be looking for the check boxes from the right place :)

Finally. Since you do have a limited number of the check boxes which are not created dynamically and the names of the check boxes are known at the design time, update_GCSTotal procedure could be implemented in the following way

GCS_Value = 0
If Me.Eyes1.Checked Then
  GCS_Value += CInt(Me.Eyes1.Text)
End If
.
.
.
If Me.Verbal1.Checked Then
  GCS_Value += CInt(Me.Verbal1.Text)
End If
.
.
.
lblGCSTotal.Text = CStr(GCS_Value)

Of course, you have to use correct check box names and this solution is far away from elegant compared to the one above.

GCS_Value = 0
Dim oCtrl As Control
Dim oTabPage As TabPage ' This tab page contains the table (layoutpanel)
Dim oTable As TableLayoutPanel ' Checkboxes are in this table

' Get first the correct tab page. This page contains TableLayoutPanel
oTabPage = TabControl1.TabPages.Item(0)
' Get the table assuming only one table (in this tab page). Otherwise, no For loop and hardcode the name of the table
oTable = Nothing
For Each oCtrl In oTabPage.Controls
  ' Is this a table?
  If TypeOf (oCtrl) Is TableLayoutPanel Then
    oTable = CType(oCtrl, TableLayoutPanel) ' Found a table
  End If
Next

' Check that the table was found
If oTable Is Nothing Then
  ' No table, exit
  lblGCSTotal.Text = "N/A"
  Exit Sub ' Can't continue
End If

' Now we have the table. Loop the TABLE'S control collection
For Each oCtrl In oTable.Controls
  Try
    ' Is this a check box?
    If TypeOf (oCtrl) Is CheckBox Then
      ' It's a check box. Is it checked?
      If CType(oCtrl, CheckBox).Checked Then
        ' Yes, it's checked. Convert Text property to integer and sum it up
        Try
          GCS_Value += CInt(oCtrl.Text)
        Catch ex As Exception
          ' oCtrl.Text wasn't an integer value
        End Try
      End If
    End If
  Catch ex As Exception

  End Try
Next

lblGCSTotal.Text = CStr(GCS_Value)

The above code isit copy into the Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click :?: and I'm having 3 different table which are the Eyes, Verbal and Motor. And all 3 table will only be having 1 tick in 1 table. so how the codes able to detect all 3 tables? isit i must copy paste the whole process for 3times?

GCS_Value = 0
If Me.Eyes1.Checked Then
  GCS_Value += CInt(Me.Eyes1.Text)
End If
.
.
.
If Me.Verbal1.Checked Then
  GCS_Value += CInt(Me.Verbal1.Text)
End If
.
.
.
lblGCSTotal.Text = CStr(GCS_Value)

Does this replace mine code:

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
.
.
.
.
rivate Sub cbxVerbal4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

cbxVerbal1.Checked = False
cbxVerbal2.Checked = False
cbxVerbal3.Checked = False
cbxVerbal4.Checked = True
cbxVerbal5.Checked = False
update_GCSTotal()

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

cbxMotor1.Checked = True
cbxMotor2.Checked = False
cbxMotor3.Checked = False
cbxMotor4.Checked = False
cbxMotor5.Checked = False
cbxMotor6.Checked = False
update_GCSTotal()

End Sub

and do I put the above quoted codes in Sub update_GCSTotal() and take away the checked true/false codes?

sry if I hav been asking question that already hav answer right in the post. i'm totally new in vb.

do I put the above quoted codes in Sub update_GCSTotal()

Yes. I should have made that clear, my mistake.

Like I said about control (collection) hierarchy, having three TableLayoutPanels forms the following hierarchy:

Form -> TabControl -> TabPage -> TableLayoutPanel(s) -> CheckBox(es)

In the code it means, get the right TabPage, loop it's control collection to find TableLayoutPanels, and for each TableLayoutPanel, loop it's control collection to get check boxes. I won't give the code for that. If you need it, just ask.

I suggest using the latter approach I gave in my previous post. With If Me.Eyes1.Checked Then syntax, you'll get the check boxes without knowing the control hierarchy and summed them. And these If Then clauses go to update_GCSTotal procedure. I believe it will be the easiest (although not fanciest) way to get that summing to work.

Do not change any of the event handlers for summing the points. Just call update_GCSTotal in every check box click event handler like you already do.

A one small point. You seem to allow only one check box to be checked (in each GSC section) at the time. There's a RadioButton control in VB which does this without explicitly checking the others off or on. Visually check boxes and radio buttons differ and you may have wanted to have "check box" looking radio buttons. If you weren't aware of radio button control, consider using it next time you come up with "only one option selected" situation. You'd still need to trap click events to sum up the points. Anyway, do not change check boxes to radio buttons at this point. Your "only one check box selected" code seems ok, so there's no point to re-write that part.

the application can wrk but my codes are only like that....

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

and the application turn out to be able to check per table and calculate the total w/o putting in

GCS_Value = 0
Dim oCtrl As Control
Dim oTabPage As TabPage ' This tab page contains the table (layoutpanel)
Dim oTable As TableLayoutPanel ' Checkboxes are in this table

' Get first the correct tab page. This page contains TableLayoutPanel
oTabPage = TabControl1.TabPages.Item(0)
' Get the table assuming only one table (in this tab page). Otherwise, no For loop and hardcode the name of the table
oTable = Nothing
For Each oCtrl In oTabPage.Controls
' Is this a table?
If TypeOf (oCtrl) Is TableLayoutPanel Then
oTable = CType(oCtrl, TableLayoutPanel) ' Found a table
End If
Next

' Check that the table was found
If oTable Is Nothing Then
' No table, exit
lblGCSTotal.Text = "N/A"
Exit Sub ' Can't continue
End If

' Now we have the table. Loop the TABLE'S control collection
For Each oCtrl In oTable.Controls
Try
' Is this a check box?
If TypeOf (oCtrl) Is CheckBox Then
' It's a check box. Is it checked?
If CType(oCtrl, CheckBox).Checked Then
' Yes, it's checked. Convert Text property to integer and sum it up
Try
GCS_Value += CInt(oCtrl.Text)
Catch ex As Exception
' oCtrl.Text wasn't an integer value
End Try
End If
End If
Catch ex As Exception

End Try
Next

lblGCSTotal.Text = CStr(GCS_Value)

am i on the right track? or there is something wrong with my VB? i hav attached my printscreen below =)

am i on the right track? or there is something wrong with my VB?

Yes you are. The screen shot was taken at the run time, three check boxes checked with the correct total sum. The code works as expected, so I don't see that there's anything wrong with the code.

Ok, the code (and my solution) is not as elegant as it could have been, but since you have only fifteen check boxes and the code works, I wouldn't try any other approach. A good rule of thumb in programming is "If it ain't broke, don't fix it" ;)

okok. shall fix it if only there is something went wrong right? :)

btw can i get some hint from u whether how do i add in all my data into there table? add data in the checked boxes and textboxes :)

shall fix it if only there is something went wrong right?

Yes. If it works now, that's great. No need to fix anything.

how do i add in all my data into there table? add data in the checked boxes and textboxes

Didn't quite get that. To me it seems like a Physical Examination form that a user fills, right? You need to store the data somewhere, a database perhaps or a file? Do you need to retrieve the data back from the database or a file?

If you do mean saving and loading data, then let's start by data storage. I mean, what kind of storage you will have for the data?

Basically you already know how to do it. Simply assign (saved) values to properties, like cbEyes1.Checked = True or txtPulse.Text = "60".

Sorry for the unclear information. what i meant previously is regarding saving those checked checkbox and textbox data into the SQL database.

I have try the below codes for saving data. it works but it looks kinda of in a mess so i was wondering if there is anyway i can make it neater? hope u understand what i'm typing :S

Dim value_Eyes As String = ""
    Dim value_Verbal As String = ""
    Dim value_Motor As String = ""

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()
        value_Eyes = "4"

    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()
        value_Eyes = "3"

    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
.
.
.
.
.
.

        Dim connAdd As New SqlConnection _
        ("Data Source=(PC's Name , Port No.);" + "Initial Catalog=(Database's Name);" + "User ID=uid;" + "Password=pw;")

        Dim connAdd1 As New SqlConnection _
        ("Data Source=(PC's Name , Port No.);" + "Initial Catalog=(Database's Name);" + "User ID=uid;" + "Password=pw;")

        Dim mySQL As String

        Try
            connAdd.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Open", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        mySQL = "insert into gcs (eyesOpen, verbal, motor, totalGcs) values ('" & Trim(value_Eyes) & "','" & Trim(value_Verbal) & "','" & Trim(value_Motor) & "','" & Trim(lbl_Total.Text) & "' ) "

        Dim cmdAdd As New SqlCommand(mySQL, connAdd)

        Try
            cmdAdd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Query", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Try
            connAdd1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Open", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        mySQL = "insert into vitalSign (pulse, respiration, mean, spo2, temperature) values ('" & Trim(txtPulse.Text) & "','" & Trim(txtRespiration.Text) & "','" & Trim(txtMean.Text) & "','" & Trim(txtSpo2.Text) & "','" & Trim(txtTemperature.Text) & "') "

        Dim cmdAdd1 As New SqlCommand(mySQL, connAdd)

        Try
            cmdAdd1.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Query", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Actually I tried using SELECT...CASE for the selection of checked box to be save but I do not know how to use so end up i uses value_Eyes = "4", value_Verbal = "5", value_Motor = "6", in individual checkbox to make it works :)

it works but it looks kinda of in a mess so i was wondering if there is anyway i can make it neater?

I have to say again that it does work but it's not very elegant solution :)

Actually I tried using SELECT...CASE for the selection of checked box to be save

If you have to read data back from the DB, you can use Select...Case to check the check boxes.

You could take value_Eyes = "4" etc. away from mouse click event handlers and move it to update_GCSTotal()

Sub update_GCSTotal()

        GCS_Value = 0

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

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

Since value_Eyes is actually storing integer values, you could also change Dim value_Eyes As String to Dim value_Eyes As Integer and so on . Then you would have to change the data type in DB from VarChar to Int and insert statement from mySQL = "insert into gcs (eyesOpen, verbal, motor, totalGcs) values ('" & Trim(value_Eyes) & "','" & Trim(value_Verbal) & "','" & Trim(value_Motor) & "','" & Trim(lbl_Total.Text) & "' ) " to mySQL = "insert into gcs (eyesOpen, verbal, motor, totalGcs) values (" & value_Eyes & "," & value_Verbal & "," & value_Motor & "," & GCS_Value & " ) " . Declare GCS_Value as a global (integer) variable like you do with value_Eyes etc.

If you decide to keep value_Eyes etc. as a string variable and store values as text, just make sure that you don't use DB's default field sizes, like VarChar(50), since you'll have a maximum of two digits to store (VarChar(50) -> VarChar(2)).

Just a few ideas how to improve the code a bit. But as a reminder (again), if it does work now... :)

thks thks :) I have tried out the codes but it turn out to be not wrking so i change it back to my old codes.

now I have another problem. currently I have these coding for the selection area on image isit possible to edit the codes in order to have multi-selection instead of only 1? n how do I save the image plus the selection parts into my SQL database aft a SAVE button is being press?

This image thing is in the 2nd tabpage while the above checkboxes stuffs are on the 1st tabpage. Can 1 button save all data in tab 1 & 2? where shld the button is placed at? 1st or 2nd tabpage?

Public Class Page_2
' for selection on part of body
    Private start As Point = Point.Empty
    Private rect As Rectangle
    Private p As Pen

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

        'initialize pen
        p = New Pen(Color.Red)
        p.DashStyle = Drawing2D.DashStyle.Dash

   End Sub

    Private Sub pbBody_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown

        start = e.Location

    End Sub

    Private Sub pbBody_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseMove

        If start <> Point.Empty Then
            Dim x As Integer = Math.Min(start.X, e.X)
            Dim y As Integer = Math.Min(start.Y, e.Y)
            Dim wid As Integer = Math.Abs(start.X - e.X)
            Dim hgt As Integer = Math.Abs(start.Y - e.Y)
            Dim changefilter As Integer = 5
            If Math.Abs(rect.Width - wid) > changefilter _
            Or Math.Abs(rect.Height - Height) > changefilter Then
                rect = New Rectangle(x, y, wid, hgt)
                pbBody.Refresh()
            End If
        End If

    End Sub

    Private Sub pbBody_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp

        start = Point.Empty

    End Sub

    Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint

        If rect <> Rectangle.Empty Then e.Graphics.DrawRectangle(p, rect)

    End Sub

    Private Sub menuUndo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuUndo.Click

        rect = Rectangle.Empty
        pbBody.Refresh()

    End Sub

End Class

Example how the image n selection works.

I have tried out the codes but it turn out to be not wrking so i change it back to my old codes.

That's fine, it worked. I just showed a few ideas.

This image thing is in the 2nd tabpage while the above checkboxes stuffs are on the 1st tabpage. Can 1 button save all data in tab 1 & 2? where shld the button is placed at? 1st or 2nd tabpage?

The button can be on the either one or on the form. I previously mentioned about control hierarchy. If the button is on the same page, the button has "direct access" to the right image. But you can always qualify names, like myForm.myTabControl.myTabPage(1).myPicture, or if the picture box has an unique name simply use myPicture.

isit possible to edit the codes in order to have multi-selection instead of only 1?

Yes. Instead of Private rect As Rectangle you need an array Private rect() As Rectangle (and maybe some counter variable). Everywhere you have rect you use rect(CurSel) if CurSel is the index in the array for the current selection. In the MouseDown event you increase the CurSel (counter), redim (preserve) rect(CurSel) array, and if the user undos, dispose rect(CurSel) and decrease CurSel. That's the basic idea. Shouldn't be very hard to implement since you already have a working code for a one selection.

n how do I save the image plus the selection parts into my SQL database aft a SAVE button is being press?

For saving the image, see my blog posts Convert image to byte array and vice versa and Save binary data to SQL Server with VB.NET. If you have questions about blog posts, post the questions here in DaniWeb, not to blog since I don't check comments daily.

Saving the selections is a bit harder. Since you don't know, how many selections user makes, you can't have a single fields to store x, y, wid and hgt. I assume that you store data in a single table. One way to store "an array" would be to "pack" values (integers) in a VarChar field, for example VarChar(1024). You do have to limit the number of selections and check that the "packed" data does not exceed field length (1024). Here's a way to pack (a global) rect array to a string

Private Function PackData(ByRef PackedData As String) As Boolean
  ' Pack rect array to a string.
  ' Returns True if the packed data does not exceed the predifed maximum length
  Const MAX_LEN As Integer = 1024 ' Change this to match the field size in DB
  Const ValueSep As String = ";" ' Change this if you want to
  Dim i As Integer

  PackedData = ""
  For i = 0 To CurSel ' CurSel is the index of the last item in the rect array
    ' Pack an array item separated with separator char
    PackedData = PackedData & rect(i).X & ValueSep & rect(i).Y & ValueSep & rect(i).Width & ValueSep & rect(i).Height
    If i < CurSel Then
      ' Not last item in the array, add separator
      PackedData = PackedData & ValueSep
    End If
  Next i
  ' Check the length
  If PackedData.Length <= MAX_LEN Then
    Return True
  Else
    Return False
  End If

End Function

You can call it each time the user makes a new selection just to make sure the data fits in the DB. If it doesn't, tell the user that he/she can't make any more selections (and undo last selection). Here's how you call the function

Dim PackedRectArray As String

PackedRectArray = ""
If PackData(PackedRectArray) Then
  ' Ok to save to DB
Else
  MessageBox.Show("The data can not be saved to the database", "Too many selections", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

To read selection from the database, read the packed data string, use Split (Strings.Split(StringFromDB, ValueSep)) to get an array of values, loop values and for each for values in the array create a new rect (i.e. selection) in an array. Remember also casting types (.ToString to get Integer -> String and CInt to get String -> Integer). And declare Const ValueSep As String = ";" as a global if you intend to load selections from the DB.

This was mostly "pseudo-code" or simply explanation how to do it. If you can't get your code to work, post it here and I'll take a look it. But I believe you get at least started with these "few" tips ;)

ok. I think I will handle with the multi-selection first before proceed with others....

Private rect As Rectangle you need an array Private rect() As Rectangle (and maybe some counter variable).

I don't get this part. what you mean by "(and maybe some counter variable)" and do i need to put anything inside the ()?

Everywhere you have rect you use rect(CurSel) if CurSel is the index in the array for the current selection. In the MouseDown event you increase the CurSel (counter), redim (preserve) rect(CurSel) array, and if the user undos, dispose rect(CurSel) and decrease CurSel. That's the basic idea. Shouldn't be very hard to implement since you already have a working code for a one selection.

And this part. I have to DIM CurSel as and isit DIM in gobal? Sorry :(

what you mean by "(and maybe some counter variable)"

The size of the array. I used that CurSel variable to point to current (= last) array item's index.

do i need to put anything inside the ()?

Yes if you want a fixed number of selections. But I'll show how to use a dynamically sized array.

I have to DIM CurSel as and isit DIM in gobal?

Yes, use a global variable.

Ok, here's the code for MouseMove and menuUndo.Click events

Private Sub pbBody_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseMove

  If start <> Point.Empty Then
    Dim x As Integer = Math.Min(start.X, e.X)
    Dim y As Integer = Math.Min(start.Y, e.Y)
    Dim wid As Integer = Math.Abs(start.X - e.X)
    Dim hgt As Integer = Math.Abs(start.Y - e.Y)
    Dim changefilter As Integer = 5
    ' This has to be changed since we don't have a rect yet!
    'If Math.Abs(rect.Width - wid) > changefilter _
    'Or Math.Abs(rect.Height - Height) > changefilter Then
    If Math.Abs(x - wid) > changefilter _
      OrElse Math.Abs(y - Height) > changefilter Then
      CurSel += 1 ' Increase counter
      ' Redim the array
      ReDim Preserve rect(CurSel)
      rect(CurSel) = New Rectangle(x, y, wid, hgt)
      ' At this point you may call PackData function to see if the new selection data "fits".
      ' If it doesn't, "rollback" this new item.
      ' A better way may be to "disable" selection feature if the user has made too many selections
      pbBody.Refresh()
    End If
  End If

End Sub

Private Sub menuUndo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuUndo.Click
  ' Remove current selection

  If CurSel >= 0 Then
    rect(CurSel) = Rectangle.Empty
    CurSel -= 1 ' Decrease counter
    If CurSel >= 0 Then
      ' Redim the array, can't redim to a negative size!
      ReDim Preserve rect(CurSel)
    End If
    pbBody.Refresh()
  End If

End Sub

I hope you get the idea. I mean how the array of selections is changed dynamically.

The point where the selection (=rect) is created may not be the best one. Maybe you should just draw the "selection" at MouseMove event and once the user releases mouse button (MouseUp event), you create the selection (new rect() item) if the criteria has been met. That code is now in the MouseMove event so you may move it to MouseUp (not the selection drawing code!). I didn't actually test the code, but I think it would work better in that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.