Sharing data between forms

  • Thread starter Thread starter Geoff Pennington
  • Start date Start date
G

Geoff Pennington

I have a form, call it SearchCriteria, which opens another form using this
code:
Dim frmResults As New SearchResults()

frmResults.ShowDialog()

In the SearchCriteria form the users specify, well, the criteria for
conducting a search. SearchResults should use that criteria to query the
database and then display the results. How do I pass the criteria from
SearchCriteria to SearchResults?

Much obliged.
 
* "Geoff Pennington said:
I have a form, call it SearchCriteria, which opens another form using this
code:
Dim frmResults As New SearchResults()

frmResults.ShowDialog()

In the SearchCriteria form the users specify, well, the criteria for
conducting a search. SearchResults should use that criteria to query the
database and then display the results. How do I pass the criteria from
SearchCriteria to SearchResults?

Add a public property to 'SearchResults' of the type of the search
criteria. Set this property after instantiating 'SearchResults' and
before showing the search results form.
 
OK, thanks, that works. Out of curiousity, is there a way to go in reverse,
that is, could 'SearchResults' reference a public property in
'SearchCriteria'? Seems like it should, but I don't know how 'SearchResults'
can get a reference to the form that opened it.
 
You can pass an object through the Constructor of the
second form, and created a local reference to it there.
then as you change data, it is getting changed on both.

Private object as MyClass

Public Sub New(ByVal objFromFrm1 as MyClass)

object = objFromFrm1


End Sub

Private SomSub
' Value will get changed in the object on both forms
' as objects work like pointers
object.value = Somedata

End Sub
 
Geoff Pennington said:
In the SearchCriteria form the users specify, well, the criteria for
conducting a search. SearchResults should use that criteria to query the
database and then display the results. How do I pass the criteria from
SearchCriteria to SearchResults?


If you don't already know about this, you will probably benifit greatly from
reading about the SmallTalk Model-View-Presenter pattern. Do a search on
google for "Model View Controller" or "Model View Presenter", I can't even
begin to explain how much this concept has helped my OOP skills!


~
Jeremy
 
* "Geoff Pennington said:
OK, thanks, that works. Out of curiousity, is there a way to go in reverse,
that is, could 'SearchResults' reference a public property in
'SearchCriteria'? Seems like it should, but I don't know how 'SearchResults'
can get a reference to the form that opened it.

You can pass the whole form (a reference to it) if you change the type
of the property to the form's type and set the property to 'Me' in the
calling form.
 
Back
Top