Passing parameters between various Forms

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

Hi,

I have created a new form which gets initialized by an event.

Private Sub btnScenGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnScenGenerate.Click

Dim Test As FormTest = New FormTest

Test.ShowDialog(Me)

End Sub



The new form is the only one active and we need to kill it to get back to the original form. And this exactly what I want.

My question is: what is the best way to pass info from the original form into the second ?

For example I want to pass a string form fMainForm to formTest.

TIA

-steve
 
'Having your Main Form pass info to your test form.
Dim Test As FormTest = New FormTest()
Test.SomeTextBox.Text = Me.someString

'Having your Test form pull info from your Main Form
'Declared inside your TestForm class
Dim parentForm As MainForm()
parentForm = DirectCast(parentForm, MainForm)
Me.SomeTextBox.Text = parentForm.someString
 
rmacias said:
'Having your Main Form pass info to your test form.
Dim Test As FormTest = New FormTest()
Test.SomeTextBox.Text = Me.someString

IMHO this is really bad form. It ties your calling code to tightly to your
form. Use either paramaters in the forms constructor or use public
properties instead. That way you can do this....

Dim Test As FormTest
Test = New FormTest(argument1, argment2, argument3)
Test.Show()

OR

Dim Test as New FormTest()
Test.Arg1 = "string1"
Test.Arg2 = "string2"
Test.Arg3 = 'string3"
Test.Show()
 
I see.

Thanx for your advice.
However , using Show() means that I can still put focus on the parent form.
Should I use ShowDialog(Me) instead or is there a better way ?

TIA
-steve
 
You can use:

Dim Test As FormTest
Test = New FormTest(argument1, argment2, argument3)
Test.ShowModal()

....to show a form as a modal (has full focus) form.

ShowDialog I belive gives the abillity to return form the form with a dialog
result but I have not used it before so...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top