Accessing Custom Properties in Forms -- Parent and Child ? 
Author Message
 Accessing Custom Properties in Forms -- Parent and Child ?

Note: The one block of code below contains
two forms.  It is "plug-n-play".  

Create a new WinForm project, delete all
the code that is generated and then cut
and paste the code below into the editor
and press F5 - presto, the form should
appear.  Now for the question or problem.

' --------------------------------------
' I have two forms, Form1 and Form2.  Form2
' is invoked by Form1.  
'
' I wish to access custom properties in
' Form2 (PhoneNbr) from Form1 and access
' custom properties in Form1 (ZipCode) from
' Form2.
'
' Can someone show me what needs to be done
' to establish this communications between
' the two forms.

Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms

Public Class Form1

    Inherits System.WinForms.Form

    Dim f2 As WinForms.Form

    Public ReadOnly Property ZipCode() as Integer
       Get
          ZipCode = 12345
       End Get
    End Property

    Public Sub New()
        MyBase.New
        Form1 = Me
        InitializeComponent
    End Sub

    Overrides Public Sub Dispose()
        MyBase.Dispose
        components.Dispose
    End Sub

#Region " Windows Form Designer generated code "

     '-- Required by the Windows Form Designer
     Private components As System.ComponentModel.Container
     Private WithEvents Button1 As System.WinForms.Button

     Dim WithEvents Form1 As System.WinForms.Form

     Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Button1 = New System.WinForms.Button
        Button1.Location = New System.Drawing.Point(96, 40)
        Button1.Size = New System.Drawing.Size(75, 23)
        Button1.TabIndex = 0
        Button1.Text = "Form2"
        Me.Text = "Form1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(264, 197)
        Me.Controls.Add(Button1)
     End Sub

#End Region

    Protected Sub Button1_Click( ByVal sender As Object, _
                                 ByVal e As System.EventArgs )
       f2      = new Form2                          ' This works
       f2.Top  = me.Top  + 25                       ' This works
       f2.Left = me.Left + 25                       ' This works
       f2.StartPosition = StartPosition.Manual      ' This works

       ' Error: PhoneNbr is not a member of System.WinForms.Form       ' <==
       ' MessageBox.Show( "Phone Nbr. from Form2 = " + f2.PhoneNbr )   ' <==

       f2.Show()
    End Sub

End Class

'--- Form2 begins here ------------------------------------------------------

'Imports System.Drawing
'Imports System.WinForms
'Imports System.ComponentModel

Public Class Form2

    Inherits System.WinForms.Form

    Public ReadOnly Property PhoneNbr as String
       Get
          PhoneNbr = "888-555-1212"
       End Get
    End Property

    Public Sub New()
        MyBase.New
        Form2 = Me
        InitializeComponent()

        ' Error: Form1.ZipCode requires an object ref. and not is supplied   ' <==
        ' HOW DO I CREATE A REFERENCE TO ZipCode IN FORM1???                 ' <==
        ' MessageBox.Show( "ZipCode from Form1 = " + Form1.ZipCode )         ' <==

    End Sub

    '-- Form overrides dispose to clean up the component list.
    Overrides Public Sub Dispose()
        MyBase.Dispose
        components.Dispose
    End Sub

#Region " Windows Form Designer generated code "

    '-- Required by the Windows Form Designer
    Private components As System.ComponentModel.Container
    Private WithEvents btnClose As System.WinForms.Button

    Dim WithEvents Form2 As System.WinForms.Form

    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.btnClose = New System.WinForms.Button
        btnClose.Location = New System.Drawing.Point(72, 40)
        btnClose.Size = New System.Drawing.Size(75, 23)
        btnClose.TabIndex = 0
        btnClose.Text = "Close"
        Me.Text = "Form2"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(232, 157)
        Me.Controls.Add(btnClose)
    End Sub

#End Region

    Protected Sub btnClose_Click( ByVal sender As Object, _
                                  ByVal e As System.EventArgs )
       Close    
    End Sub

End Class



Tue, 11 Nov 2003 15:45:34 GMT  
 Accessing Custom Properties in Forms -- Parent and Child ?
In VBNet forms must be treted as regular classes,  so i think that in order
to access form1 from F2 you must pass it a reference to calling form, you
could do it modifying the form2 constructor so that it accept a reference to
a callback form (overloading could also be considered...)

e.g in Form2

Quote:
>     Public Sub New(CallBackForm as Form)
>         MyBase.New
>         Form2 = Me
>         InitializeComponent()

>         ' Error: Form1.ZipCode requires an object ref. and not is supplied
' <==
>         ' HOW DO I CREATE A REFERENCE TO ZipCode IN FORM1???
' <==
>         ' MessageBox.Show( "ZipCode from Form1 = " +

