• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Sending a message or posting a message

Hi

Can Postmessage and SendMessage be used in Visual Basic.Net to send messages to trigger user defined events? If so, can somebody give examples of how they are implemented?

Issac
0
IssacJones
Asked:
IssacJones
  • 4
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you elaborate?

Are you sending messages to your app?...an external app?

Details...
0
 
IssacJonesAuthor Commented:
Sending messages to the same application.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Messages can be processed by overriding WndProc().  This is easy for the main window (form).  For other controls you can use NativeWindow or Inherit from that type of control.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What kind of "user defined events" are you trying to trigger?
0
 
IssacJonesAuthor Commented:
I come from a C++/MFC background so trying to implement something like this:

PostMessage( myhandle, WM_USER_MESSAGE1, (WPARAM) NULL, (LPARAM) NULL );

for

      ON_REGISTERED_MESSAGE( WM_USER_MESSAGE1, &CMyDlg::OnMessage1 )

LRESULT CMyDlg::OnMessage1( WPARAM wParam, LPARAM lParam )
{
      SendMessage( WM_CLOSE );

      return 0;
}

In doing this, messages can be sent from various classes (e.g. different dialogs or from a View to a dialog) and events can be called. I'd like to reproduce this in VB.net

Issac
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"messages can be sent from various classes (e.g. different dialogs or from a View to a dialog) and events can be called"

You don't need to go that low level in .Net.  MFC is preventing you from seeing the "new" way...  =)

Just declare a custom event and wire it up using AddHandler.  To fire it, use RaiseEvent.

Here's a simple example:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Button1.Enabled = False

        Dim F2 As New Form2
        AddHandler F2.Message, AddressOf Me.F2_Message
        AddHandler F2.FormClosed, AddressOf Me.F2_FormClosed
        F2.Show()
    End Sub

    Private Sub F2_Message(ByVal msg As String)
        Me.Label1.Text = msg
    End Sub

    Private Sub F2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        Me.Button1.Enabled = True
    End Sub

End Class

Public Class Form2

    Public Event Message(ByVal msg As String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent Message(Me.TextBox1.Text)
    End Sub

End Class

Open in new window


When the Button on Form1 is pressed, Form2 is opened up.
When the Button2 on Form2 is pressed the custom event is raised and the value in the TextBox of Form2 gets passed to the Label in Form1.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now