• (3 Pages)
  • +
  • 1
  • 2
  • 3

Passing Data Between Forms Rate Topic: ***** 2 Votes

#31 AmrAly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-November 17

Posted 02 November 2017 - 03:04 AM

View Postandrewsw, on 28 October 2013 - 01:20 PM, said:

I will add the following as a note as I am happy with my tutorial content as it stands.

An alternative way to provide the onpropertychanged helper method in the last (additional) example is:

    ' This method is called by the Set accessor of each property. 
    ' The CallerMemberName attribute that is applied to the optional propertyName 
    ' parameter causes the property name of the caller to be substituted as an argument. 
    Private Sub onpropertychanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

See the example at INotifyPropertyChanged :MSDN. This requires this import:

Imports System.Runtime.CompilerServices

in the Person Class.

This means that we can call onpropertychanged without supplying the property-name, as it will be supplied by the CallerMemberName() attribute.

This is interesting, but not essential.

I want continue reading this article but .....this is my problem

[/quote]
I want continue reading this article but .....this is my problem
[/quote]
Was This Post Helpful? 0
  • +
  • -

#32 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,917
  • Joined: 12-December 12

Posted 02 November 2017 - 03:26 AM

What problem?
Was This Post Helpful? 0
  • +
  • -

#33 AmrAly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-November 17

Posted 02 November 2017 - 04:16 AM

The person class code didn't appear and also other codes below i don't know why any help in order to completing this helpful topic
Really i need it to enhance the opening of my second form
Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#34 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,917
  • Joined: 12-December 12

Posted 02 November 2017 - 04:33 AM

I see what you mean, the Spoiler tags aren't working/expanding on the site. I've copied the hidden code, expanded, below:

I apologise for this failure of the site.

Public Class Person

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property
    Property LastName As String     'auto-implemented
    Property Address As String = String.Empty   'auto-implemented with default

    Public Sub New()
        ' TODO: Complete member initialization 
    End Sub

    Sub New(ByVal first As String, ByVal last As String, ByVal address As String)
        Me.FirstName = first
        Me.LastName = last
        Me.Address = address
    End Sub

End Class


(This Class demonstrates three different ways to define Properties.)

Create a new form named frmChildProperty. Add a Button to the main form named btnShowChildProperty.

Modify the code of the main form to include the following.

Public Class frmMain
    Private thePerson As Person
    Private frmProp As frmChildProperty

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        thePerson = New Person With {.FirstName = "Bob", .LastName = "Biddle"}
        txtFirstName.Text = thePerson.FirstName
        txtLastName.Text = thePerson.LastName
    End Sub

    Private Sub btnShowChildProperty_Click(sender As Object, e As EventArgs) Handles btnShowChildProperty.Click
        frmProp = New frmChildProperty()
        frmProp.ThePerson = Me.thePerson
        frmProp.Show(Me)
    End Sub


I haven't added any Controls to frmChildProperty, just the following code.

Public Class frmChildProperty
    Private _Person As Person
    Public Property ThePerson() As Person
        Get
            Return _Person
        End Get
        Set(ByVal value As Person)
            _Person = value
        End Set
    End Property

    Private Sub frmChildProperty_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not ThePerson Is Nothing Then
            MessageBox.Show("Person received:" & vbCrLf & ThePerson.FirstName & " " & ThePerson.LastName)
        End If
    End Sub
End Class



Code for frmConstructor:
Public Class frmConstructor
    Private _UserName As String
    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
        End Set
    End Property

    Public Sub New(ByVal first As String, ByVal last As String)
        InitializeComponent()       'required
        Me.UserName = first & " " & last
    End Sub

    Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        MessageBox.Show("The username is " & Me.UserName)
    End Sub
End Class



Public Class frmMain
    Private WithEvents frmCls As frmClosure
    'WithEvents: indicate that we want to receive events raised by this object

    Private Sub btnClosure_Click(sender As Object, e As EventArgs) Handles btnClosure.Click
        frmCls = New frmClosure()
        AddHandler frmCls.FormClosing, AddressOf ItIsClosing
        AddHandler frmCls.FormClosed, AddressOf ItIsClosed
        frmCls.Show(Me)
    End Sub

    Private Sub ItIsClosing(sender As Object, e As FormClosingEventArgs)
        If frmCls.Successful Then
            MessageBox.Show("Form was successful!")
        End If
        RemoveHandler frmCls.FormClosing, AddressOf ItIsClosing
    End Sub

    Private Sub ItIsClosed(sender As Object, e As FormClosedEventArgs)
        RemoveHandler frmCls.FormClosed, AddressOf ItIsClosed
        frmCls = Nothing
    End Sub


Many handlers can be added to the same event (cf. multi-cast delegates) so each AddHandler should have a corresponding RemoveHandler. However, providing you take care, and dispose of the form (by setting the form-reference to Nothing) when it is no longer needed then this needn't be too much of a concern.

Code for frmClosure:
Public Class frmClosure 
    Private _Successful As Boolean
    Public Property Successful() As Boolean
        Get
            Return _Successful
        End Get
        Set(ByVal value As Boolean)
            _Successful = value
        End Set
    End Property

    Private Sub btnYes_Click(sender As Object, e As EventArgs) Handles btnYes.Click
        Me.Successful = True
        Me.Close()
    End Sub

    Private Sub btnNo_Click(sender As Object, e As EventArgs) Handles btnNo.Click
        Me.Successful = False
        Me.Close()
    End Sub

    Private Sub frmClosure_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Successful = False
    End Sub
End Class



New code for the main form:
    Private WithEvents frmColour As frmChildColour

    Private Sub btnShowChildColour_Click(sender As Object, e As EventArgs) Handles btnShowChildColour.Click
        frmColour = New frmChildColour()
        frmColour.Show()
    End Sub

    Private Sub OnColourChange(ByVal chosenColour As Colour) Handles frmColour.ColourChanged
        Me.BackColor = chosenColour.ColourValue
    End Sub


Code for frmChildColour:
Public Class frmChildColour
    Dim Colours() As Colour = {New Colour With {.ColourName = "Red", .ColourValue = Color.Red},
                               New Colour With {.ColourName = "Green", .ColourValue = Color.Green},
                               New Colour With {.ColourName = "Blue", .ColourValue = Color.Blue}}

    Public Event ColourChanged(ByVal chosenColour As Colour)

    Private Sub frmChild_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each clr In Colours
            lstColours.Items.Add(clr)
        Next
    End Sub

    Private Sub lstColours_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstColours.SelectedIndexChanged
        RaiseEvent ColourChanged(lstColours.SelectedItem)
    End Sub
End Class



Change the Person.vb file to the following.

Imports System.ComponentModel

Public Class Person : Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, _
            e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    'helper method
    Private Sub onpropertychanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property
    Property LastName As String     'auto-implemented
    Property Address As String = String.Empty   'auto-implemented with default

    Private _Salary As Decimal
    Public Property Salary() As Decimal
        Get
            Return _Salary
        End Get
        Set(ByVal value As Decimal)
            _Salary = value
            Me.onpropertychanged("Salary")
        End Set
    End Property

    Public Sub New()
        ' TODO: Complete member initialization 
    End Sub

    Sub New(ByVal first As String, ByVal last As String, _
                   ByVal address As String, Optional ByVal sal As Decimal = 0.0)
        Me.FirstName = first
        Me.LastName = last
        Me.Address = address
        Me.Salary = sal
    End Sub

End Class


This implements the INotifyPropertyChanged interface. This requires the PropertyChanged Event. The onpropertychanged procedure is just a helper method that raises the PropertyChanged event. It is not essential but gives us the opportunity to perform some last checks, and to tailor the arguments supplied to the event, before eventually firing the event.

Changes to the main form:
Public Class frmMain
    Private WithEvents thePerson As Person
    Private frmPerson As frmChildCtor

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        thePerson = New Person With {.FirstName = "Bob", .LastName = "Biddle"}
        txtFirstName.Text = thePerson.FirstName
        txtLastName.Text = thePerson.LastName
    End Sub

Private Sub txtFirstName_TextChanged(sender As Object, e As EventArgs) Handles txtFirstName.TextChanged
        thePerson.FirstName = txtFirstName.Text
    End Sub

    Private Sub txtLastName_TextChanged(sender As Object, e As EventArgs) Handles txtLastName.TextChanged
        thePerson.LastName = txtLastName.Text
    End Sub

    Private Sub txtAddress_TextChanged(sender As Object, e As EventArgs) Handles txtAddress.TextChanged
        thePerson.Address = txtAddress.Text
    End Sub

    Private Sub btnChildShare_Click(sender As Object, e As EventArgs) Handles btnChildShare.Click
        If frmPerson Is Nothing Then
            frmPerson = New frmChildCtor(thePerson)     'share the person instance
            AddHandler thePerson.PropertyChanged, AddressOf ChangedSalary
            frmPerson.Show(Me)
        End If
    End Sub

    Private Sub ChangedSalary(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
        If e.PropertyName = "Salary" Then
            lblSalary.Text = String.Format("{0:C}", thePerson.Salary)
        End If
    End Sub



Code for the frmChildCtor:
Public Class frmChildCtor
    Public WithEvents ThePerson As Person

    Public Sub New(ByRef pers As Person)
        InitializeComponent()
        Me.ThePerson = pers
        Me.txtFirstName.Enabled = False
        Me.txtLastName.Enabled = False
        Me.txtAddress.Enabled = False
        Me.btnChange.Enabled = False
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmChildCtor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not ThePerson Is Nothing Then
            txtFirstName.Text = ThePerson.FirstName
            txtLastName.Text = ThePerson.LastName
            txtAddress.Text = ThePerson.Address
        End If
    End Sub

    Private Sub txtSalary_TextChanged(sender As Object, e As EventArgs) Handles txtSalary.TextChanged
        Dim decSalary As Decimal
        If Decimal.TryParse(txtSalary.Text, decSalary) Then
            ThePerson.Salary = decSalary
        End If
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#35 AmrAly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-November 17

Posted 02 November 2017 - 04:38 AM

Thanks Andrewsw ,
If i faced any problem in this thread i will tell you
Thanks again for fast reply
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3