|
>まず、クラスモジュールから
>
>Class1のクラスモジュールに
>
>'================================================
>Event cchange(combo As Object)
>Private cmb As Class2
>Private c_controls As Collection
>'================================================
>Private Sub Class_Initialize()
> Set c_controls = New Collection
>End Sub
>'================================================
>Private Sub Class_Terminate()
> Set c_controls = Nothing
>End Sub
>'================================================
>Sub cmd_change(combo As Object)
> RaiseEvent cchange(combo)
>End Sub
>'================================================
>Sub add(obj1 As Object)
> Set cmb = New Class2
> cmb.set_controls obj1, Me
> c_controls.add cmb
>End Sub
>
>
>Class2のクラスモジュールに
>'================================================
>Private WithEvents cmd As MSForms.ComboBox
>Private pa_obj As Object
>'================================================
>Private Sub cmd_change()
> pa_obj.cmd_change cmd
>End Sub
>'================================================
>Sub set_controls(obj1 As Object, obj2 As Object)
> Set cmd = obj1
> Set pa_obj = obj2
>End Sub
>
>
>で問題のUserform1には、複数のコンボボックスと
>ラベル(Label1)は最低貼り付けておいてください。
>
>そのユーザーフォームのモジュールに
>
>'================================================
>
>Private WithEvents class_cmb As Class1
>'================================================
>Private Sub class_cmb_cchange(combo As Object)
>’このイベントプロシジャーに一括してコードが書けます
>
> Label1.Caption = "今、" & combo.Name & " を変更したよ"
>End Sub
>'================================================
'*************************************
>Private Sub CommandButton1_Click()
> Dim aaa As Object
> Set aaa = ComboBox1
> MsgBox TypeName(aaa)
>End Sub
'↑これ削除してください
'***************************************
>'================================================
>Private Sub UserForm_Initialize()
> Dim cont As Object
> Set class_cmb = New Class1
> With class_cmb
> For Each cont In Controls
> If UCase(TypeName(cont)) = UCase("combobox") Then
> With cont
> .List() = Array("a", "b", "c")
> .ListIndex = 0
> End With
> .add cont
> End If
> Next
> End With
>End Sub
>'================================================
>Private Sub UserForm_Terminate()
> Set class_cmb = Nothing
>End Sub
>
>
>最後に
>標準モジュールに
>'===============================================
>sub main()
> userform1.show
>end sub
尚、Excel2000以上にで動作可能です。
|
|