Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Raise Event on another Form

Raise Event on another Form

Scheduled Pinned Locked Moved Visual Basic
question
3 Posts 3 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EngrImad
    wrote on last edited by
    #1

    Hello everyone subject is declare event on Form1 , and run that event on Form2 here is the code , but event doesnt work on Form2 !! what is missing here?? many thanks

    Public Class Form1

    Private Sub btnOpenForm2\_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
        Form2.Show()
    End Sub
    
    Public Event show\_My\_Message()
    Private Sub btnShowMessageOnForm2\_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
        RaiseEvent show\_My\_Message()
    End Sub
    

    End Class

    Public Class Form2

    Public WithEvents My\_Form1 As Form1 = New Form1
    Private Sub Show\_My\_Message\_On\_Form2() Handles My\_Form1.show\_My\_Message
        MsgBox("Hello")
    End Sub
    

    End Class

    D 1 Reply Last reply
    0
    • E EngrImad

      Hello everyone subject is declare event on Form1 , and run that event on Form2 here is the code , but event doesnt work on Form2 !! what is missing here?? many thanks

      Public Class Form1

      Private Sub btnOpenForm2\_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
          Form2.Show()
      End Sub
      
      Public Event show\_My\_Message()
      Private Sub btnShowMessageOnForm2\_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
          RaiseEvent show\_My\_Message()
      End Sub
      

      End Class

      Public Class Form2

      Public WithEvents My\_Form1 As Form1 = New Form1
      Private Sub Show\_My\_Message\_On\_Form2() Handles My\_Form1.show\_My\_Message
          MsgBox("Hello")
      End Sub
      

      End Class

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      What you're doing seems backwards, but, hey, it's your app and I don't know anything about it. Since Form2 doesn't (and SHOULDN'T!) know anything about Form1. By doing this, you forever tie both forms together making it impossible to use one of them without the other. Your Form2 code is creating a NEW INSTANCE of Form1, NOT using the existing instance! That's why it doesn't work. You do not need the "WithEvents", or even that entire line, on Form2. If Form2 needs to subscribe to events exposed by Form1, you have to pass your instance of Form1 to a constructor on Form2 so it knows which instance events to subscribe to. Then the constructor needs to "wire-up" the event handlers for whatever events it needs, manually using AddHandler. My VB.NET is really rusty, so the following may not even compile let alone run:

      Public Class Form1
      Public Event Show_My_Message(ByVal message As String)

      Private Sub btnOpenForm2\_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
          Dim form2 As New Form2(Me)         ' This passes the reference to the Form1 instance to the Form2 instance
          form2.Show()                       ' Show an instance of Form2
      End Sub
      
      Private Sub btnShowMessageOnForm2\_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
          RaiseEvent Show\_My\_Message("Some message")
      End Sub
      

      End Class

      Public Class Form2
      Public Sub New(Form1 eventProvider) ' THIS IS BAD PRACTICE! Form1 should be replaced by an interface!
      AddHandler eventProvider.Show_My_Message, AddressOf ShowMyMessageHandler
      End Sub

      Public Sub ShowMyMessageHandler(ByVal message As String)
          MsgBox(message)
      End Sub
      

      End Class

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        What you're doing seems backwards, but, hey, it's your app and I don't know anything about it. Since Form2 doesn't (and SHOULDN'T!) know anything about Form1. By doing this, you forever tie both forms together making it impossible to use one of them without the other. Your Form2 code is creating a NEW INSTANCE of Form1, NOT using the existing instance! That's why it doesn't work. You do not need the "WithEvents", or even that entire line, on Form2. If Form2 needs to subscribe to events exposed by Form1, you have to pass your instance of Form1 to a constructor on Form2 so it knows which instance events to subscribe to. Then the constructor needs to "wire-up" the event handlers for whatever events it needs, manually using AddHandler. My VB.NET is really rusty, so the following may not even compile let alone run:

        Public Class Form1
        Public Event Show_My_Message(ByVal message As String)

        Private Sub btnOpenForm2\_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
            Dim form2 As New Form2(Me)         ' This passes the reference to the Form1 instance to the Form2 instance
            form2.Show()                       ' Show an instance of Form2
        End Sub
        
        Private Sub btnShowMessageOnForm2\_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
            RaiseEvent Show\_My\_Message("Some message")
        End Sub
        

        End Class

        Public Class Form2
        Public Sub New(Form1 eventProvider) ' THIS IS BAD PRACTICE! Form1 should be replaced by an interface!
        AddHandler eventProvider.Show_My_Message, AddressOf ShowMyMessageHandler
        End Sub

        Public Sub ShowMyMessageHandler(ByVal message As String)
            MsgBox(message)
        End Sub
        

        End Class

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        J Offline
        J Offline
        jsc42
        wrote on last edited by
        #3

        I've never got along with Events - they seem overly convoluted for simple tasks. My (also) rusty VB suggests that something like the following might do what you want (it is probably overkill)

        Public Class Form1
        Private f2 As Form2 = Nothing

        Sub F2IsActive(Enabled As Boolean)
            btnStartForm2.Enabled = Not Enabled
            btnDoSomethingOnForm2.Enabled = Enabled
            btnCloseForm2.Enabled = Enabled
        
            If Enabled Then
                If Not FormIsOpen(f2) Then  ' Needs to be opened
                    f2 = New Form2()
                    f2.Show()
                End If
            ElseIf FormIsOpen(f2) Then   ' Needs to be closed
                f2.Close()
                f2 = Nothing
            End If
        End Sub
        
        Private Sub btnStartForm2\_Click(sender As Object, e As EventArgs) Handles btnStartForm2.Click
            F2IsActive(True)
        End Sub
        
        Private Sub btnDoSomethingOnForm2\_Click(sender As Object, e As EventArgs) Handles btnDoSomethingOnForm2.Click
            If f2 Is Nothing Then
                MsgBox("You have to open Form2 first")
            ElseIf FormIsOpen(f2) Then
                f2.DoSomethingOnForm2()
            Else    ' No longer exists
                MsgBox("Form2 has disappeared")
                F2IsActive(False)
            End If
        End Sub
        
        Private Function FormIsOpen(formToFind As Form) As Boolean
            For Each testform As Form In Application.OpenForms
                If testform.Equals(formToFind) Then
                    Return True
                End If
            Next
        
            Return False
        End Function
        
        Private Sub btnCloseForm2\_Click(sender As Object, e As EventArgs) Handles btnCloseForm2.Click
            If Not FormIsOpen(f2) Then
                MsgBox("Form2 has already been closed")
            End If
        
            F2IsActive(False)
        End Sub
        
        Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            F2IsActive(False)
        End Sub
        

        End Class

        Public Class Form2
        Public Sub DoSomethingOnForm2()
        MsgBox("Hello from Form 1")
        End Sub
        End Class

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups