Ed Bitzer said:
I am trying to avoid global variables and take advantage of OOP and
now understand how to pass a variable to another form using a
constructor. But how to I return that variable if changed on Form 2?
Sample:
\\\
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Vom Windows Form Designer generierter Code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean _
)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents btnCancel As System.Windows.Forms.Button
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnCancel = New System.Windows.Forms.Button()
Me.btnOK = New System.Windows.Forms.Button()
Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker()
Me.SuspendLayout()
'
'btnCancel
'
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnCancel.Location = New System.Drawing.Point(176, 144)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(80, 24)
Me.btnCancel.TabIndex = 0
Me.btnCancel.Text = "Cancel"
'
'btnOK
'
Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnOK.Location = New System.Drawing.Point(88, 144)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(80, 24)
Me.btnOK.TabIndex = 1
Me.btnOK.Text = "OK"
'
'DateTimePicker1
'
Me.DateTimePicker1.Location = New System.Drawing.Point(16, 24)
Me.DateTimePicker1.Name = "DateTimePicker1"
Me.DateTimePicker1.Size = New System.Drawing.Size(240, 20)
Me.DateTimePicker1.TabIndex = 2
'
'Form2
'
Me.AcceptButton = Me.btnOK
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.btnCancel
Me.ClientSize = New System.Drawing.Size(274, 184)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DateTimePicker1,
Me.btnOK, Me.btnCancel})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form2"
Me.ShowInTaskbar = False
Me.Text = "Select date"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnOK_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnOK.Click
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
Private Sub btnCancel_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnCancel.Click
Me.DialogResult = DialogResult.Cancel
Me.Close()
End Sub
Public Property [Date]() As Date
Get
Return Me.DateTimePicker1.Value
End Get
Set(ByVal Value As Date)
Me.DateTimePicker1.Value = Value
End Set
End Property
End Class
///
Call:
\\\
Private Sub Button1_Click(...) ...
Using f As New Form2()
If f.ShowDialog() = DialogResult.OK Then
MsgBox(f.Date.ToString())
Else
MsgBox("Cancelled")
End If
End Using
End Sub
///