"Ed Bitzer" <ed******@yahoo .comschrieb:
>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()
InitializeCompo nent()
End Sub
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean _
)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
Friend WithEvents btnCancel As System.Windows. Forms.Button
Friend WithEvents btnOK As System.Windows. Forms.Button
Friend WithEvents DateTimePicker1 As System.Windows. Forms.DateTimeP icker
<System.Diagnos tics.DebuggerSt epThrough()_
Private Sub InitializeCompo nent()
Me.btnCancel = New System.Windows. Forms.Button()
Me.btnOK = New System.Windows. Forms.Button()
Me.DateTimePick er1 = New System.Windows. Forms.DateTimeP icker()
Me.SuspendLayou t()
'
'btnCancel
'
Me.btnCancel.Di alogResult = System.Windows. Forms.DialogRes ult.Cancel
Me.btnCancel.Fl atStyle = System.Windows. Forms.FlatStyle .System
Me.btnCancel.Lo cation = New System.Drawing. Point(176, 144)
Me.btnCancel.Na me = "btnCancel"
Me.btnCancel.Si ze = New System.Drawing. Size(80, 24)
Me.btnCancel.Ta bIndex = 0
Me.btnCancel.Te xt = "Cancel"
'
'btnOK
'
Me.btnOK.FlatSt yle = System.Windows. Forms.FlatStyle .System
Me.btnOK.Locati on = New System.Drawing. Point(88, 144)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing. Size(80, 24)
Me.btnOK.TabInd ex = 1
Me.btnOK.Text = "OK"
'
'DateTimePicker 1
'
Me.DateTimePick er1.Location = New System.Drawing. Point(16, 24)
Me.DateTimePick er1.Name = "DateTimePicker 1"
Me.DateTimePick er1.Size = New System.Drawing. Size(240, 20)
Me.DateTimePick er1.TabIndex = 2
'
'Form2
'
Me.AcceptButton = Me.btnOK
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.CancelButton = Me.btnCancel
Me.ClientSize = New System.Drawing. Size(274, 184)
Me.Controls.Add Range(New System.Windows. Forms.Control() {Me.DateTimePic ker1,
Me.btnOK, Me.btnCancel})
Me.FormBorderSt yle = System.Windows. Forms.FormBorde rStyle.FixedDia log
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form2"
Me.ShowInTaskba r = 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.EventArg s _
) Handles btnOK.Click
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
Private Sub btnCancel_Click ( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s _
) Handles btnCancel.Click
Me.DialogResult = DialogResult.Ca ncel
Me.Close()
End Sub
Public Property [Date]() As Date
Get
Return Me.DateTimePick er1.Value
End Get
Set(ByVal Value As Date)
Me.DateTimePick er1.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.T oString())
Else
MsgBox("Cancell ed")
End If
End Using
End Sub
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>