Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I have a form (form1). and 3 textboxes on it. txtname, txtadres, txtid. I also have an object "cust" from my customer class. cust has the properties. cust.name, cust.adres, cust.id .

 

I want to create an event(or events) that when the value of cust.name or cust.adres or cust.id changes.... that those values are put in to txtname.text or txtadres.text or txtid.text .

 

can anyone show me how to create this event ?

 

thx,

Posted

1. Create a event for each property and catch the event in the form

 

Public Class Customer

 

Public Event propertyChanged As EventHandler

 

Private _name As String

 

Public Property Name() As String

Get

Return Me._name

End Get

Set(ByVal Value As String)

Me._name = Value

RaiseEvent propertyChanged(Me._name, New EventArgs)

End Set

End Property

 

End Class

 

 

In Form:

 

Private MyCustomer As Customer

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.MyCustomer = New Customer

AddHandler Me.MyCustomer.propertyChanged, AddressOf CustomerChanged

End Sub

 

Private Sub CustomerChanged(ByVal sender As Object, ByVal e As EventArgs)

Me.customername.Text = sender.ToString()

End Sub

 

2. Create an Event Argument class that passes the name of the field modified, catch the event in the form and enumerate the property modified

 

 

Public Delegate Sub ChangedEventHandler(ByVal sender As Object, ByVal e As CustomerEventArgs)

 

Public Class Customer

 

Public Event propertyChanged As ChangedEventHandler

 

Private _name As String

 

Public Property Name() As String

Get

Return Me._name

End Get

Set(ByVal Value As String)

Me._name = Value

RaiseEvent propertyChanged(Me._name, New CustomerEventArgs("Name"))

End Set

End Property

 

End Class

 

 

Public Class CustomerEventArgs

Inherits EventArgs

 

Private _customerField As String

 

Public Property CustomerField() As String

Get

Return Me._customerField

End Get

Set(ByVal Value As String)

Me._customerField = Value

End Set

End Property

 

Public Sub New(ByVal field As String)

Me._customerField = field

End Sub

 

 

In Form:

 

'Form_Load is same as previous

 

Private Sub CustomerChanged(ByVal sender As Object, ByVal e As CustomerEventArgs)

 

Select Case (e.CustomerField.ToLower())

Case "name" : Me.customername.Text = sender.ToString()

End Select

End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...