標題:
[發問]
請教 Label 與 Combobox 對應讀取
[打印本頁]
作者:
mark15jill
時間:
2012-12-18 12:04
標題:
請教 Label 與 Combobox 對應讀取
問題:
若未在 userform 上預先設定 label 與 combobox 兩物件的話
要如何判斷 當 combobox 數值變動 ( 如 text 從 1 變動至 2 )
label 的 caption 也跟著變動?
以下程式碼
Private Sub UserForm_Initialize()
Dim myForm1
Dim myLabel1 As MSForms.Label
Dim mycom As MSForms.ComboBox
Set mycom = UserForm1.Controls.Add("forms.combobox.1")
With mycom
.Top = 100
.Left = 10
For su = 1 To 30
.AddItem (su)
Next
End With
'動態新增Label控件
Set myLabel1 = _
UserForm1.Controls.Add("forms.Label.1")
'設置該Label的名稱、高度、寬度、位置等
With myLabel1
.Left = 10
.Top = 10
.Width = 50
.Height = 50
.BackColor = 201
.Caption = mycom.Text
End With
End Sub
複製代碼
作者:
GBKEE
時間:
2012-12-18 12:40
本帖最後由 GBKEE 於 2012-12-18 12:47 編輯
回復
1#
mark15jill
使用 [物件類別模組]
[attach]13591[/attach]
Option Explicit
Dim XClass As New Class1 '新的 [物件類別模組]
Public myLabel1 As MSForms.Label '需設為公用:物件類別模組的程式才可用到
Private Sub UserForm_Initialize()
Dim myForm1, su As Variant
'Dim myLabel1 As MSForms.Label '註解掉
Dim mycom As MSForms.ComboBox
Set mycom = UserForm1.Controls.Add("forms.combobox.1")
Set XClass.CoBoX = mycom
With mycom
.Top = 100
.Left = 10
For su = 1 To 30
.AddItem (su)
Next
End With
'動態新增Label控件
Set myLabel1 = _
UserForm1.Controls.Add("forms.Label.1")
'設置該Label的名稱、高度、寬度、位置等
With myLabel1
.Left = 10
.Top = 10
.Width = 50
.Height = 50
.ForeColor = vbWhite
.BackColor = vbBlue
.Caption = mycom.Text
End With
End Sub
複製代碼
Option Explicit
Public WithEvents CoBoX As MSForms.ComboBox
Private Sub CoBoX_Change()
UserForm1.myLabel1.Caption = CoBoX
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)