There are tons of ways to pass data from one form to another, I'm going to give a few samples here that will hopefully answer some questions.
One thing to get a good handle on is Scope and Access. On any class you can declare a variable as
Private,
Protected, or
Public (There is also Friend, but we won't go into it here).
Things declared
Private are only available in that class.
Things declared
Protected are only available in that class, or classes that inherit that class.
Things declared
Public are available from anywheres.
Now that we know the three primary access types, you should be able to see that if you want to pass data from one object to another, there must be a
Public sub, function, property, or variable. With out some sort of public access, there is no way to pass the value.
Another important issue that many VB6 coders have problems with (specificly w/ forms) is the difference between a Class and an Instantiation. I would recommend reading this FAQ: http://www.tek-tips.com/faqs.cfm?fid=5611 if you are not familiar with the difference.
For these examples lets assume we have 2 form classes. These classes will be called
Form1 and
Form2. It is important to not confuse your
Objects with your
Classes. So when we have instantiations of these to forms I will call them
frmSender and
frmReceiver. We will also assum the Form1 has a textbox called "txtSend" and Form2 has a text box called "txtReceive"
Option 1: Passing data to a method
-=-------------------------------------
on the Form2 class we will add a sub that will accept a string value and assign it to Form2.txtReceive. We declare it as
Public so that other objects can access it.
Code:
Public Sub SetReceive(Value as String)
txtReceive.text = Value
End Sub
In the Form1 class, we can now instantiate a Form2 object, and send a value to it. We declare it as
Private because nothing outside of this class needs to call this function. We use
ShowDialog so that the code in this class will wait until frmReceiver closes to continue.
Code:
Private Sub OpenForm2
dim frmReceiver as new Form2
frmReceiver.SetReceive(txtSend.text)
frmReceiver.ShowDialog
End Sub
Option 2: Passing data to a Property
-=-------------------------------------
In some situations, you may need to set a value, and get it back. An easy way to do this is with Public Properties.
We can make the txtReceive text box's text property accessable through a public property like this on the class Form2:
Code:
Private Property ReceiveText() As String
Get
Return txtReceive.Text
End Get
Set(ByVal Value As String)
txtReceive.Text = Value
End Set
End Property
Now in the class Form1 we can access the ReceiveText property from Form2 any place that we have an instantiation of Form2. This type of access would by more useful when you have both forms open at the same time in a non-dialog manor. This would be likely to occur in a MDI environment.
Option 3: Passing data to a specific control
-=-------------------------------------
This is probrably the cheapest, fastest, not quite as elegant way to move data. In the code of the Form2 class there is a region called
Windows Form Designer Generated Code.
If you open that up, you'll see a few things, first, the
Constructor which is called "Public Sub New()" This sub is fired when you instantiate the class (ie: dim frmReceiver as
New Form2)
Next is the
Destructor, which is called "Protected Overloads Overrides Sub Dispose". This sub is fired when an object is destroyed or goes out of Scope. (ie: frmReceiver.Close or frmReceiver = Nothing)
Next up you'll see a declaration, and then the block we are looking for. You'll know you're in the right area when you see a Microsoft warning:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Once you see that, look for this:
Friend WithEvents txtReceive As System.Windows.Forms.TextBox
All we need to do is change that
Friend to
Public and we can now access the txtReceive object from anywheres we have and instance of the class. Notice that this will expose the control itself, not just the text property.
Option 4: Passing data to the constructor
-=-------------------------------------
Dig back into that
Windows Form Designer Generated Code in Form2 again. This time, goto the
Public Sub New(). We can add a parameter to the constructor and assign the text here:
Code:
Public Sub New(ReceiveValue as string)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
txtReceive.Text = ReceiveValue
End Sub
Then when we instantiate frmReceiver from Form1 we do this:
Code:
Private Sub OpenForm2
dim frmReceiver as new Form2(txtSend.text)
frmReceiver.ShowDialog
End Sub
Option 5: Passing data and waiting for a return value
-=-------------------------------------
This is covered in my other passing data FAQ here: http://www.tek-tips.com/faqs.cfm?fid=5671
Option 6: Passing a form to a process
-=-------------------------------------
This is a style that I have abused quite a bit when working with processes. A lot of our work here is process based, and while the process is working, I want to update the form with a process or progress bar, or update a status bar, etc.
For this sample, we will have Form1, and we will have Process1. Process1 will be instantiated to pcsDoThing.
In Process1 we add a constructor. Since this class isn't a GUI, it doesn have a windows generated block or a consturctor, so we'll make our own:
Code:
private m_frmOwner as Form
Public Sub New(byref frmOwner as Form)
m_frmOwner = frmOwner
End Sub
We can use the private variable
m_frmOwner to track the form that was passed, in through out the class now! for example:
Code:
Public Sub DoProcess()
dim i as integer
for i = 0 to 100000
'do something
m_frmOwner.text = "Item " & i & " of 100000"
next i
End Sub
In Form1 we could call it like this:
Code:
Private Sub StartProcess
dim pcsDoThing as New Process1(Me)
pcsDoThing.DoProcess
End Sub
This is just a small sample of the ways to pass data from one form or object to another. The two main things you need are an instance of the object, and access to a member that exposes the object or property that you need. Hope it helps, if you have questions, post them on the forums, I'll expand the FAQ to cover more issues, or more details on these issues as questions arise.
-Rick