我試圖在用戶表單中創建一個私有程序,該程序可以更改特定框架中標簽的邊框樣式。
Private Sub ShowBorder(LabelName As String, frame As MSForms.frame)
Dim ctr As MSForms.Control
Debug.Print frame.Controls.Count ' returns 7 which is correct as thers are only 7 labels in this frame
For Each ctr In frame.Controls
If TypeName(ctr) = "Label" And ctr.Tag = "BorderShow" And ctr.Name = LabelName Then
'change its borderstyle to 1
Else
'change its borderstyle to 0
End If
Next ctr
End Sub
我已經測驗了回圈實際上回圈沒有任何錯誤,但我只是不知道如何設定它的屬性。ctr.Intellisense 中沒有 Borderstyle。我在這段代碼中遺漏了什么嗎?謝謝您的幫助。
編輯:我應該提到我在標簽滑鼠移動事件上呼叫了這個程序。
uj5u.com熱心網友回復:
您可以使用 Label 物件的 BorderStyle 屬性...
Private Sub ShowBorder(LabelName As String, frame As MSForms.frame)
Dim ctr As MSForms.Control
Debug.Print frame.Controls.Count ' returns 7 which is correct as thers are only 7 labels in this frame
For Each ctr In frame.Controls
If TypeName(ctr) = "Label" And ctr.Tag = "BorderShow" And ctr.Name = LabelName Then
'change its borderstyle to 1
ctr.BorderStyle = fmBorderStyleSingle
Else
'change its borderstyle to 0
ctr.BorderStyle = fmBorderStyleNone
End If
Next ctr
End Sub
請注意,IntelliSense 在這種情況下不可用,因為ctr它被宣告為通用物件而不是標簽。為了使其可用,您可以分配ctr給已宣告為標簽的變數,正如@Shrotter 所建議的那樣。
Private Sub ShowBorder(LabelName As String, frame As MSForms.frame)
Dim ctr As MSForms.Control
Dim lbl As MSForms.Label
Debug.Print frame.Controls.Count ' returns 7 which is correct as thers are only 7 labels in this frame
For Each ctr In frame.Controls
If TypeName(ctr) = "Label" Then
Set lbl = ctr
If lbl.Tag = "BorderShow" And lbl.Name = LabelName Then
'change its borderstyle to 1
lbl.BorderStyle = fmBorderStyleSingle
Else
'change its borderstyle to 0
lbl.BorderStyle = fmBorderStyleNone
End If
End If
Next ctr
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515451.html
標籤:擅长vba循环用户表单
下一篇:合并檔案夾VBA中的所有作業表
