Rabu, 04 Februari 2009

Sending data/commands between forms (by using events) vb 2005


IDE : VS 2005,

posted by Pidgeon


Introduction

It is easy to access the properties of a child form directly (as shown here ). Although this is sufficient for most circumstances, there are certain scenarios where it just won’t work.

What happens if you want to keep the second form open, and access a property when a user clicks a button (e.g. Word’s Find/Replace form)?

You can use a timer (on your first form), check if the value of the second form has changed at intervals, and then, if it has, use the value. But this seems like it is going to make a lot of unnecessary calls to the second form (and a LOT of work).

A resolution for this is to overload the constructor of the second form, pass the first form as a variable to the second form, and “do whatever needs to be done with the first form” in the second form.

But, once again, this has limitations. What happens if your first form is not always a form, but a different class? Or if you want to do different “stuff” depending on what your first form is. You can write a long case statement:
VB:

Code: Select Case MyForm.GetType.FullName
Case Form1.GetType.FullName
'Do Stuff
Case ControllerClass.GetType.FullName
'Do other Stuff
End Select


C#:

Code: switch (MyForm.GetType().FullName.ToString())
{
case typeof(Form1).FullName.ToString():
{
//do stuff
break;
}
case typeof(ControllerClass).FullName.ToString()
{
//do stuff
break;
}

}

But once again, this seems like a lot of work (and it doesn’t really seem like object orientated programming).

Luckily, with .Net, there is a different way to handle this, called events.

What is an event?
I’m going to use a real life example to try and explain this (if you already know what events are, and how they are used, you can skip to the example at the end of this faq).

