Search code examples
vb.netselectdatepickertextboxnumericupdown

Check if multiple control types are empty inside groupbox and display tags of empty controls


As mentioned i need to check if all fields in a groupbox are empty and if they are then display a messagebox showing the tags of the empty fields.

Issue is i have multiple types of controls on this box including: TextBox, NumericUpDown and DatePicker.

I am wondering if there is an easier way than what i am doing below.

        If BenchQtyCombo.Text = 1 Then
            Dim B1emptyTextBoxes =
            From txt In Bench1_Combo.Controls.OfType(Of TextBox)()
            Where txt.Text.Length = 0
            Select txt.Tag

            Dim B1emptyNumericBox =
            From num In Bench1_Combo.Controls.OfType(Of NumericUpDown)()
            Where num.Value = 0
            Select num.Tag

            If B1emptyTextBoxes.Any Or B1emptyNumericBox.Any Then
                MessageBox.Show(String.Format("Please Complete the Following Required Fields:" & vbCrLf & vbCrLf & "{0}", String.Join(", ", B1emptyTextBoxes, B1emptyNumericBox)))

            End If
        End If

Im also having an issue with B1emptyTextBoxes, B1emptyNumericBox displaying the tags in the messagebox as i get this string instead of the tags

Im yet to include the code for the datepicker which will be something like Where DatePicker.Date > Today code yet until i get this working.

Any suggestions would be greatly appreciated.


Solution

  • To begin with, a group box's text property can never equal 1. 1 is a numeric type. Normally a text property contains a string.

    If GroupBox1.Text = "1" Then
    

    Next, a .Tag property is of datatype Object. You can throw anything you want into it. However, you will get an object when you reference it. When you call .ToString on an Object (which is what your String.Format is doing) you will get the fully qualified name of the Object.

    The CStr() changes it back to the underlying type String. If you hold your cursor over B1emptyTextBoxes and B1emptyNumericBox befor adding the CStr() you will see that the datatype is IEnumerable of Object. After adding CStr() you will see IEnumerable of String.

    Then add the 2 IEnumerables together with the Concat() method and Bob's your uncle.

    Private Sub OPCode()
        If GroupBox1.Text = "GroupBox1" Then
            Dim B1emptyTextBoxes = From txt In GroupBox1.Controls.OfType(Of TextBox)()
                                   Where txt.Text.Length = 0
                                   Select CStr(txt.Tag)
    
            Dim B1emptyNumericBox = From num In GroupBox1.Controls.OfType(Of NumericUpDown)()
                                    Where num.Value = 0
                                    Select CStr(num.Tag)
    
    
            If B1emptyTextBoxes.Any Or B1emptyNumericBox.Any Then
                Dim BothEnumerables = B1emptyTextBoxes.Select(Function(s) s).Concat(B1emptyNumericBox.Select(Function(s) s))
                MessageBox.Show(String.Format("Please Complete the Following Required Fields:" & vbCrLf & vbCrLf & "{0}", String.Join(", ", BothEnumerables)))
            End If
        End If
    End Sub