PDA

View Full Version : Solved: I Want Write a Class Module Code



Erdin? E. Ka
11-10-2006, 11:32 AM
Hi,

I am inexperienced about Class Modules.

I have 6 CommandButtons on UserForm1. And i want to doing something via Class Module coding to all of them.

I tried to change by click to CommandButton6 the ForeColor of first 5 CommandButtons but i couln't. Can anyone help me please?:dunno

I attached the file.

Thanks a lot.

Ken Puls
11-10-2006, 01:19 PM
Hi Erdinc,

I recently worked with Treygor on a thead where we used a class module to capture clicks on listboxes. That thread can be found here (http://www.vbaexpress.com/forum/showthread.php?t=10083). Do you think you can adapt the code? It is commented, so hopefully should provide some hints.

HTH,

Erdin? E. Ka
11-10-2006, 01:35 PM
I Ken,

I checked that thread but, as you know i am inexperinced in Class Modules. That is a little complicated for me for this time. But if i can work on some 2-3 samples i am sure that i will be much better.
At first time, even only one easy sample can be eanugh for me. If you have a free time, please give me an easy example, if it possible.

Thank you very much for your kindly helps. :( :friends:

Bob Phillips
11-10-2006, 01:35 PM
Deja Vu!

First create a class, call it 'clsUserFormEvents', and add this code



Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
msgbox mButtonGroup.Name
End Sub



Then in your userform, add this code to initialise the class



Public mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub

Norie
11-10-2006, 01:50 PM
Erdinc

See the attached file.

Andy Pope
11-10-2006, 01:52 PM
Actually your example for changing 5 things when a button is pressed is perhaps dealt better with the use of a collection rather than a class.

The ability of the class to capture events is not required as the CommandButton6 is being used.


Dim m_colButs As Collection
Private Sub CommandButton6_Click()

Dim objX As MSForms.CommandButton

For Each objX In m_colButs
objX.ForeColor = &H8080FF ' number not string
Next

End Sub

Private Sub UserForm_Initialize()

Dim lngIndex As Long

Set m_colButs = New Collection
For lngIndex = 1 To 5
m_colButs.Add Me.Controls( _
"CommandButton" & lngIndex), CStr(m_colButs.Count + 1)
Next

End Sub


The way you normally use the class and events is like this.
Class1 code.

Public WithEvents CBN As MSForms.CommandButton

Private Sub CBN_Click()

MsgBox CBN.Caption & " Button " & CBN.Name & _
" was clicked", vbInformation

End Sub

And userform code

Dim m_clsCBN(5) As Class1

Private Sub UserForm_Initialize()

Dim lngIndex As Long

For lngIndex = 1 To 5
Set m_clsCBN(lngIndex) = New Class1
Set m_clsCBN(lngIndex).CBN = _
Me.Controls("CommandButton" & lngIndex)
Next

End Sub

Now when you click 1 of the 5 buttons a msgbox is displayed.

This is slightly more complex but allows you to channel all of the events of the 5 buttons through 1 event handler.

Class1 code

Public WithEvents CBN As MSForms.CommandButton
Public Event Click()

Private Sub CBN_Click()

Set CBN.Parent.ButEvt = Me.CBN
RaiseEvent Click

End Sub


Userform code

Dim m_colButs As Collection
Public WithEvents ButEvt As MSForms.CommandButton

Private Sub ButEvt_Click()

Dim objX As Class1

For Each objX In m_colButs
objX.CBN.ForeColor = &H8080FF
Next


End Sub

Private Sub UserForm_Initialize()

Dim lngIndex As Long
Dim objX As Class1

Set m_colButs = New Collection
For lngIndex = 1 To 5
Set objX = New Class1
Set objX.CBN = Me.Controls( _
"CommandButton" & lngIndex)
m_colButs.Add objX, CStr(m_colButs.Count + 1)
Next

End Sub


Now we are using a custom event within the class. And a collection to hold the 5 buttons. When any one of the buttons is clicked it assigns the public BntEvt object to that button and raises a Click event.
Within the userform the BntEvt captures the event and then applys the changes to all the controls in the collection.

Erdin? E. Ka
11-10-2006, 04:59 PM
:clap2:
Ken, xld, Andy Pope and Norie; (http://www.vbaexpress.com/forum/member.php?u=573)

Actually i don't how should i thank you all. I am so happy with your kindly helps. These samples are really easy and understandable for me. Thank your very very very much you all.:joy:


If you want to visit Turkey, please inform me. i wish entertain you all in our Country. You are so beautiful people.

Thanks again.

:mbounce:

Bob Phillips
11-10-2006, 05:13 PM
If you want to visit Turkey, please inform me. i wish entertain you all in our Country. You are so beautiful people.

So is Turkey. I have been three times already, and I will be back one day, I love the place.

Ken Puls
11-10-2006, 05:35 PM
I haven't made it to Turkey yet, but would certainly like to. Suat (one of our board coders) lives in Istanbul as well. :)

Erdin? E. Ka
11-10-2006, 07:24 PM
So we are agree. I hope to see all of you here.
(00 90 532 592 68 90 is my mobile.)

Also, i know Mr. Suat, He is one of the coder of your board. Last month i wrote to him via private message method to meeting with him. But he don't reply to me something yet.

Anyway, it doesn't matter. I love VBAX.

See you guys...
:hi:

Bob Phillips
11-11-2006, 03:20 AM
So we are agree. I hope to see all of you here.
(00 90 532 592 68 90 is my mobile.)

I was hoping to get your area from your area code, but I see it is a mobile pone :(

Erdin? E. Ka
11-11-2006, 09:43 AM
Hi xld.

My local number is;
0090 224 366 43 80
00 international code
90 Turkey's code
224 Bursa's code
366 43 80 my home number.

But now i have to go out.

If you want to call me, you can do it at between 11:00 And 21:00 C.E.T.

See you.

Ken Puls
11-12-2006, 11:16 AM
Also, i know Mr. Suat, He is one of the coder of your board. Last month i wrote to him via private message method to meeting with him. But he don't reply to me something yet.

Erdinc,

Head over to MrExcel.com and PM him there. Suat works for MrExcel and doesn't always make it here on a regular basis. :)