简体   繁体   中英

How can I dynamically access a method from all open forms with option strict on

So a have a multiple form application with most forms having a method recol() I want to apply an application wide theme to all my open forms. However, I also want to have strict on. I rather not have to call each form's recol() method individually.

Example of what I have now:

Public Class Theme
  Public Shared Sub RecolorAll()
        For Each win As Form In My.Application.OpenForms
            If win.Name.ToString <> meloadscreen.Name Then
                #Disable Warning BC42017 ' Late bound resolution
                    win.recol()
                #Enable Warning BC42017 ' Late bound resolution
            End If
        Next
  End Sub
End Class
Public Class ExampleForm_1
   Public Sub recol()
       BTN1.backcolor = My.Settings.examplecolor1
       BTN2.backcolor = My.Settings.examplecolor2
   End Sub
End Class

...

Public Class ExampleForm_N
   Public Sub recol()
       SomeCustomControl1.linecolor = My.Settings.examplecolor12
       BTN1.backcolor = My.Settings.examplecolor3
   End Sub
End Class

Form class names do not follow SomeName_number ie ExampleForm_1 , ExampleForm_2 … etc

If you aim to apply standard formatting (colors in your case) to specific types of controls (ie the same color in all buttons in all forms) then you could try adding an Extension to the Form type.

For example, place the following code in a module:

<System.Runtime.CompilerServices.Extension>
Public Sub ReCol(ByRef MyForm As Form)
    For Each Ctl As Control In MyForm.Controls
        If Ctl.GetType.Equals(GetType(Button)) Then
            Ctl.BackColor = My.Settings.examplecolor1
        End If
    Next
End Sub

ReCol method will then be automatically added to each form in your project and then you can modify your ReColorAll function to something like this:

 Public Shared Sub RecolorAll()
    For Each win As Form In My.Application.OpenForms
        win.ReCol()
    Next
 End Sub

You can of course easily modify this code to load specific settings for a specific control in a specific form from Settings if you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM