• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

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

0
toyboy61
Asked:
toyboy61
  • 3
  • 3
2 Solutions
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now