Resolved How to Search and find control with the .tag (over all forms in project), show control flashing on related form.

dbtryout

Member
Joined
Nov 14, 2018
Messages
14
Programming Experience
Beginner
Is this possible at runtime?

People ask me why you want to search in forms which aren't opened.

Simple... Each control, in my case 'buttons' are pointing out a detail of some kind. The detail is written in every button tag.
I want to be able to write the detail i'm looking for in a textbox, when i press a search button:
the program will then look through all of the button.tags in my project over all forms and brings up the form and button i was searching for.

Is this possible at runtime?
 
Problem fixed!
VB.NET:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim asm = System.Reflection.Assembly.GetExecutingAssembly
        Dim myTypes As Type() = asm.GetTypes()
        Dim frm As Form
        For Each t As Type In myTypes
            If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) Then
                frm = CType(Activator.CreateInstance(t), Form)

                For Each ctrl As Control In frm.Controls
                    Dim Textbox As String
                    Textbox = TextBox3.Text

                    If TypeOf ctrl Is Button And ctrl.Tag IsNot Nothing AndAlso ctrl.Tag.ToString.Contains(Textbox) Then
                        Dim a = DirectCast(ctrl, Button)
                        a.BackColor = Color.FromArgb(150, Color.Yellow)
                        a.Enabled = False

                        Dim myForm As Form = a.FindForm()
                        myForm.Show()
                        myForm.Text = Textbox & " CONTROL FOUND"
                        myForm.ControlBox = True
                    ElseIf TypeOf ctrl Is Button Then
                        Dim b = DirectCast(ctrl, Button)
                        b.Visible = False
                    End If
                    For Each k In frm.Controls
                        If TypeOf k Is Label Then
                            Dim f = DirectCast(k, Label)
                            f.Visible = False
                        End If

                    Next
                Next
            End If

        Next
    End Sub
 
You can clean that code up a bit, and can also use the My.Forms object to loop the forms of the project. This has the difference that while your code creates new form instances for each search, the My.Forms only returns a single instance. This also means that if found form is not closed between searches the found buttons also need to be shown since they may be hidden last time.
VB.NET:
For Each prop In GetType(My.MyProject.MyForms).GetProperties()
    Dim frm = CType(prop.GetValue(My.Forms), Form)
    If frm Is Me Then Continue For 'exclude search form
    For Each btn In frm.Controls.OfType(Of Button)
        If btn.Tag?.ToString.Contains(SearchTextBox.Text) Then
            btn.BackColor = Color.Yellow
            btn.Enabled = False
            btn.Show()
            frm.Show()
        Else
            btn.Hide()
        End If
    Next
    For Each lbl In frm.Controls.OfType(Of Label)
        lbl.Hide()
    Next
Next
Cleanups here include:
  • .OfType extension to loop only controls of that type, loop variable type is inferred
  • ?. null conditional operator to check for nothing before accessing member
  • using the existing form reference instead of another variable and accessing it from the control
If you wanted just a single loop over form controls that could still be done much simpler, this would probably have no impact on performance though.
VB.NET:
For Each ctrl As Control In frm.Controls
    If TypeOf ctrl Is Button Then
        If ctrl.Tag?.ToString.Contains(SearchTextBox.Text) Then
            ctrl.BackColor = Color.Yellow
            ctrl.Enabled = False
            ctrl.Show()
            frm.Show()
        Else
            ctrl.Hide()
        End If
    ElseIf TypeOf ctrl Is label Then
        ctrl.Hide()
    End If
Next
You see also here the control type casts is not needed since these members are common to all controls.
 
Back
Top