Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows form
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:
 
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 :/ ..:
 
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 16-Jan-13 17:15pm
Comments
Sergey Alexandrovich Kryukov at 16-Jan-13 22:58pm
   
C++ tag is confusing. This is C++/CLI, for .NET.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?
  Permalink  
Comments
Philippe Mori at 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)

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100