Case 0
Dim i As Long
ReDim myTb(1 To 5)
For i = 1 To 5
Set myTb(i) = New myTbClass
Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
Next
myTb(1).ck = 1
myTb(2).ck = 2
myTb(3).ck = 3
myTb(4).ck = 2
myTb(5).ck = 1
Case 1
Dim j As Long
ReDim myTb(6 To 10)
For j = 6 To 10
Set myTb(j) = New myTbClass
Set myTb(j).Tb = Me.Controls("TextBox" & CStr(j))
Next
myTb(6).ck = 1
myTb(7).ck = 2
myTb(8).ck = 3
myTb(9).ck = 2
myTb(10).ck = 1
End Select
End Sub
module1
Sub ufshow()
UserForm1.Show vbModeless
End Sub
物件類別模組→myTbClass
Option Explicit
'類別模組myTbClass
Private WithEvents myTb As MSForms.TextBox
Private myCkType As Long
Public Property Set Tb(setTb As MSForms.TextBox)
Set myTb = setTb
End Property
Public Property Get Tb() As MSForms.TextBox
End Property
Public Property Let ck(setCk As Long)
myCkType = setCk
End Property
Public Property Get ck() As Long
End Property
Private Sub myTb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case myCkType
Case 1
'拒絕數字以外的輸入
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0: Beep
End If
Case 2
'拒絕數字,"-"以外的輸入
If (KeyAscii >= Asc("0") And _
KeyAscii <= Asc("9")) Or KeyAscii = Asc("-") Then
Else
KeyAscii = 0: Beep
End If
Case 3
'拒絕數字,"英文大寫"以外的輸入
If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or _
(KeyAscii >= Asc("A") And KeyAscii <= Asc("Z")) Then
Else
KeyAscii = 0: Beep
End If
End Select
End Sub
謝謝各位大大。作者: GBKEE 時間: 2010-10-10 10:47
回復 1#dechiuan999
Private Sub UserForm_Initialize() 這是表單顯示之前的程式設定
因為 表單顯示你設定前UserForm1.MultiPage1.Value=0 (Page1) 所以只有 textbox 1 - 5 有設定到
請將Private Sub UserForm_Initialize() 名稱改成 Private Sub MultiPage1_Change() 試試看作者: dechiuan999 時間: 2010-10-10 12:01
謝謝版主大大。
小弟試著改至Private Sub MultiPage1_Change()
已達成運作。
但整個語法小弟至今還是霧裡看花,無法全然理解。
不知能否請版主大大稍作說明,也讓小弟能有所理解。
下列語法如下:
'類別模組myTbClass
Private WithEvents myTb As MSForms.TextBox
Private myCkType As Long
Public Property Set Tb(setTb As MSForms.TextBox)
Set myTb = setTb
End Property
Public Property Get Tb() As MSForms.TextBox
End Property
Public Property Let ck(setCk As Long)
myCkType = setCk
End Property
Public Property Get ck() As Long
End Property
感恩大大!作者: GBKEE 時間: 2010-10-10 18:57
回復 3#dechiuan999
Property Get 陳述式, Property Let 陳述式,Property Set 陳述式 你可詳看VBA的說明
在物件類別事件一般的使用方式
1 物件類別模組中 Public WithEvents MySh As Worksheet 設為外部可讀取的物件變數
MySh 在物件類別模組中 會有物件事件可使用 如圖:
[attach]3114[/attach]
2 Dim GetSh As NewMyClass -> 在物件裡宣告一變數 為新的物件類別模組
Set GetSh.MySh = Sheet1 將類別模組的物件GetSh變數 指定為 Sheet1
如祔件 的Private Sub CommandButton1_Click()
請看附檔
[attach]3115[/attach]作者: dechiuan999 時間: 2010-10-10 20:29