Public Class Form1

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then txt1.Visible = True Else txt1.Visible = False
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then txt2.Visible = True Else txt2.Visible = False
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked = True Then txt3.Visible = True Else txt3.Visible = False
    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.Checked = True Then txt4.Visible = True Else txt4.Visible = False
    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.Checked = True Then txt5.Visible = True Else txt5.Visible = False
    End Sub

    Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
        If CheckBox6.Checked = True Then txt6.Visible = True Else txt6.Visible = False
    End Sub

    Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
        If CheckBox7.Checked = True Then txt7.Visible = True Else txt7.Visible = False
    End Sub

    Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
        If CheckBox8.Checked = True Then txt8.Visible = True Else txt8.Visible = False
    End Sub

    Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
        If CheckBox9.Checked = True Then txt9.Visible = True Else txt9.Visible = False
    End Sub

    Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
        If CheckBox10.Checked = True Then txt10.Visible = True Else txt10.Visible = False
    End Sub

    Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
        If CheckBox11.Checked = True Then txt11.Visible = True Else txt11.Visible = False
    End Sub

    Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
        If CheckBox12.Checked = True Then txt12.Visible = True Else txt12.Visible = False
    End Sub

    Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
        If CheckBox13.Checked = True Then txt13.Visible = True Else txt13.Visible = False
    End Sub

    Private Sub CheckBox14_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox14.CheckedChanged
        If CheckBox14.Checked = True Then txt14.Visible = True Else txt14.Visible = False
    End Sub

    Private Sub CheckBox15_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox15.CheckedChanged
        If CheckBox15.Checked = True Then txt15.Visible = True Else txt15.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim mssg As String
        Dim totalprice As Integer
        mssg = "QTY             DESCRIPTION             PRICE               TOTAL" + vbCr

        If CheckBox1.Checked = True Then
            mssg += txt1.Text & "                   " & CheckBox1.Text & "                 " & Label3.Text & "                   " & (Label3.Text * txt1.Text).ToString & vbCr
            totalprice += Label3.Text * txt1.Text
        End If
        If CheckBox2.Checked = True Then
            mssg += txt2.Text & "                   " & CheckBox2.Text & "                   " & Label4.Text & "                   " & (Label4.Text * txt2.Text).ToString & vbCr
            totalprice += Label4.Text * txt2.Text
        End If
        If CheckBox3.Checked = True Then
            mssg += txt3.Text & "                 " & CheckBox3.Text & "                 " & Label5.Text & "                 " & (Label5.Text * txt3.Text).ToString & vbCr
            totalprice += Label5.Text * txt3.Text
        End If
        If CheckBox4.Checked = True Then
            mssg += txt4.Text & "                   " & CheckBox4.Text & "                   " & Label6.Text & "                   " & (Label6.Text * txt4.Text).ToString & vbCr
            totalprice += Label6.Text * txt4.Text
        End If
        If CheckBox5.Checked = True Then
            mssg += txt5.Text & "                   " & CheckBox5.Text & "                   " & Label7.Text & "                   " & (Label7.Text * txt5.Text).ToString & vbCr
            totalprice += Label7.Text * txt5.Text
        End If
        If CheckBox6.Checked = True Then
            mssg += txt6.Text & "                   " & CheckBox6.Text & "                          " & Label9.Text & "                     " & (Label9.Text * txt6.Text).ToString & vbCr
            totalprice += Label9.Text * txt6.Text
        End If
        If CheckBox7.Checked = True Then
            mssg += txt7.Text & "                   " & CheckBox7.Text & "                  " & Label10.Text & "                     " & (Label10.Text * txt7.Text).ToString & vbCr
            totalprice += Label10.Text * txt7.Text
        End If
        If CheckBox8.Checked = True Then
            mssg += txt8.Text & "                   " & CheckBox8.Text & "                  " & Label11.Text & "                     " & (Label11.Text * txt8.Text).ToString & vbCr
            totalprice += Label11.Text * txt8.Text
        End If
        If CheckBox9.Checked = True Then
            mssg += txt9.Text & "                   " & CheckBox9.Text & "                  " & Label12.Text & "                    " & (Label12.Text * txt9.Text).ToString & vbCr
            totalprice += Label12.Text * txt9.Text
        End If
        If CheckBox10.Checked = True Then
            mssg += txt10.Text & "                   " & CheckBox10.Text & "                   " & Label13.Text & "                   " & (Label13.Text * txt10.Text).ToString & vbCr
            totalprice += Label13.Text * txt10.Text
        End If
        If CheckBox11.Checked = True Then
            mssg += txt11.Text & "                   " & CheckBox11.Text & "                 " & Label15.Text & "                    " & (Label15.Text * txt11.Text).ToString & vbCr
            totalprice += Label15.Text * txt11.Text
        End If
        If CheckBox12.Checked = True Then
            mssg += txt12.Text & "                   " & CheckBox12.Text & "                   " & Label16.Text & "                   " & (Label16.Text * txt12.Text).ToString & vbCr
            totalprice += Label16.Text * txt12.Text
        End If
        If CheckBox13.Checked = True Then
            mssg += txt13.Text & "                   " & CheckBox13.Text & "                   " & Label17.Text & "                   " & (Label17.Text * txt13.Text).ToString & vbCr
            totalprice += Label17.Text * txt13.Text
        End If
        If CheckBox14.Checked = True Then
            mssg += txt14.Text & "                   " & CheckBox14.Text & "                   " & Label18.Text & "                   " & (Label18.Text * txt14.Text).ToString & vbCr
            totalprice += Label18.Text * txt14.Text
        End If
        If CheckBox15.Checked = True Then
            mssg += txt15.Text & "                   " & CheckBox15.Text & "                   " & Label19.Text & "                   " & (Label19.Text * txt15.Text).ToString & vbCr
            totalprice += Label19.Text * txt15.Text
        End If

        mssg += vbCr + vbCr
        mssg += vbCr + vbCr
        mssg += "Total : " & totalprice
        MsgBox(txt16.Text + vbCr + mssg)

    End Sub
