1) Dim din klasse med WithEvents der, hvor du ønsker at fange events fra den klasse, fx Dim WithEvents myThis as clsThis 2) Deklarer public events i selve klassen, fx Public Event onSomethingHappened(parm1, parm2) 3) Brug RaiseEvent, så event'et skal fyres af, fx RaiseEvent onSomethingHappened(33, 444) 4) Event'et fanges i det modul/den klasse, som instantierede klassen - myThis vil være i dropdown'en øverst i edit-vinduet, hvorefter du kan vælge det event, som du ønsker at fange, i dette tilfælde myThis_onSomethingHappened(parm1,parm2)
Hvis du vil have et kodeeksempel, så sig til.
Synes godt om
Nybegynder
01. juni 2003 - 12:34#3
Jeg ved godt hvordan man bruger en event og raiseevent. Men når det er inde i en Collection af classer så bliver det lidt trickey. Se classer i http://exp.dk/spm/354821
hvorfor det? Der, hvor du instantierer klassen, fx Set x = New clsUserCommando skal du vel blot deklarere x med WithEvents - det har jo ikke noget med din collection at gøre? Det sker jo før du kalder col.Add funktionen med objekt-instansen...
Jeg ved ikke konkret hvordan jeg ville kode det, så hvis jeg får tid, melder jeg tilbage her.
Synes godt om
Nybegynder
03. juni 2003 - 11:23#8
Hej Olet. Jeg har fundet ud af det. Man gør det at man laver en Friend i parent classen og kalder den fra child til. Også RasieEvent in parent til hvor man vil havde den. Det man skal huske er at set sin parent = den new class. Du får dine poing. Hilsen Lars
cls Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private mvarUserName As String Private mvarUserIP As String Private mvarUserHost As String Private mvarCommando As Byte Private mvarUserPassWord As String Private WithEvents Server As CSocket Attribute Server.VB_VarHelpID = -1 Private mvarIsLogOn As Boolean Private mvarParent As col
Public Sub init(parent As col) Set mvarParent = parent End Sub
Public Property Let IsLogOn(ByVal vData As Boolean) mvarIsLogOn = vData End Property
Public Property Get IsLogOn() As Boolean IsLogOn = mvarIsLogOn End Property
Public Property Let Socket(ByVal requestID As Long) Call Server_OnConnectionRequest(requestID) End Property
Public Property Let UserPassWord(ByVal vData As String) mvarUserPassWord = vData End Property
Public Property Get UserPassWord() As String UserPassWord = mvarUserPassWord End Property
Public Property Let Commando(ByVal vData As Byte) mvarCommando = vData End Property
Public Property Get Commando() As Byte Commando = mvarCommando End Property
Public Property Let UserHost(ByVal vData As String) mvarUserHost = vData End Property
Public Property Get UserHost() As String UserHost = mvarUserHost End Property
Public Property Let UserIP(ByVal vData As String) mvarUserIP = vData End Property
Public Property Get UserIP() As String UserIP = mvarUserIP End Property
Public Property Let UserName(ByVal vData As String) mvarUserName = vData End Property
Public Property Get UserName() As String UserName = mvarUserName End Property
Private Sub Class_Initialize() Set Server = New CSocket Server.LocalPort = Port IsLogOn = False End Sub
Private Sub Class_Terminate() Set Server = Nothing End Sub
Private Sub Server_OnClose() Server.CloseSocket mvarParent.Notify_OnClose Me, Server.RemoteHostIP End Sub
Private Sub Server_OnConnectionRequest(ByVal requestID As Long) Server.Accept requestID mvarParent.Notify_OnConect Server.RemoteHostIP End Sub
Private Sub Server_OnDataArrival(ByVal bytesTotal As Long) Dim bData() As Byte Server.GetData bData(), vbArray + vbByte Me.UserIP = Server.RemoteHostIP Me.UserHost = Server.RemoteHost Call Command(bData) End Sub Private Function Command(bData() As Byte) As Boolean Dim strData As String Dim BrugerNavn As String Dim PassWord As String
Select Case bData(0) Case 0 ' Login strData = String(UBound(bData) + 1, 0) Call CopyMemory(ByVal strData, bData(0), (UBound(bData) + 1) - 0) BrugerNavn = Split(strData, Chr(0))(1) PassWord = Split(strData, Chr(0))(2) '****************************************************************** '* til test * '****************************************************************** mvarParent.Notify_OnChatData BrugerNavn + " : " + PassWord 'frmWinsock.List1.AddItem BrugerNavn + " : " + PassWord '****************************************************************** UserName = BrugerNavn UserPassWord = PassWord IsLogOn = True 'Check Brugernavn og Password Case 1 If IsLogOn Then 'din kode her Else 'Server.CloseSocket End If Case Else strData = String(UBound(bData) + 1, 0) Call CopyMemory(ByVal strData, bData(1), UBound(bData)) mvarParent.Notify_OnChatData strData End Select End Function
Col
Option Explicit Public mCol As Collection Public Event OnChatData(st As String) Public Event OnClose(nr As Integer, st As String) Public Event OnConect(st As String)
Public Function Add(NewMember As clsUser) mCol.Add NewMember NewMember.init Me End Function
Public Property Get Item(vntIndexKey As Variant) As clsUser Attribute Item.VB_UserMemId = 0 Set Item = mCol(vntIndexKey) End Property
Public Property Get Count() As Long Count = mCol.Count End Property
Public Sub Remove(vntIndexKey As Variant) mCol.Remove vntIndexKey End Sub
Public Property Get NewEnum() As IUnknown Attribute NewEnum.VB_UserMemId = -4 Attribute NewEnum.VB_MemberFlags = "40" Set NewEnum = mCol.[_NewEnum] End Property
Private Sub Class_Initialize() Set mCol = New Collection End Sub
Private Sub Class_Terminate() Set mCol = Nothing End Sub
Friend Sub Notify_OnChatData(st As String) RaiseEvent OnChatData(st) End Sub
Friend Sub Notify_OnClose(obj As clsUser, st As String) Dim i As Integer For i = 1 To mCol.Count If obj.UserIP = mCol.Item(i).UserIP Then Exit For End If Next i RaiseEvent OnClose(i, st) End Sub
Friend Sub Notify_OnConect(st As String) RaiseEvent OnConect(st) End Sub
Form Private WithEvents sjov As col Set sjov = Col Private Sub sjov_OnChatData(st As String) frmWinsock.Text1 = st End Sub
Private Sub sjov_OnClose(nr As Integer, st As String) SckCol.Remove nr frmWinsock.List1.AddItem "Close Socket : " + st End Sub
Private Sub sjov_OnConect(st As String) frmWinsock.List1.AddItem "Open Socket : " + st End Sub
Hvordan kan du så se hvilken af instanserne, som fyrer et givet event af, eller det er måske ligegyldigt?
Synes godt om
Nybegynder
04. juni 2003 - 00:34#12
Fra col raise du jo dine events som blev kandt fra cls. Det er hvis ligegyldig tor jeg nok. Det der er vigtig er at det virker og det gør det. Hilsen Lars
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.