
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