HELP! Passing Parameters using Properties...
Hello all.
I am a newbie to VB6, and am stuggling to understand how to pass a variable from one Form to another.
I am writing an app that is creating a Process, then opening up another form for the Duration that the Process is running, so i need to Pass the ProcessID to the Second form. I can get the process ID well enough, though just can't get the second form to see It!
I have been attempting to do This by using the Form2 Properties (of which I've never actually written any myself yet), though with little success. Can anyone help!??!
# 1 Re: HELP! Passing Parameters using Properties...
You have several options. You could use the Form's Tag property: from Form1, set Form2.Tag = ProcessID, then read the value from Form2's code. Or, you could declare a Public variable in Form2 and set it from Form1:
Form2.VariableName = ProcessID
Or, you could define a custom property in Form2, but that seems like overkill in this case.
# 2 Re: HELP! Passing Parameters using Properties...
Thanks Phil!
The .Tag option seems to be the one! Although, I cannot seem to Access it in Form2 Form_Load()? - though in other events it exists.
Do you know why this is?
I wanted to Put the WaitForSingleObject call in the Form_load() of Form2. Where would be the best procedure for this call?
# 3 Re: HELP! Passing Parameters using Properties...
It looks like the .Tag property is not available until the form is fully loaded (that is, after the Form_Load event). You might try creating a Public Sub to do your WaitForSingleObject stuff; when you call the method, the form will load automatically.
# 4 Re: HELP! Passing Parameters using Properties...
Ok,
I've just tried that, but Where I can now See the .Tag Value here, the Form2 is not Displayed!! :confused:
The Purpose of Form2 is to display a Progressbar while A process kicked off in Form1 is running. A Cancel button Should be here also, which terminates this Process early if required.
I am now thinking that i should be Creating the Process in Form2, so no need to pass the id accross to it.
However, I have 2 individual processes to run (both requiring 'Progress updates'). Am I correct in saying that I would create 2 FormObjects from Form2, (frm_proc1, frm_proc2) one for each process, passing the relevant Process name (i.e. calc.exe) to them.
This leaves me in a similar position to where I am now - Passing a parameter to a Form, then accessing that Parameter in the correct module of that form.
What would be your take on how to achieve this?
# 5 Re: HELP! Passing Parameters using Properties...
Here's how I'd do it. Don't have Form2 (which I would call something like frmProgress or frmCancel) do anything except display the progress and notify the parent form if the user presses the Cancel button:
' frmCancel
Option Explicit
Public Event Cancel()
Private Sub cmdCancel_Click()
RaiseEvent Cancel
End Sub
Public Sub UpdateProgress(ByVal PercentDone As Integer)
' Update progress bar
End Sub
Have the main form do everything else: start the process, report progress to frmCancel, and abort the process if the user clicks Cancel:
' frmMain
Option Explicit
Private bCancel As Boolean
Private WithEvents CancelForm As frmCancel
Private Sub CancelForm_Cancel()
bCancel = True
End Sub
Private Sub Form_Click()
Dim I As Long
Set CancelForm = New frmCancel
CancelForm.Show
' Begin lengthy process
For I = 1 To 2000000000
' Every 1000th time through the loop...
If (I Mod 1000) = 0 Then
' Calculate percent complete and
' pass to CancelForm to update
' progress bar
CancelForm.UpdateProgress I / 20000000
' Check for Cancel event
DoEvents
' If cancelled, close CancelForm
' and abort process
If bCancel Then
Unload CancelForm
Exit For
End If
End If
Next
End Sub
But that's just me. ;-)
# 6 Re: HELP! Passing Parameters using Properties...
Cheers Phil - I shall give that a go!
# 7 Re: HELP! Passing Parameters using Properties...
Thanks a lot for your help Phil - that worked a treat! I can now finally move on to the next part Of the Project! :D
# 8 Re: HELP! Passing Parameters using Properties...
Passing variables between forms is as Phil said.
However, if the only thing that form2 does is display the progress bar, why not place the progress bar in a frame that you make visible when you need it (and then set visible=false when finished)?
P.S. The form can also contain command buttons - 'cancel' etc
gupex at 2007-11-11 17:30:03 >
