类模块中的Click事件未触发 [英] Click Event in Class Module Not Firing

查看:117
本文介绍了类模块中的Click事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表单,该表单显示逐行验证错误(在文本框中),我想用用户表单标签作为超链接来补充,用户可以单击链接以直接单击出现问题的单元格。



我有一些代码可以动态建立标签,并通过类模块添加了click事件,但是我无法在类模块中触发click事件。



我确实从运行中的代码中修改了此代码,该代码可快速构建这种类型的标签和click事件,但是该代码在用户窗体启动时加载标签,并将每个类对象放入集合中。我不知道是否有必要将其内置到我的解决方案中,但是我玩了一下却无法使其正常工作。



这是我放置标签的程序如果需要,请在用户表单上显示。如果需要验证,它将在另一个过程中运行。如果需要验证,则显示用户表单,并填写消息(以及现在创建的一个标签)。

 子PlaceLinkLabel(以字符串形式表示,以字符串形式表示,以字符串形式表示范围)

Dim lblNew作为MSForms.Label
设置lblNew = frmValidationMessage.Controls.Add(bstrProgID:= Forms.Label。 1,名称:=说什么,可见:= True)

和lblNew
和.Font
.Size = 10
.Name = Comic Sans MS


结尾。标题= SayWhat
.Top = 55
.Height = 15
.Left = 465
.Width = 100
结尾为

Dim clsLabel作为UserFormLabelLinks
设置clsLabel =新的UserFormLabelLinks
设置clsLabel.lbl = lblNew

使用clsLabel
.WhichRange = WhereRange
.WhichSheet = whichSheet

结尾
'不确定是否需要这样做
'将pLabels作为集合
'设置pLabels =新集合
'pLabels。添加clsLabel

结束子

这里是 UserFormLabelLinks 类模块:

  Option Explicit 

私有WithEvents pLabel作为MSForms.Label
私有sWhichRange作为字符串
私有sWhichSheet作为String

公共属性集lbl(value as MSForms.Label)
Set pLabel = value
结束属性

公共属性Get WhereSheet()作为字符串
WhereSheet = sWhichSheet
End属性

公共属性Let WhoSheet(值作为字符串)
sWhichSheet =值
结束属性

公共属性Get WhereRange()作为字符串
WhoRange = sWhichRange
最终财产

公共财产让WhereRange(值作为字符串)
sWhichRange =值
最终财产


Private Sub pLabel_Click()

MsgBox hi'当我单击标签时,此doe不会触发

’Application.Goto ThisWorkbook.Worksheets(WhichSheet).Range(WhichRange),True
’ActiveWorkbook.FollowHyperlink(#&哪张纸! & whichRange)

End Sub


解决方案

PlaceLinkLabel 退出后, MSForms.Label 对象就会超出范围,<$ c $也会退出c> UserFormLabelLinks 对象引用;因此,您正在创建标签,但这是即弃即忘的事情,一旦 End Sub 被设置,您将无法通过编程方式访问



您需要一个私有字段来保留 UserFormLabelLinks 对象引用(并因此通过封装的 pLabel 字段保留 MSForms.Label 引用):

 选项显式
私人clsLabel作为UserFormLabelLinks

然后在过程中删除此行:


 将clsLabel用作UserFormLabelLinks 


换句话说,将该局部变量提升为一个字段,以保留该变量该过程完成后。


I have a user form that displays line-by-line validation errors (in text box) that I want to supplement with user form labels that act as hyperlinks that users can click to go directly to the cell with issues.

I have code that builds labels on the fly and have added a click event through class modules but I cannot get it the click event in the class module to fire.

I did modify this code from working code that builds this type of label and click event on the fly, but that code loads labels at userform initiation and places each class object into a collection. I don't know if that is necessary to build into my solution, but I played with it and could not get it to work.

Here is my procedure to place label on the userform if needed. It runs inside another procedure if validation is needed. Userform is than shown, filled out with message (and this one label that gets created for now), if validation is needed.

Sub PlaceLinkLabel(SayWhat As String, WhichSheet As String, WhichRange As String)

    Dim lblNew As MSForms.Label
    Set lblNew = frmValidationMessage.Controls.Add(bstrProgID:="Forms.Label.1", Name:=SayWhat, Visible:=True)

    With lblNew
        With .Font
            .Size = 10
            .Name = "Comic Sans MS"
        End With

        .Caption = SayWhat
        .Top = 55
        .Height = 15
        .Left = 465
        .Width = 100
    End With

    Dim clsLabel As UserFormLabelLinks
    Set clsLabel = New UserFormLabelLinks
    Set clsLabel.lbl = lblNew

    With clsLabel
        .WhichRange = WhichRange
        .WhichSheet = WhichSheet
    End With

    'not sure if this is needed or not
    'Dim pLabels As Collection
    'Set pLabels = New Collection
    'pLabels.Add clsLabel

End Sub

Here is UserFormLabelLinks class module:

Option Explicit

Private WithEvents pLabel As MSForms.Label
Private sWhichRange As String
Private sWhichSheet As String

Public Property Set lbl(value As MSForms.Label)
    Set pLabel = value
End Property

Public Property Get WhichSheet() As String
    WhichSheet = sWhichSheet
End Property

Public Property Let WhichSheet(value As String)
    sWhichSheet = value
End Property

Public Property Get WhichRange() As String
    WhichRange = sWhichRange
End Property

Public Property Let WhichRange(value As String)
    sWhichRange = value
End Property


Private Sub pLabel_Click()

    MsgBox "hi" 'when i click label, this does not fire

    'Application.Goto ThisWorkbook.Worksheets(WhichSheet).Range(WhichRange), True
    'ActiveWorkbook.FollowHyperlink ("#" & WhichSheet & "!" & WhichRange)

End Sub

解决方案

The MSForms.Label object is going out of scope as soon as PlaceLinkLabel exits, as does the UserFormLabelLinks object reference; thus you're creating a label, but it's a fire-and-forget thing that you can't programmatically access as soon as End Sub is reached, hence the events never fire.

You need a private field to hold on to the UserFormLabelLinks object reference (and thus keep the MSForms.Label reference around via the encapsulated pLabel field):

Option Explicit
Private clsLabel As UserFormLabelLinks

Then remove this line in the procedure:

Dim clsLabel As UserFormLabelLinks

In other words, promote that local variable to a field, to keep it around after the procedure has completed.

这篇关于类模块中的Click事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