views:

245

answers:

3

I'm teaching myself VB.Net.

Here is a problem I have recently come across. Say I have a main Form1 in my application. Form1 calls a second LoginForm which (like the name suggests) is a login window with username/password type fields. Expected behaviour is that LoginForm will capture login details and pass them back to Form1.

What is the best way to do this?

In my mind, I was thinking along the lines of a function call like 'doLogin' that would 'show' the LoginForm, capture the data entered, dispose of the form and return the login details (probably in some kind of bean). Somehow I don't see this as being possible

What I have currently is less elegant. LoginForm is shown by Form1 modally (i.e. showDialog); a 'me' reference is passed to the second window. After user input has been received on LoginForm, I set a value on Form1, then dispose.

Is this the way everybody does it?

+1  A: 

I've always passed in a delegate to the second form which can be called to 'pass back' the values from the second form into the first.

That way you are avoiding any tight coupling.

Classic observer pattern.

Take a look at some of the code samples on this page:

http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

Codebrain
Care to illustrate with a code sample?
Cerebrus
chosen for the great article. thanks
kipkuch
A: 

Although this is in C#, it is easy to convert: C# beginner help, How do I pass a value from a child back to the parent form?

Mitch Wheat
A: 

You can also retrieve data in VB.NET using "My.Forms"

The below takes the text from a textbox in the LoginForm and puts it in a textbox in the main form.

Me.RetrievedDataTextBox.Text = My.Forms.LoginForm.Textbox1.Text
JTA