Can't find control type from a collection of controls

Rich Goldberg 121 Reputation points
2022-05-05T16:47:43.367+00:00

I’m still new to VB.NET, and trying to find the correct way to simplify some code (with the understanding that it will add to the runtime complexity).

I’ve defined a Windows Form with a number of fields. The user enters data into those textboxes, checkboxes, etc., then hits the form’s “Search” button. My existing (working) code looks at the first input field and passes 4 (or optionally 5) parameters to my BuildSearch function, which returns an SQL Where clause segment based on that first input field. It then repeats the procedure for the remaining input fields, concatenating all of the SQL segments with “ AND ”, before submitting the request to the server.

My existing code looks like this:

If TxtSort_Key.Text <> "" Or LblSort_KeyOp.Text <> "" Then g$ = g$ & BuildSearch("Sort_Key", "", LblSort_KeyOp.Text, TxtSort_Key)   ' Sort_Key  
If TxtFullName.Text <> "" Or LblFullNameOp.Text <> "" Then g$ = g$ & BuildSearch("LastName", "", LblFullNameOp.Text, TxtFullName)   ' LastName  

There are 19 of these IF statements, one for each of the input fields on the form. The first two fields passed to BuildSearch provide text for the SQL statement. The remaining 2 parameters get text from the various controls to be used in the SQL statement.

Similar code will be used in other forms, using different field names. So rather than coding several more bunches of these IF statements for each form (and as a learning exercise), I’d like to build a Collection in each form containing the 4 (or 5) pieces of data for each input field, then generate the IF statements dynamically. Here’s what I’ve done in the first module:

Public Class FormFld  
    Public Property ScrField As Control  
    Public Property ScrLabel As Control  
    Public Property DbField As String  
    Public Property DbNumeric As Boolean  
End Class  

and in the code module:

Public Class FrmSearch  
    Private ReadOnly FormFlds As New List(Of FormFld) From  
        {  
    New FormFld With {.ScrField = TxtSort_Key, .ScrLabel = LblSort_KeyOp, .DbField = "Sort_Key", .DbNumeric = False}} ',  
    New FormFld With {.ScrField = TxtFullName, .ScrLabel = LblFullNameOp.Text, .DbField = "LastName", .DbNumeric = False},  
    ...  

I was expecting to be able to do:

For Each formfld In FormFlds  
    With formfld  

        ' Examine .ScrField to determine the type of control, then extract the proper field and determine the  
        ' proper condition to generate, e.g., TxtSort_Key.Text <> "", or ChkVolunteer.Checked…, etc.  
    End With  
Next  

But when setting a breakpoint there and looking at ScrLabel in the Locals watch, I don’t see anything that tells me the name of the control or what type of control it is:
199335-image.png

Is what I’m trying to do possible? If so, could someone give me a hint as to find the type of control? I appreciate any help you can provide.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,657 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 28,781 Reputation points Microsoft Vendor
    2022-05-06T02:08:33.763+00:00

    Hi @Rich Goldberg ,
    You can refer to the following code to determine the type of the control.

            Dim a As Control = CheckBox1  
            If TypeOf (a) Is TextBox Then  
                Console.WriteLine(a.Text)  
            ElseIf TypeOf (a) Is CheckBox Then  
                Dim b As CheckBox = a  
                Console.WriteLine(b.Text)  
            End If  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Rich Goldberg 121 Reputation points
    2022-05-06T19:28:29.537+00:00

    @Jiachen Li-MSFT : I woke up this morning, thought about the fact that the control was noted as "Nothing", and said, "I've got to move that code to after the form loads." Then I saw your response. I moved the building of the collection to the Load routine, and now it works great. Thanks for taking the time to give me two accurate responses.

    0 comments No comments