CallbackForm.ZipCode )         ' <==

Quote:

>     End Sub

in form1 you instantiate this way

Quote:
>     Protected Sub Button1_Click( ByVal sender As Object, _
>                                  ByVal e As System.EventArgs )
>        f2      = new Form2(Me)                          ' Pass a form1
reference to Form2
>        f2.Top  = me.Top  + 25                       ' This works
>        f2.Left = me.Left + 25                       ' This works
>        f2.StartPosition = StartPosition.Manual      ' This works

it would not be a bad idea to remove callback reference when form2 is
unloaded, but garbage collector shod do the work for you

Hope this helps...

Corrado


Quote:
> Note: The one block of code below contains
> two forms.  It is "plug-n-play".

> Create a new WinForm project, delete all
> the code that is generated and then cut
> and paste the code below into the editor
> and press F5 - presto, the form should
> appear.  Now for the question or problem.

> ' --------------------------------------
> ' I have two forms, Form1 and Form2.  Form2
> ' is invoked by Form1.
> '
> ' I wish to access custom properties in
> ' Form2 (PhoneNbr) from Form1 and access
> ' custom properties in Form1 (ZipCode) from
> ' Form2.
> '
> ' Can someone show me what needs to be done
> ' to establish this communications between
> ' the two forms.

> Imports System.ComponentModel
> Imports System.Drawing
> Imports System.WinForms

> Public Class Form1

>     Inherits System.WinForms.Form

>     Dim f2 As WinForms.Form

>     Public ReadOnly Property ZipCode() as Integer
>        Get
>           ZipCode = 12345
>        End Get
>     End Property

>     Public Sub New()
>         MyBase.New
>         Form1 = Me
>         InitializeComponent
>     End Sub

>     Overrides Public Sub Dispose()
>         MyBase.Dispose
>         components.Dispose
>     End Sub

> #Region " Windows Form Designer generated code "

>      '-- Required by the Windows Form Designer
>      Private components As System.ComponentModel.Container
>      Private WithEvents Button1 As System.WinForms.Button

>      Dim WithEvents Form1 As System.WinForms.Form

>      Private Sub InitializeComponent()
>         Me.components = New System.ComponentModel.Container
>         Me.Button1 = New System.WinForms.Button
>         Button1.Location = New System.Drawing.Point(96, 40)
>         Button1.Size = New System.Drawing.Size(75, 23)
>         Button1.TabIndex = 0
>         Button1.Text = "Form2"
>         Me.Text = "Form1"
>         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>         Me.ClientSize = New System.Drawing.Size(264, 197)
>         Me.Controls.Add(Button1)
>      End Sub

> #End Region

>     Protected Sub Button1_Click( ByVal sender As Object, _
>                                  ByVal e As System.EventArgs )
>        f2      = new Form2                          ' This works
>        f2.Top  = me.Top  + 25                       ' This works
>        f2.Left = me.Left + 25                       ' This works
>        f2.StartPosition = StartPosition.Manual      ' This works

>        ' Error: PhoneNbr is not a member of System.WinForms.Form       '
<==
>        ' MessageBox.Show( "Phone Nbr. from Form2 = " + f2.PhoneNbr )   '
<==

>        f2.Show()
>     End Sub

> End Class

> '--- Form2 begins

here ------------------------------------------------------

- Show quoted text -

Quote:

> 'Imports System.Drawing
> 'Imports System.WinForms
> 'Imports System.ComponentModel

> Public Class Form2

>     Inherits System.WinForms.Form

>     Public ReadOnly Property PhoneNbr as String
>        Get
>           PhoneNbr = "888-555-1212"
>        End Get
>     End Property

>     Public Sub New()
>         MyBase.New
>         Form2 = Me
>         InitializeComponent()

>         ' Error: Form1.ZipCode requires an object ref. and not is supplied
' <==
>         ' HOW DO I CREATE A REFERENCE TO ZipCode IN FORM1???
' <==
>         ' MessageBox.Show( "ZipCode from Form1 = " + Form1.ZipCode )
' <==

>     End Sub

>     '-- Form overrides dispose to clean up the component list.
>     Overrides Public Sub Dispose()
>         MyBase.Dispose
>         components.Dispose
>     End Sub

> #Region " Windows Form Designer generated code "

>     '-- Required by the Windows Form Designer
>     Private components As System.ComponentModel.Container
>     Private WithEvents btnClose As System.WinForms.Button

>     Dim WithEvents Form2 As System.WinForms.Form

>     Private Sub InitializeComponent()
>         Me.components = New System.ComponentModel.Container
>         Me.btnClose = New System.WinForms.Button
>         btnClose.Location = New System.Drawing.Point(72, 40)
>         btnClose.Size = New System.Drawing.Size(75, 23)
>         btnClose.TabIndex = 0
>         btnClose.Text = "Close"
>         Me.Text = "Form2"
>         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>         Me.ClientSize = New System.Drawing.Size(232, 157)
>         Me.Controls.Add(btnClose)
>     End Sub

> #End Region

>     Protected Sub btnClose_Click( ByVal sender As Object, _
>                                   ByVal e As System.EventArgs )
>        Close
>     End Sub

> End Class



Tue, 11 Nov 2003 15:02:06 GMT  
 Accessing Custom Properties in Forms -- Parent and Child ?
Many thanks.  That works, with one minor change --
and that change leaves one minor problem.  The
type in Sub New had to be specified as Form1,
rather than the more generic type Form.  Otherwise,
there is an error on oParent.ZipCode.

That means, AFAIK, that Form2 can only be called
from Form1.  In the realworld, we may want to
invoke Form2 from more than one form.  Perhaps
that is what you meant by overloading.

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

In Form1, I change the ZipCode property to a
string, from integer, and I have in the code:

    Protected Sub Button1_Click( ByVal sender As Object, _
                                 ByVal e As System.EventArgs )
       f2      = new Form2( Me )   ' Pass an Obj. Ref. to THIS form
       f2.Top  = me.Top  + 25
       f2.Left = me.Left + 25
       f2.StartPosition = StartPosition.Manual
       f2.Show()
    End Sub

Then in Form2, I have:

    Public Sub New( oParent as Form1 )    ' Constructor, Obj. Ref. to Form1
        MyBase.New                        ' received as argument oParent
        Form2 = Me                        ' Type Form, vs. Form1, does not work
        InitializeComponent()
        Form2.Text = Form2.Text + "  Zip Code from Form1 = " + oParent.ZipCode
    End Sub

===================================================================

Quote:
> In VBNet forms must be treted as regular classes,  so i think that in order
> to access form1 from F2 you must pass it a reference to calling form, you
> could do it modifying the form2 constructor so that it accept a reference to
> a callback form (overloading could also be considered...)

> e.g in Form2

> >     Public Sub New(CallBackForm as Form)
> >         MyBase.New
> >         Form2 = Me
> >         InitializeComponent()

> >         ' Error: Form1.ZipCode requires an object ref. and not is supplied
> ' <==
> >         ' HOW DO I CREATE A REFERENCE TO ZipCode IN FORM1???
> ' <==
> >         ' MessageBox.Show( "ZipCode from Form1 = " +
> CallbackForm.ZipCode )         ' <==

> >     End Sub

> in form1 you instantiate this way

> >     Protected Sub Button1_Click( ByVal sender As Object, _
> >                                  ByVal e As System.EventArgs )
> >        f2      = new Form2(Me)                          ' Pass a form1
> reference to Form2
> >        f2.Top  = me.Top  + 25                       ' This works
> >        f2.Left = me.Left + 25                       ' This works
> >        f2.StartPosition = StartPosition.Manual      ' This works

> it would not be a bad idea to remove callback reference when form2 is
> unloaded, but garbage collector shod do the work for you

> Hope this helps...

> Corrado



Tue, 11 Nov 2003 18:53:51 GMT  
 Accessing Custom Properties in Forms -- Parent and Child ?
This sound like a case of "Implements".
If you want Form2 to invoke the ZipCode property that may be present on
different forms you migth create an
interface and implement that interface on all the forms that expose the
ZipCode property.
So:
Create an Interface MyCode with the property ZipCode
in Form1 use:
Implements MyCode and add code to it
Then in form2..

 Public Sub New( oParent as MyCode )    ' Constructor, Obj. Ref. to Form1
...
...
oParent.ZipCode
 End Sub

Of couse implement the interface on all forms you wish to callback.
Think you ca also use late-Binding declaring oParent as object then invoking
the Zipcode property but i would not recommend it...
Another alternative could be to have Forms that inherits from Form1 (with
ZipCode) then use Form1 as param but sound too complicated...

Corrado



Wed, 12 Nov 2003 03:13:41 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. detect when in a child custom control when the parent form is minimized/maximized

2. detect when in a child custom control when the parent form is minimized/maximized

3. Accessing Objects on a Mdi Parent form from a Mdi Child form

4. MSHFlexGrid format child side in parent child form

5. need to access a parent variable (Public) from a child form

6. Accessing Parent control from children form

7. Accessing Parent control from children form

8. Accessing Parent control from children form

9. requery parent form when user adds a record to child form

10. how child form find out parent form?

11. Modifying object in MDI Parent form from MDI Child form

12. Loading MDI Child forms to a parent form from a DLL

 

 
Powered by phpBB® Forum Software