This project has moved. For the latest updates, please go here.

Raising Event VB.NET

Topics: Getting Started
Nov 25, 2014 at 6:02 AM
I seem to be having a hard time raising an event in a vb.net application from the IMidiDataReceiver interface. Any suggestions?
Imports CannedBytes.Midi

Public Class Form1
    Public WithEvents obj As MyReceiver
    Dim midiIn = New MidiInPort()

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        midiIn.Stop()
        midiIn.Close()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        midiIn.Successor = New MyReceiver
        midiIn.Open(0)
        midiIn.Start()
    End Sub

    Private Sub event1() Handles obj.event1
        MessageBox.Show("Event1 Triggered")
    End Sub
End Class

Public Class MyReceiver
    Implements IMidiDataReceiver
    Public Event event1()

    Private Sub LongData(buffer As MidiBufferStream, timestamp As Long) Implements IMidiDataReceiver.LongData
    End Sub

    Private Sub ShortData(data As Integer, timestamp As Long) Implements IMidiDataReceiver.ShortData
        Console.WriteLine("Data Recieved - " & data)
        RaiseEvent event1()
    End Sub
End Class
Coordinator
Nov 25, 2014 at 12:54 PM
I do not know VB very well, but I see you maintain a form instance member obj (MyReceiver) for wich you handle the event. But you assign a new instance directly to the midi port. So if you assign the new instance to your obj member and assign that to the midi port, it will probably work...

[2c]
Marc
Nov 25, 2014 at 2:54 PM
Thank you. I had briefly thought something like that. But your reply really helped me target the problem. Was able to solve changing only 2 lines! Much appreciated.
Imports CannedBytes.Midi

Public Class Form1
    Public WithEvents obj As New MyReceiver     'EDITED
    Dim midiIn = New MidiInPort()

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        midiIn.Stop()
        midiIn.Close()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        midiIn.Successor = obj     'EDITED
        midiIn.Open(0)
        midiIn.Start()
    End Sub

    Private Sub event1() Handles obj.event1
        MessageBox.Show("Event1 Triggered")
    End Sub
End Class

Public Class MyReceiver
    Implements IMidiDataReceiver
    Public Event event1()

    Private Sub LongData(buffer As MidiBufferStream, timestamp As Long) Implements IMidiDataReceiver.LongData
    End Sub

    Private Sub ShortData(data As Integer, timestamp As Long) Implements IMidiDataReceiver.ShortData
        Console.WriteLine("Data Recieved - " & data)
        RaiseEvent event1()
    End Sub
End Class