Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, I've been doing my project but when I try to write the state a checkbox on another form

First wrote this code but did note that error:

C++
Form2::checkBox1->Checked=true;


Error:
A referral from a non-static member must be relative to a specific object

So I decided to search in google to expand my knowledge and I found this code but neither has worked for me :/ ..:

C++
Form2 ^formA = gcnew Form2();

formA::checkBox1->Checked=true



Error 1 error C2065: 'formA' : identificador no declarado

Error 2 error C2065: 'r' : identificador no declarado


I also found another code that replaces formA to frm2 but also caused by the same error. :/

Some Solution ?

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 22:58pm    
C++ tag is confusing. This is C++/CLI, for .NET.
—SA

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

With C++ and C++/CLI, due to lack of partial classes, my advice of using partial classes found in my past answers is inapplicable; just ignore this part.

—SA
 
Share this answer
 
In addition to solution 1, there are some alternatives that might be usefull in some cases.

In simple case, the second form might have 1 or 2 properties.

A static Show method (like MessageBox::Show) could be used if there are a lot of varied caller.

If the second form is complex, then an idea might be to have a class that represent the data (as in Model-View-Controler pattern) and be able to set or get the data object from the form.
 
Share this answer
 
Forgive, but do not understand much of what you meant in your other question.

They could give me a code to help me finish my proyect?
 
Share this answer
 
Comments
Philippe Mori 17-Jan-13 19:09pm    
Well, if you want to go with properties, then you can look at MSDN documentation on
them: property. You would have a property in the second form and that property would change the state of the check box. As mentionned in my answer, using a simple type property might be adequate for very small number of properties (say < 3).

If this is not clear enough, then I would suggest you to first learn the basic of the C++/CLI language...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900