Results 1 to 9 of 9

Thread: Event handling problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    27

    Event handling problem

    i need to Invoke an event from one of my class .what i am trying to do is for each RaiseEvent the listbox should add an item .

    here is the code.

    PHP Code:
    Public Class Example
        
    Public Event StatusLog(ByVal actionName As String)
      
     
       
    Sub test()
      
    RaiseEvent StatusLog("TEST1.")
      
    RaiseEvent StatusLog("TEST2.")
      
    RaiseEvent StatusLog("TEST3.")

        
    End Sub
    End 
    Class 
    PHP Code:
     Public Class Form1

        
    Private As New Example



        
    Private WithEvents Log As Example
        
    Private Sub Log_StatusUpdate(ByVal actionName As StringHandles Log.StatusLog
            UPdatelog
    (actionName)
        
        
    End Sub




        Delegate Sub _UPdatelog
    (ByVal actionName As String)
        Private 
    Sub UPdatelog(ByVal actionName As String)
            If 
    InvokeRequired Then
                Invoke
    (New _UPdatelog(AddressOf UPdatelog), actionName)
            Else

             
                
    ListBox1.Items.Add(actionName)
            
    End If
        
    End Sub

     




        
    Private Sub Button1_Click(sender As ObjectAs EventArgsHandles Button1.Click
            T
    .test()
           
        
    End Sub 

  2. #2
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Event handling problem

    Why are you doing this using events not subs or functions ? And what isn't working /do you need help with?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Event handling problem

    1) I don't think the above code is live code, but an illustration of the problem - besides, how else should should a class report back? No one questions when the Changed event happens in a text box, but add a ReportStatus event to a class and everyone thinks you're bonkers.
    2) I don't see a question, so I'm not sure what the problem is.
    3) I don't see where Test is called, so the events will NEVER get raised. In fact you don't create an instance except for T at the top, but it never gets used either.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Event handling problem

    Quote Originally Posted by techgnome View Post
    1) I don't think the above code is live code, but an illustration of the problem - besides, how else should should a class report back? No one questions when the Changed event happens in a text box, but add a ReportStatus event to a class and everyone thinks you're bonkers.
    that's a fair point I read the thread too fast and on a iphone and missed that it was a separate class

  5. #5

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    27

    Re: Event handling problem

    what i am trying to do this is when ever i RaiseEvent StatusLog in a sub or function it should Pass that Log and update the listbox like in the example Test1, test2,test3 .

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Event handling problem

    But you never call the method "Test" ... so since you never call it, it doesn't raise the event...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Event handling problem

    You've got two fields typed as Example in your Form, T which you assign a new instance of Example to in its initialiser, and Log. You call the Test() method on T, but the Log_UpdateStatus event handler method is subscribed to the StatusLog event from the Log field.

    What you want is to have only the one field:

    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private WithEvents Log As New Example
    4.  
    5.     Private Sub Log_StatusUpdate(ByVal actionName As String) Handles Log.StatusLog
    6.         UPdatelog(actionName)
    7.     End Sub
    8.  
    9.     ' ...
    10.  
    11.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    12.         Log.test()
    13.     End Sub

    Now you should be okay, because the instance that is having its test() method called is the same instance that the event handler method is listening to.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    27

    Re: Event handling problem

    thanks M8 , now my problem is solved .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width