Passing values from one Form to another

ultraman

Centurion
Joined
Dec 31, 1969
Messages
138
Location
Quebec, Canada
Hello everyone !

My problem is that I'm trying to return a DataSet to the Main Form of my App from an instance of frmSearch. But I can't, since I don't now how to access the Main Form instance from there ?

I first added a new creator to frmSearch with the calling Form in argument. But this parameter is declared as Form (not as frmMain)because I want it to be as generic as possible.

What would be the best way to pass a parameter to a Form and returns something ? What I often did in VB6 was to call a Public Function of the frmMain or a Property, but I have troubles managing it in .NET. If you have any links to good tutorials with code sample on that topic, it would be great !


Thanx in advance !

Ultraman
 
The solution is to pass it as the type of the form you actually need, not just Form. You say you want it to be as generic as possible, but how can it be generic if you need to update something on a specific form?

The other solution is to cast your variable to the right type when you need to, e.g.

Visual Basic:
DirectCast(myStoredForm, frmMain).Blah = "blah"
 
I know it's not really generic, but you know, just be able to call this form from a few other in my App where I'll declare the same Public Property or Function.

All I do is fill the DataSet in rmSearch, then pass it to the frmMain instance (or other caller) so that it can fill its ListView with the results.


The DirectCast thing works just fine !

Thanx a lot, you're a genius :)


UltraHappyMan
 
If frmSearch didn't need anything from the parent form except a reference with which to send back a DataSet, you could use an Interface. Define the interface with method GetDataSet and have frmMain inherit from Form (as usual) but also implement your interface. In C#, you use something like:
Code:
class frmMain : Form, MyInterface
{
}

Or, you could define a new class that inherits from Form and have your frmMain (and everything else that calls frmSearch) inherit from that instead of Form. Use that new type as the parameter to frmSearch. Of course, the new class could expose a GetDataSet method.

The interface would be ideal since you could define as many of these interfaces as you want and have frmMain implement them all. You can only inherit from one type in C# though.

If you need a sample project, let me know.

-Nerseus
 
That's great ! I'm new to the concepts of heritage in Interface 'caus I come from the wonderful (sic) world of VB6. But I'm always more than happy to learn new and improved techniques.



If you have this code sample of yours, that would be nice.


Thanx a lot !


Ultraman
 
Attached is a sample VB.NET project. I actually use C# so I'm not 100% sure how to do things in VB.NET. But, the project does work. If you need C#, it's waaaaay easier for me and I can have that sample up in no time.

The sample...
It has 2 forms and one Interface. The interface is named ISearchUtil. When I wrote it, I wasn't sure what you wanted (didn't have an internet connection) so here's what it does:
1. frmMain implements the ISearchUtil interface.
2. The ISearchUtil interface defines two functions: GetDataSet and GetDataTableName.
3. frmSearch's constructor (method New) takes an ISearchUtil parameter.

Basically, when frmMain creates frmSearch (in the button's click event), it passes a reference to itself to frmSearch's constructor. Because frmMain implements the interface, frmSearch recognizes it as ISearchUtil and not a Form. Therefore, you can NOT call any methods of frmMain from within frmSearch. If you need them, you'll have to define them explicitly in ISearchUtil or use some other mechanism :)

frmMain contains a private DataSet variable. My assumption was that frmSearch needs to get some data from a calling form. That data is passed in through a DataSet. Since you need the opposite, change the method from GetDataSet to SetDataSet and have frmSearch call it, passing in its own DataSet rather than grabbing one from frmMain.

Sorry for the confusion. If you need a better sample that suits your needs, let me know - now that I have my connection back, all is gooood.

-Nerseus
 

Attachments

Nerseus, your sample was great, but I tried heritage to do the trick, and it's simple, perfect, just what I was looking for. So for the general benefits, I post this to tell you how I've done it.

First declare a BaseForm that will contain the methods you want all your other forms to use :

Visual Basic:
Public MustInherit Class frmBaseForm
    Inherits System.Windows.Forms.Form

    Public MustOverride Sub PopulateResultList(ByVal dsSearchResult As DataSet)

End Class

The MustOverride tells you that all the forms that will Inherit from BaseForm will HAVE TO implement this abstract method.

Then you create a normal Form, but change the "Inherits System.Windows.Forms.Form" statement by
Visual Basic:
Inherits BaseForm

In this new Form, you'll have to implements (Override) the method. in this sample, I've also Overloaded it.

Visual Basic:
    Public Overloads Overrides Sub PopulateResultList(ByVal dsSearchResult As DataSet)
        '************************************************
        'Function called by frmSearch to fill the list with the search results
        '************************************************
'Some code....

    End Sub

    Private Overloads Sub PopulateResultList()
        '************************************************
        'Fill the lists with all values at tartup
        '************************************************

'Some other code..........

    End Sub


In frmSearch now, you have to declare a locale variable of type Form that will be initialised in a new constructor.
Visual Basic:
Private mfrmParent As System.Windows.Forms.Form

Public Sub New(ByRef ParentForm As Form)
        '****************************************************************
        'One argument constructor that is used by clients so that we can call a method from it
        '****************************************************************
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        mfrmParent = ParentForm
    End Sub

Now, we're ready to call frmSearch from our main form (which Inherits BaseForm)

Visual Basic:
Dim mfrmSearch As frmSearch = New frmSearch(Me)
mfrmSearch.Show()

When we are ready, we can now call the filling method of the main Form from frmSearch, you can call this method by applying DirectCast for the BaseForm Type
Visual Basic:
DirectCast(mfrmParent, BaseForm).PopulateResultList(dsArtiste)


Job's done ! Now your frmSearch can call the method in all your forms if they all inherits from BaseForm. Hope it will help some of you !


Ultraman

P.S. just a little problem, the form designer seems not to apreciate it very well, but the program works just fine ! One way of skippin the problem is to change the Inherits declaration at design time. Not very practical, but works.
 
Back
Top