End Class

it works perfectly fine earlier but now when I click the button the message box wouldn't appear

Hi I take it txt16.Text actually contains text just I can't see it being populated.

Also you appear to want the results in a table, why not use a dialog form instead? You could remove its cancel button and use a datagrid to display the text you need - it would give you more control over how it all looked (and save you relying on unweldy strings)

Label3.Text * txt1.Text

You should convert the strings to numbers before performing numeric calculations.

If CheckBox8.Checked = True Then txt8.Visible = True Else txt8.Visible = False

This can be simplified to

txt8.Visible = CheckBox8.Checked

Also, you might consider creating these controls at runtime and saving references in an array. You could use one handler for each set of controls and replace the brute force code with loops. An example of creating controls at runtime can be found here

Here is how to simplify the code. The spacing of the message box at the end will need some tweaking to align everything. Note that I use the Tag property of CheckBox to determine the corresponding label and textbox controls.

Imports System.Text

Public Class Form1

    Private cbox As New List(Of CheckBox)
    Private tbox As New List(Of TextBox)
    Private cost As New List(Of Label)

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

        For i As Integer = 0 To 14

            Dim y As Integer = 50 + i * 23

            'Create a new label, save reference in list and add to form controls

            Dim lb As New Label
            lb.Text = y.ToString
            lb.Location = New Point(13, y)
            lb.AutoSize = True
            cost.Add(lb)
            Me.Controls.Add(lb)

            'Create a new checkbox, save reference in list and add to form controls

            Dim cb As New CheckBox
            cb.Text = "CheckBox " & i.ToString
            cb.Location = New Point(50, y)
            cb.Checked = False
            cb.Tag = i
            cb.AutoSize = True
            cbox.Add(cb)
            AddHandler cb.CheckedChanged, AddressOf CheckBox_CheckedChanged
            Me.Controls.Add(cb)

            'Create a new checkbox, save reference in list and add to form controls

            Dim tb As New TextBox
            tb.Text = i.ToString
            tb.Location = New Point(150, y)
            tb.Visible = False
            tbox.Add(tb)
            Me.Controls.Add(tb)

        Next

    End Sub

    Private Sub CheckBox_CheckedChanged(sender As System.Object, e As System.EventArgs)
        Dim cb As CheckBox = sender
        tbox(cb.Tag).Visible = cb.Checked
    End Sub

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

        Dim msg As String = "QTY             DESCRIPTION             PRICE               TOTAL" + vbCrLf
        Dim totalPrice As Integer = 0

        For Each cb As CheckBox In cbox
            Dim i As Integer = cb.Tag
            If cb.Checked Then
                totalPrice += CInt(cost(i).Text) * CInt(tbox(i).Text)
                msg &= tbox(i).Text & "                   " &
                       cbox(i).Text & "                 " &
                       cost(i).Text & vbCrLf
            End If
        Next

        msg &= vbCrLf & vbCrLf & vbCrLf & vbCrLf &
               "Total: " & totalPrice

        MsgBox(msg)

    End Sub

End Class

Edited 4 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.