Let’s say that I’m in a car accident. I need to let someone know that I’m in trouble, and where I am. I don’t really care who comes and save me (as long as they are qualified). The best thing in this situation would be to phone. The operators at 999 (Editor's Note: 911 in the US) has a list paramedics in the area. When I phone them (with my problem and location), they locate the paramedics, tell them where I am, and they respond to the emergency.

An event is like the accident. It is something that happened and that someone needs to respond to.




Raising the event is when I phone 999 with my emergency.

A delegate is like the 999 operators. They know who to phone, and what information to pass to them.

An event handler is like the paramedics who respond to the event.



So what does this mean for passing information between forms? Basically, what we can do is raise an event when our information changes on our second form, and respond to that event in our first form.

Implementing Custom Events
Let’s take a look at how we do this:

First, we need to decide what information the first form is going to require (my name, the type of emergency, and my location). Microsoft suggests that we use a class that inherits from the EventArgs class. For this example I’ll only return a string:

VB:

Code: Public Class CustDataEventArgs
Inherits System.EventArgs
Private _MyValue As String
Public Property MyValue() As String
Get
Return _MyValue
End Get
Set(ByVal value As String)
_MyValue = value
End Set
End Property

Public Sub New(ByVal MyValue As String)
_MyValue = MyValue
End Sub
End Class


C#:

Code: public class CustDataEventArgs : EventArgs
{
private String _MyValue;

public String MyValue
{
get { return _MyValue; }
set { _MyValue = value; }
}

public CustDataEventArgs(String MyValue)
{
_MyValue = MyValue;
}

}

Secondly, we will have to create a delegate (the 999 operator) and an event of the delegate's type, which defines its signature. In other words, an event type of CustDataEvent (accident) might happen, and if it does, we should invoke (call) the CustDataDelegate (999 operator).

VB:

Code: Public Delegate Sub CustDataDelegate(ByVal sender As Object, _
ByVal e As CustDataEventArgs)
Public Event CustDataEvent As CustDataDelegate

C#:

Code: public delegate void CustDataDelegate(Object sender, CustDataEventArgs e);
public event CustDataDelegate CustDataEvent;

On our first form, we will need a function that responds to our event. This needs to have the same “signature” as the delegate (the paramedics need to be able to receive all the information that the 999 operators supply to them):

VB:

Code: Private Sub DoWork(ByVal sender As Object, ByVal e As Form2.CustDataEventArgs)
MsgBox("The accident happened at: " & e.MyValue)
End Sub

C#:

Code: private void DoWork(Object sender, Form2.CustDataEventArgs e)
{
MessageBox.Show("The accident happened at: " + e.MyValue);
}

Next, we need to “subscribe” to the event. To use the accident example, the 999 operators need a list of paramedics that can respond to the emergency. So we need to add our function to the list (you can put this in the first form’s load event):

VB:

Code: AddHandler oForm.CustDataEvent, New Form2.CustDataDelegate(AddressOf DoWork)

C#:

Code: oForm.CustDataEvent += new Form2.CustDataDelegate(DoWork);

This seems complicated, but all I’m really doing, is saying that should an event of type CustDataEvent happens, CustDataDelegate should call my DoWork function.

And lastly (now that everything is in place to handle our event), we need to raise our event (phone 999 if an accident happens). The best place to do this is usually in the property that you want to return from your second form to your first form:

VB:

Code: Public Property MyLocation() As String
Get
Return _MyLocation
End Get
Set(ByVal value As String)
'if the value is different
If Not value = _MyLocation Then
RaiseEvent CustDataEvent(Me, New CustDataEventArgs(value))
End If
_MyLocation = value
End Set
End Property

C#:

Code: private String _Location;

public String Location
{
get { return _Location; }
set {
if (value != _Location)
{
//code to raise the event
CustDataDelegate MyHandler = CustDataEvent;
CustDataEventArgs e = new CustDataEventArgs(value);
if (MyHandler != null)
{
MyHandler(this, e);
}
}
_Location = value;
}
}

Every time the property MyLocation changes on the second form, it will raise the CustDataEvent. CustDataDelegate will let all of its listeners (DoWork) know that the event took place, and send them an object of CustDataEventArgs.

What makes this code really great, is the ability to use this for almost anything in multiple forms. We can raise our event from form2, then hide our form, and show form1 (all of this without form2 knowing, or caring who responds to their event)

On Form2:

VB:

Code: RaiseEvent CustDataEvent(Me, New CustDataEventArgs(value))
Me.Close


C#

Code: //code to raise the event
CustDataDelegate MyHandler = CustDataEvent;
CustDataEventArgs e = new CustDataEventArgs(value);
if (MyHandler != null)
{
MyHandler(this, e);
}
this. Close ();

and on Form1:

VB:

Code: Public Sub DoWork(ByVal sender As Object, ByVal e As Form2.CustDataEventArgs)
Me.Show
MsgBox("The accident happened at: " & e.MyValue)
End Sub

C#:

Code: private void DoWork(Object sender, Form2.CustDataEventArgs e)
{
this.Show();
MessageBox.Show("The accident happened at: " + e.MyValue);
}

Complete Code:
The entire code will be (assuming that you have to forms called “form1” and “form2”, each with a button called “button1” on them):

VB:

Code: Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oForm As Form2 = New Form2()
AddHandler oForm.CustDataEvent, New Form2.CustDataDelegate(AddressOf DoWork)
oForm.Show()
End Sub
Public Sub DoWork(ByVal sender As Object, ByVal e As Form2.CustDataEventArgs)
MsgBox("The accident happened at: " & e.MyValue)
End Sub

End Class

Public Class Form2

Public Class CustDataEventArgs
Inherits System.EventArgs
Private _MyValue As String
Public Property MyValue() As String
Get
Return _MyValue
End Get
Set(ByVal value As String)
_MyValue = value
End Set
End Property

Public Sub New(ByVal MyValue As String)
_MyValue = MyValue
End Sub
End Class

Public Delegate Sub CustDataDelegate(ByVal sender As Object, ByVal e As CustDataEventArgs)

Public Event CustDataEvent As CustDataDelegate

Private _MyLocation As String
Public Property MyLocation() As String
Get
Return _MyLocation
End Get
Set(ByVal value As String)
'if the value is different
If Not value = _MyLocation Then
RaiseEvent CustDataEvent(Me, New CustDataEventArgs(value))
End If
_MyLocation = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyLocation = “London”
End Sub


End Class



C#:

Code: namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{


}
private void DoWork(Object sender, Form2.CustDataEventArgs e)
{
MessageBox.Show("The accident happened at: " + e.MyValue);
}

private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.CustDataEvent += new Form2.CustDataDelegate(DoWork);
oForm.Show();
}
}
}
namespace WindowsApplication1
{
public partial class Form2 : Form
{
public class CustDataEventArgs : EventArgs
{
private String _MyValue;

public String MyValue
{
get { return _MyValue; }
set { _MyValue = value; }
}

public CustDataEventArgs(String MyValue)
{
_MyValue = MyValue;
}

}

public delegate void CustDataDelegate(Object sender, CustDataEventArgs e);
public event CustDataDelegate CustDataEvent;

private String _Location;

public String MyLocation
{
get { return _Location; }
set {
if (value != _Location)
{
CustDataDelegate MyHandler = CustDataEvent;
CustDataEventArgs e = new CustDataEventArgs(value);
if (MyHandler != null)
{
MyHandler(this, e);
}
}
_Location = value;
this.Hide();
}
}

public Form2()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
this.MyLocation = "London";
}
}
}



source :http://www.vbcity.com

-------------------------------------------------------

Trik Gambar Bergerak

Trik Gambar-dimouse

Trik hapus pwd mysql

Trik insertin to db

Trik jadi root dilinux

Trik jam-distatus-bar

Trik Koneksi-ke database

Trik Koneksi-msql-php

Trik lihat-database-mysql

Trik membahas-fungsi-else

Trik member-area