1

VB.NET 2008 windows app form. I have a groupbox with several checkboxes, comboboxes, and textboxes within it.

With the help of StackOverFlow members, I've learned how to use the FOR/NEXT loop to find all checkboxes that are checked.

Code:

    Dim chk As CheckBox
    Dim sb As New System.Text.StringBuilder
    For Each chk In gbInterior.Controls.OfType(Of CheckBox)()
        If chk.Checked Then
            sb.AppendLine(chk.Text)
        End If
    Next chk

I am then using this information to write the checkbox names in the body of an email, using the following code:

Dim Outl As Object
    Outl = CreateObject("Outlook.Application")
    If Outl IsNot Nothing Then
        Dim omsg As Object
        omsg = Outl.CreateItem(0)
        omsg.To = ""
        omsg.Subject = "Cabinet Request"
        omsg.Body = "A cabinet request has been created with the following information:" _
        + vbCrLf + sb.ToString

As you can see by the code sb.tostring will print every checked checkbox name on its own line...great. How do I associate the proper combobox.selecteditem, textbox.value, and checkbox name to print out on the same line. When I say proper...example; checkbox=pen, combobox=color, textbox=quantity.Let's say my other checkboxes are different items and the rest of the boxes are the same. Since I'm using a loop, how do I create the association? This is the result I'm looking for:

pen blue 1
pencil black 3
eraser pink 2

Thanks in advance

UBel
  • 71
  • 1
  • 3
  • 10

2 Answers2

1

Final EDIT (tested and confirmed):

Public Class Form1

    Private Enum interiorTypes

        Rack
        RackShelf
        RackSlide
        RackDrawer
        BackPanel
        Shelf
        Drawer
        Light
        Fan
        Therm

    End Enum       

    Private Sub btnOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOrder.Click

        Dim sb As New System.Text.StringBuilder

        For Each type As interiorTypes In System.Enum.GetValues(GetType(interiorTypes))

            Dim chk = CType(gbInterior.Controls.Find("c" & type.ToString(), True)(0), CheckBox)
            Dim cb = CType(gbInterior.Controls.Find("cb" & type.ToString(), True)(0), ComboBox)
            Dim tb = CType(gbInterior.Controls.Find("tb" & type.ToString(), True)(0), TextBox)

            If chk.Checked Then
                sb.AppendFormat("{0} {1} {2}", chk.Text, cb.SelectedItem.ToString, tb.Text)
                sb.AppendLine()
            End If

        Next

        MsgBox(sb.ToString, MsgBoxStyle.OkOnly, "Order Summary")

    End Sub

End Class

If you don't have all 3 inputs for each Interior Type, then you will need to check for Nothingness. To go with your example of a missing ComboBox, after your Dim declarations inside your For loop, you could do:

If chk.Checked Then

    sb.Append(chk.Text)
    If cb IsNot Nothing Then sb.Append(" " & cb.SelectedItem.ToString)
    sb.Append(" " & tb.Text)
    sb.AppendLine()

End If
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • I would appreciate the help. What are you refering to "design file markup" is this the "form[design]"? – UBel Mar 21 '12 at 12:04
  • Yea, but more importantly, how are you grouping these controls? Do they all follow a certain naming convention like cb1, chk1, tb1 then cb2, chk2, tb2? Or are they grouped together where each group has its own container, so that the containers could be looped through? – Code Maverick Mar 21 '12 at 15:12
  • They are all in the same group, do I need to place one tb, cb, and chk in its own group? they do follow a certain naming convention. I am still not seeing the for loop...For each ???? – UBel Mar 21 '12 at 17:58
  • Give me the naming convention and I can modify my code accordingly. – Code Maverick Mar 21 '12 at 18:46
  • My naming convention has the following prefixes c for checkbox, cb for combobox, and tb for textbox. Now I have 10 of each with the following names: - rack - rackshelf - rackslide - rackdrawer - backpanel - shelf - drawer - light - fan - therm. All are in gbinterior groupbox. – UBel Mar 22 '12 at 14:50
  • See my edit and tell me if I understood you correctly with respect to your naming convention. – Code Maverick Mar 22 '12 at 15:29
  • You understood me correctly. I'm starting to understand how you're tying it all together now but I can't get it to work. I think it's due to me not knowing where to place the enum code. I'm sorry but I'm a novice and have never even used private much less enum. Still googling for quidance, thanks. – UBel Mar 23 '12 at 13:06
  • The Enum you can put where you put all of your variable declarations for that Form. Usually at the top of the code file somewhere. In my TestApp that I created, I used Form1.vb, and my Enum was right under the `Public Class Form1` line at the top of the code file. – Code Maverick Mar 23 '12 at 14:11
  • Here's my error. argument not specified for parameter 'searchallchildren of public function find(key as string, searchallchildren as boolean) as system.windows.forms.control(). This error occurs for each of the dim lines above: gbInterior.Controls.Find("c" & type.ToString()) – UBel Mar 26 '12 at 18:59
  • See my final edit. I went ahead and wrote a test app that is fully functional. I saw the error you spoke of and another after fixing that one. It should be good to go now. My code above is inside a button click, but you can figure that out I'm sure. =D – Code Maverick Mar 27 '12 at 11:41
  • Thank you for all your time. It works perfectly. If there is a quick answer, I'd like to ask; if you didn't have one of the input elements (such as combobox) in your form, could you still use the code you provided with modifications or would you persue another method of coding? – UBel Mar 27 '12 at 12:47
  • Awesome. Glad it worked for you. As to your question, yes I would use the code I provided with a slight modification. You would simply just check the input's Nothingness before using it. I'll edit it again to show an example. – Code Maverick Mar 27 '12 at 13:30
0

If all checkboxes, text boxes and comboboxes are part of a single parent (eg. the group box) it's nearly impossible to link them together.

I think you should look at grouping each set of items within a panel:

+- Panel 1 -----------------------------------------+
| Check box 1-------- Text Box 2 ---- Combobox 1 ---|
+---------------------------------------------------+
+- Panel 2 -----------------------------------------+
| Check box 2-------- Text Box 2 ---- Combobox 2 ---|
+---------------------------------------------------+

You can then loop through all panels in the group, and for each panel loop through all the controls within:

Dim stringbld As New System.Text.StringBuilder
    Dim fullstring As String

    For Each pnl As Panel In gbInterior.Controls.OfType(Of Panel)()

        Dim pen As Boolean
        Dim color As String = ""
        Dim quantity As Integer

        For Each ctrl As Control In pnl.Controls



            If TypeOf (ctrl) Is CheckBox Then
                Dim tmpchk As CheckBox = ctrl
                pen = tmpchk.Checked
            End If

            If TypeOf (ctrl) Is ComboBox Then
                Dim tmpchk As ComboBox = ctrl
                color = tmpchk.SelectedValue
            End If

            If TypeOf (ctrl) Is TextBox Then
                Dim tmpchk As TextBox = ctrl
                If IsNumeric(tmpchk.Text) Then
                    quantity = CInt(tmpchk.Text)
                End If
            End If

        Next

        stringbld.AppendLine(pen & " " & color & " " & quantity)



    Next

    fullstring = stringbld.ToString

An alternative might be to look at using a data repeater, this has methods for getting all values:

This stackoverflow thread

Community
  • 1
  • 1
bendataclear
  • 3,802
  • 3
  • 32
  • 51