【问题标题】:How do I loop this statement?我如何循环这个语句?
【发布时间】:2018-09-15 06:11:57
【问题描述】:

我如何遍历每个子并应用代码,而不是遍历每个私有子并编写相同的代码?

Private Sub cmdMenu1_GotFocus()
Me.cmdMenu1.BackColor = RGB(92, 131, 180)
Me.cmdMenu1.ForeColor = RGB(92, 131, 180)
End Sub
Private Sub cmdMenu1_LostFocus()
Me.cmdMenu1.BackColor = RGB(255, 255, 255)
Me.cmdMenu1.ForeColor = RGB(0, 0, 0)
End Sub
Private Sub cmdMenu2_GotFocus()
Me.cmdMenu2.BackColor = RGB(92, 131, 180)
Me.cmdMenu2.ForeColor = RGB(92, 131, 180)
End Sub
Private Sub cmdMenu2_LostFocus()
Me.cmdMenu2.BackColor = RGB(255, 255, 255)
Me.cmdMenu2.ForeColor = RGB(0, 0, 0)
End Sub
Private Sub cmdMenu3_GotFocus()
Me.cmdMenu3.BackColor = RGB(92, 131, 180)
Me.cmdMenu3.ForeColor = RGB(92, 131, 180)
End Sub
Private Sub cmdMenu3_LostFocus()
Me.cmdMenu3.BackColor = RGB(255, 255, 255)
Me.cmdMenu3.ForeColor = RGB(0, 0, 0)
End Sub
Private Sub cmdMenu4_GotFocus()
Me.cmdMenu4.BackColor = RGB(92, 131, 180)
Me.cmdMenu4.ForeColor = RGB(92, 131, 180)
End Sub
Private Sub cmdMenu4_LostFocus()
Me.cmdMenu4.BackColor = RGB(255, 255, 255)
Me.cmdMenu4.ForeColor = RGB(0, 0, 0)
End Sub
Private Sub cmdMenu5_GotFocus()
Me.cmdMenu5.BackColor = RGB(92, 131, 180)
Me.cmdMenu5.ForeColor = RGB(92, 131, 180)
End Sub
Private Sub cmdMenu5_LostFocus()
Me.cmdMenu5.BackColor = RGB(255, 255, 255)
Me.cmdMenu5.ForeColor = RGB(0, 0, 0)
End Sub

等等……

任何帮助将不胜感激。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    第一步是创建一个函数(一个函数,而不是两个)来覆盖重复。

    Private Sub ChangeMenuColour(Menu as ControlMenu, GotFocus as Boolean)
        If GotFocus then
            ControlMenu.BackColor = RGB(92, 131, 180)
            ControlMenu.ForeColor = RGB(92, 131, 180)
        Else
            ControlMenu.BackColor = RGB(255, 255, 255)
            ControlMenu.ForeColor = RGB(0, 0, 0)
        End If
    End Sub
    

    注意:将Menu 声明为正确的控件类型(类型安全),我只使用了ControlMenu,因为我不知道您使用的是哪种控件。

    现在,对于每个控件,您只需调用函数。例如:

    Private Sub cmdMenu1_LostFocus()
        ChangeMenuColour Me.cmdMenu1, False
    End Sub
    

    这第一步的好处是,如果您决定更改配色方案,您只需要编辑一个例程而不是几十个例程。

    下一步,您可以创建一个自定义类,其中包含一个成员 WithEvents,该成员还处理相应的事件。有关此示例,请参阅 Excel VBA add handler to every checkbox in form。但是,实现这一点的工作可能不仅仅是您上面的简单重复。

    使用我上面链接中的代码,根据您的示例量身定制:

    'This is in a class module called MenuHandler
    Private WithEvents menu As MSForms.ControlMenu ' substitute with your specific control
    
    Property Set TargetMenu(targetobject as ControlMenu)
        set menu = targetobject
    End Property
    
    Private Sub menu_GotFocus()
        ChangeMenuColour True
    end sub
    
    Private Sub menu_LosttFocus()
        ChangeMenuColour False
    end sub
    
    Private Sub ChangeMenuColour(GotFocus as Boolean) ' Note change in signature, change in context on how this is used
        If GotFocus then
            menu.BackColor = RGB(92, 131, 180)
            menu.ForeColor = RGB(92, 131, 180)
        Else
            menu.BackColor = RGB(255, 255, 255)
            menu.ForeColor = RGB(0, 0, 0)
        End If
    End Sub
    

    现在在您的主窗体中,您只需要将您的菜单与此处理程序相关联。您可以按照上面的链接通过集合来执行此操作,或者(如果您的菜单数量有限、有限且静态)只需拥有单个对象(下面的示例)。您可以选择在您的上下文中最有效且最容易维护的。

    ' This is in the user form
    ' Declare the handler links as module-level, not within any sub or event handler
    Private menuHandler1 as new MenuHandler
    Private menuHandler2 as new MenuHandler
    Private menuHandler3 as new MenuHandler
    
    ' The following code would go in the initialise/load event handler
        Set menuHandler1.TargetMenu = cmdMenu1 ' now you don't have to add any other focus handlers for this menu - but you can anyway
        Set menuHandler2.TargetMenu = cmdMenu2
        Set menuHandler3.TargetMenu = cmdMenu3
    

    或者

    ' This is in the user form
    ' Declare the handler links as module-level, not within any sub or event handler
    Private menuHandlerCollection as new Collection ' will store the MenuHandlers
    
    ' The following code would go in the initialise/load event handler or where you create your menus
        Dim menuHandlerInstance as MenuHandler
        ' Usually some other code here
        ' the following may be in a loop, or you might just have a list of items to add
        ' only a single instance included here as an example
        Set menuHandlerInstance = new MenuHandler
        Set menuHandlerInstance .TargetMenu = cmdMenuX ' now you don't have to add any other focus handlers for this menu
        menuHandlerCollection.Add menuHandlerInstance
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2015-07-13
      • 2014-06-08
      • 2012-08-27
      相关资源
      最近更新 更多