Referencing a variable defined in one form from another form...

I have a form called "Form1". From this form I'm calling another form (Miniform) using the attached code snippet (Snippet ID=8128684).

On "Form1.cpp" I have included a file called "global.h" which contains the definition of an integer array called "Dekning".

Now I want to be able to alter values in this array from a routine defined in the "Miniform.h"-file, i.e. when I click on a button on this form.

Both the Form1 and the form Miniform is already defined using the IDE of Visual Studio .NET 2003. (And yes - it should be managed code..).

How do I do this ?

Miniform *form2 = new Miniform();
			   form2->ShowDialog();

Open in new window

toyboy61Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Take a look at my article. The examples are in VB.NET and C#, but the overall concept is the same. If you need further help translating it to C++, don't hesitate to ask  : )
0
sarabandeCommented:
you could add a setParent member function to class Miniform where you can pass the 'this' pointer of Form1 class. the setParent would assign the Form1* argument to a member pointer of same pointer type.

Your above code would change to

Miniform *form2 = new Miniform();
form2->setParent(this);
form2->ShowDialog();

Open in new window



then your Form1 class should provide a public member function where the array could be updated. the miniform then could call that function using its member pointer (rather than accessing the array directly).

note, defining an array in header global.h is not a good idea. you probably would get problems whenever the global.h was used in a second cpp file. instead define the array as member of Form1 class.

Sara
0
toyboy61Author Commented:
sarabande: Error message from building:  ": error C2039: 'setParent' : is not a member of 'SystemTipp100::Miniform'.

I'm using Visual C++ 2003...
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

toyboy61Author Commented:
sarabande: Maybe I could use "form2->Parent" without parameters ??  It's a valid... :-)
0
sarabandeCommented:
i meant you to add a member function to class Miniform. but if it is a system class (??) you would need to derive from it to add new member functions.

yes, if Parent is a member of class Miniform (and is correct type) you can do

  form2->Parent = this;

i am not used to managed c++ and windows forms  so i don't know if the types do match.

Sara
0
toyboy61Author Commented:
kaufmed: I'm still a bit confused about this - especially when using Visual C++ 2003 .NET.

What I want to do is:

1) From Form1 I'm opening a new form called Miniform. (The form is already defined in the Designer).
2) On Miniform I have two values: "Kampnr" and "Dekning", both of type "int".
3) "Kampnr" and "Dekning" are updated by the user in the Miniform form.
4) When the user click on a button ("Close") on Miniform I want the values from "Kampnr" and "Dekning" to be transferred back to Form1 and used in a function called "UpdateDekning".

Could you give me a source code example on how I can accomplish this using Visual C++ ??
0
sarabandeCommented:
you can do

  UpdateDekning(form2->Kampnr, form2->Dekning);

after form2->ShowDialog();

for that the form2 should have two public int members Kampnr, Dekning which were filled when the user closes the Miniform dialog.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.