Printable Version of Topic
Click here to view this topic in its original format
Unofficial VirtualDub Support Forums > General programming > Sharing Data Between Child Forms


Posted by: stephanV Sep 15 2010, 07:31 AM
I did a course in C++. We used the Borland compiler there, but I don't like it very much and decided to switch to Visual C++ 2008. So far so good.

I'm doing some experimenting now and the situation is this.

I have a Main form and two child forms which are called by this form (say a configform and a executeform). Data is configured in the configform and something is done with this data in the executeform. For this I need to get the data from one to the other form.

I found three methods of doing this, two of which I don't like and one I don't know if is "proper" so to speak.

Method 1:

Put a bunch of hidden data objects (e.g. TextBoxes) in the executeform and route the required data to them via the Main form. This seems a rather ugly way of doing it.

Method 2:

Use external variables. The problem with this is that it only allowed me to use the basic data types (complaints about management?) and I would prefer to use the String^ class/data type. Since the options are limited I could map the Strings to chars or int with 'if/else if' or 'switch' statements, but this again seems rather ugly.

Method 3:

Make the required objects in configform public and static and put an include of configform.h in the executeform. This seems to work excellent, but I don't know if there will be any major repercussions I'm not aware of.

Any insights or other methods?

TIA

Posted by: phaeron Sep 16 2010, 06:46 AM
Eww, C++/CLI. I'm ashamed to admit that I know a lot more about this than I should. I have to say that doing WinForms in C++/CLI is pretty rough, and I would recommend C# 3.0 or higher if you really want to go that route.

Don't use TextBoxes for data interchange. You're not using VB6. smile.gif

Remember that when you are using C++/CLI, you have to be careful about trying to do things that are legal in C++ but not legal in .NET. I'm not sure why the compiler doesn't automatically handle it, but you can make global references to managed objects by using a static member of a class instead.

Putting the values on the forms is indeed a better way of doing this, but I'd recommend using instance properties rather than public fields or statics. C++/CLI has a shortcut for this:
CODE

property String^ CurrentText;


However, I'd go farther and say that you should also avoid having the two forms talk to each other. Part of your method 1 was a good idea in that you should have the main form set up the second form based on the output of the first. This is cleaner within the forms themselves and avoids the need for icky code where the second form "adopts" the parenting of the first form. I believe formally you'd want to do this via a controller object that's not a form (view), but for simple stuff I'd just put it in the main form. Just have an exit strategy for when the main form code starts to balloon, as inevitably happens.

In general, you shouldn't need to use non-constant static variables that much in .NET code. The ubiquity of delegates, events, and interfaces usually makes this unnecessary as you can always pass around class objects. It's also a good idea to avoid them because statics are excellent anchor points to introduce huge memory leaks into your program, particularly static events.

Posted by: stephanV Sep 16 2010, 07:53 AM
QUOTE (phaeron @ Sep 16 2010, 08:46 AM)
Eww, C++/CLI. I'm ashamed to admit that I know a lot more about this than I should. I have to say that doing WinForms in C++/CLI is pretty rough, and I would recommend C# 3.0 or higher if you really want to go that route.

Hehe. Yeah I was already thinking about that. I suppose the jump wouldn't be too large. I'm doing a bachelor ICT/Computer Science in the evening. The first year had a course of C++ and I just want to keep busy with it. I'm already seeming to forget things just after the holidays. sad.gif

This year will be spend on Java though (Netbeans), so time is a little bit of a problem.

One thing I remembered while playing more with this, since the second child form can only be accessed if the first is already configured, is to create the necessary privates in the second and initialize them via a constructor called by the main form. Or perhaps via

CODE
this->child2->setValueX(this->child1->getValueA())


The problem is we treated the subject of classes, but not so much with relation to the visual stuff. So I'm having a bit difficulty to decide if just should treat a form as a simple class or not.

Posted by: stephanV Sep 16 2010, 09:19 PM
Well the last does kind of work. I'm only not allowed to instantiate the pointers to the childs at the constructor stage of the main form for some kind of reason...

EDIT: well scrap that. I guess I put something between the wrong braces.. now it works. smile.gif

Powered by Invision Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)