【发布时间】:2013-07-19 22:03:55
【问题描述】:
我有一堆类似构造的用户窗体,上面有许多相同的对象。对于我的标签(超过 50 个),我创建了一个 dblClick 事件以允许用户更改标题。这一切都很好,但我想知道是否有办法改进我的代码。
有没有办法避免为了处理不同对象上的相同事件而制作数十份相同代码的副本?
这是我的代码:
Private Sub Item1_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
Item1_Label.Caption = InputBox("blah?", "blah", Item1_Label.Caption)
if Item1_Label.Caption = "" Then Item1_Label.Caption = "Item 1"
End Sub
Private Sub Item2_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
Item2_Label.Caption = InputBox("blah?", "blah", Item2_Label.Caption)
if Item2_Label.Caption = "" Then Item2_Label.Caption = "Item 2"
End Sub
Private Sub Item3_Label_dblClick(ByVal Cancel as MSForms.ReturnBoolean)
Item3_Label.Caption = InputBox("blah?", "blah", Item3_Label.Caption)
if Item3_Label.Caption = "" Then Item3_Label.Caption = "Item 3"
End Sub
'etcetera etcetera
我的用户表单中有超过 50 行这样的克隆代码。我认为有更好的方法可以做到这一点,但是当我 looked 使用 Class EventHandler 时,我看不到如何将其应用于我的目的。
有没有办法防止这种重复的代码?
谢谢,
埃利亚斯
【问题讨论】:
-
您发布的链接应该适用于标签...
标签: excel vba events userform repeat