Rtn Varialbe from Second Form

E

Ed Bitzer

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?

Ed
 
T

Terry

Define a public/friend property (maybe readonly) on the second form and
access it from the first form.
 
E

Ed Bitzer

Thanks Terry.... I can pass information over to form2 using a Friend
Write Only property but I am over my head trying to grab that same
property after changing it and passing it back.

Ed
 
T

Terry

well if you want to both set it before hand and read it afterwards, then
don't make it write only.

form2.someproperty = "Boo"
form2.showdialog
thevalue = form2.someproperty
 
H

Herfried K. Wagner [MVP]

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
///
 
E

Ed Bitzer

Terry and Herfried,

I have spent about about four hours trying to digest and make work
code that is my best interpretation of your responses, but please
forgive, I am going to bed (75 years need extra sleep) and will work
again tomorrow and the holiday. If I fail it is not your fault and I
do appreciate.

Ed
 
E

Ed Bitzer

Terry and Herfried,

I got it...spent some time learning how to use OOP using the videos on
the Express website and of course my Form2 is just another class. I
simply created Properties on Form2 and with Methods can modify and of
course use these back on Form1. I have made a big step forward and
will now avoid the use of Modules (and now not sure its purpose) and
definitely global variables.

Herfied's example is much more involved and although I can get Form2
to compile and have not figures out what information belongs on Form1
which of course I can display with fmr2.showDialog() after creating
with new.

Ed
 
T

Terry

Sounds like you made a leap forward. Yes, form2 is just another class that
can expose properties like any other class.
 
E

Ed Bitzer

Terry,

Hope you understand that this is a great hobby for a guy now retired
15 years. In the early days I had to program simply because software
just was not available for calculations and data manipulation and I
worked for a small manufacturing company that had no computer staff -
so I have programmed in RPGII, Cobol, all the MS Basics, Turbo Pascal
and then VB and now VB.net. So not being a full time job I now enjoy
as a hobby. Guys like you and Herfried sure make my day because I
have no staff with which to confer - in fact in my community I know of
know one else that even would consider programming.

Now sharing my programs with friends and families is a problem with
VB8. VB5 had a nice install program. Express's install provides no
options, such as where it will place the program, and sure stuffs it
in an obscure folder under Documents and Settings. I wrote one that
has a spell checker which uses Word and I have yet to figure out how
to send the necessary files so that they don't have to bury them. From
what I read, purchasing an upgrade might have a more conventional
install program. But it does keep me out of trouble.

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top