passing variable values between forms

SSb

Member
Joined
Sep 28, 2005
Messages
14
Programming Experience
Beginner
Hi all,
How can I pass variable values between two forms when its not a MDI application i.e. no parent-child thing.

In particular,
I have two forms Form1 and Form2.
There is a variable in Form1 say 'choice'
When a button on Form1 is pressed, Form2 comes up and the user is asked to input some data depending on the value of 'choice'.
Then finally those values are returned to Form1.
How can I do this?

Thanks,
Saurabh
 
Add public property to your form2, when calling this form2 object instance you pass the value to this property so form2 can access it. You can also add more properties to form2 where you put results user inputs, these properties exists as long as the form2 object exists. Your question is about a dialog, so I will give a brief showdialog example:

VB.NET:
class mainform
inherits system.windows.forms.form
 
  sub button_click(sender,e) handles button.click
    dim f2 as new form2
    f2.choice = 99
    if f2.showdialog=dialogresult.ok then
      msgbox(f2.userinput)
    end if
  end sub
 
end class
 
 
class form2
inherits system.windows.forms.form
 
  public choice as integer
  public userinput as string
 
  sub form_load(sender,e) handles form.load
    text = choice.tostring
  end sub
 
  sub buttonOK_click(sender,e) handles buttonOK.click
    userinput =textbox1.text
    dialogresult=dialogresult.ok
  end sub
 
end class
 
Back
Top