astro wrote:
I want to build a Filter form
<snip> When the user closes this filter form it notifies the calling
form
<snip> Could I setup a callback for this purpose? Is there a better way to do this?
<snip>
MainForm:
============
sub openfinder()
dim frm as new FinderForm
dim mydelegate as FinderForm.thisone
mydelegate = addressof SenditHere
frm.show
end sub
sub SenditHere(astr as string)
msgbox ("here it is: " & astr)
end sub
FinderForm:
============
private sFilter as string
public Delegate sub thisOne(byval astr_ as string)
...
...
private sub closeForm (sender, e) handles btnOk.click
call thisOne (sFilter) << DOES NOT COMPILE - "thisOne is a type
and cannot be used as an expression"
end sub
The compilation error exists because you are simply declaring a
delegate type, but not a varible of that type.
You must declare a variable of the delegate type in the filter form, so
interested partners may 'connect' to the form.
In FinderForm, you'd have something like this:
<code style="Air code">
Private mTarget AS ThisOne
Public Property OnThisOne As ThisOne
Get
Return mTarget
End Get
Set(Value As ThisOne)
mTarget = Value
End Set
End Property
</code>
The code above declares a *variable* of type ThisOne. The public
property allows a caller to asssign a value to the variable, Then at
the appropriate time (when the FormClose sub kicked in), you'd have:
If mTarget IsNot Nothing Then
Call mTarget(sFilter)
End If
Notice the test to see if your delegate reference (mTarget) IsNot(tm)
Nothing: If you'd just call it without testing first, you'd risk
getting a NullReference exception...
Given the complications involving the directly handling of Delegates, I
suggest, as Marina already did, that you use Events instead:
<code style="Air code">
'Inside the Filter form
Public Event OnFilter(Value As String)
'...
Private Sub CloseForm(Sender As Object, E As EventArgs) _
Handles Button1.Click
RaiseEvent OnFilter(sFilter)
End SUb
'Inside Main Form
Private WithEvents MyFinderForm As FinderForm
Sub OpenFinder()
MyFinderForm = New FinderForm
MyFinderForm.Show
End Sub
Private Sub OnFilter(Value As String) _
Handles MyFinderForm.OnFilter
MsgBox("here it is: " & Value)
End Sub
</code>
Hope this helps.
Regards,
Branco.