Working with 2 forms...

  • Thread starter Thread starter Donald Smith
  • Start date Start date
D

Donald Smith

Hey,

I have 2 forms. On the first form, there is a function that displays a lot
of data, with a lot of values, that I store in variables. So what I want to
do is use the info that I store in variables on the first form, and insert
it into the second form. Like this... (Not my real code)

Form1:

int Filter;

Filter += 1;

Form2:

this.Text = Filter.ToString();

Any help? It's really holding me back in my project.

-Donald
 
Donald,

What you need to do is expose these values so that they can be accessed
outside your form, either through a property, or method. Once you do that,
you have to pass the instance of form 1 (or whatever object that contains
the data), to the second form (through a property or method), at which
point, it will access the data that it needs.

Forms are just like objects, and if you want one instance of an object
to access properties on another object instance, you have to make the
reference available to the other object, as well as make it accessible.

Hope this helps.
 
Donald,

What you need to do is expose these values so that they can be accessed
outside your form, either through a property, or method. Once you do that,
you have to pass the instance of form 1 (or whatever object that contains
the data), to the second form (through a property or method), at which
point, it will access the data that it needs.

Forms are just like objects, and if you want one instance of an object
to access properties on another object instance, you have to make the
reference available to the other object, as well as make it accessible.

Hope this helps.

I am having similar trouble, maybe you can help me out here?

I have a main form and have added a second form, frmScreen, to the
project, which contains a button and a TextBox control. In my main
form, I create an instance of this second form and show it, like so:

Form frmMyScreen = new frmScreen( );
frmMyScreen.Show( );

Why is it that if I add a public method to the frmScreen class, it is
not available to me in the instance I create in my main form? In the
frmScreen class, I added a public method "String GetEditText()" which
simply returns the contents of the edit control, but it is not made
available to me in my main form, not through intellisense, and if I
try to call it, the compiler warns that 'System.Windows.Forms.Form'
does not contain a definition for 'GetEditText'.
So, frmMyScreen.GetEditText() appears to be a nono and I just don't
understand why!

Thanks,
Jan Roelof
 
Jan,

The reason for this is that you are declaring the variable to be of type
Form, not frmScreen. Change the line to:

frmScreen frmMyScreen = new frmScreen( );

And it should be ok.
 
Jan,

The reason for this is that you are declaring the variable to be of type
Form, not frmScreen. Change the line to:

frmScreen frmMyScreen = new frmScreen( );

And it should be ok.

Thanks! I really and truly blundered here. I have been programming
C++/MFC too long to pull something stupid like this :(

Jan Roelof
 
Back
Top