Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing values between forms

Status
Not open for further replies.

iainmc

Programmer
Dec 2, 2002
53
GB
This is a newbie problem: I am cannot seem to get a the value held in my list box on form 1 to appear in a text box on form 2.

Form 1 opens form 2 on a button click but i can't sem to pass it forward here nor can i refer back to form 1 on the form 2 load.

Help pls, appreciated
 
I presume this is a win forms question?
Win Forms -
Probably the most object orientated way is to overload the new contstructor on form 2 to accept the text you want, and have this populate the text box. When you initialise form2 in form1, use this overload.


In form2
Code:
'you'll need this in addition to the existing new sub in form 2
    Public Sub New(byref strForTextBox as string)
        MyBase.New()

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

        'Add any initialization after the InitializeComponent() call
TextBox1.text = strForTextBox
    End Sub

and in form1

Code:
dim frm2 as new form2(ListBox1.text)

Web Forms - easiest to use the query string.

Mark [openup]
 
I was trying to do something similar recently. I found a post that gave another option. You can declare a variable in a module to hold a reference to the form and set it when the form is created. Then you can use that variable to access the components on a form. You should also kill the reference when the form is closed.

This is not an object oriented approach as Custom24 gave, but may be handy depending on your needs.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top