I have two forms, namely MainDataForm and JobForm.

In MainDataForm I have a text box named txtJobID which contains a certain job ID. There I also have a button - btnNewJob, if clicked opens the above said JobForm.

Now, on JobForm there is another text box named txtNewJobID and when the JobForm loads, I need this txtNewJobID to display the same data that was on the txtJobID of MainDataForm.

At the moment, I created a get method on MainDataFrom which looks like this:

public string GetJobID
        {
            get
            {
                return this.txtJobID.Text;
            }
        }

Now I'm aware that if I call this method from withing the JobForm I'll be able to access the data which was on the MainDataForm. (Or am I wrong?)

The problem is, when I'm in the load event of the JobForm, how do I access the instance of the MainDataFrom which contains the text box txtJobID with data in it?

Please help ASAP.
Thanks in advance!

Recommended Answers

All 2 Replies

you need to pass the instance to the constructor of the new form modify the constructor to take a form type variable and assign it to a local variable. like so.

on the job form

Form MainForm;

JobForm(Form MFrm)
{
    MainForm = MFrm;
}

then you can use the "MainForm" variable to get the properties of the main form.

now be sure to pass the instance to the new jobfor using the "this" keyword when instantiating the new form.

JobForm jbf = new JobForm(this);

and that is all you need to know. there is another way, because of the way that the dot net framework works, there is a way to call the clr and get a list of all the public objects open and search for one with the correct name and type, but its complicated, and the best way is that in which I already shared.

happy coding.

Diamonddrake, thanks for the quick reply.
That works fine!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.