Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI
Hi, guys! I hope you help me with this.
I have two forms, both with only buttons. I want to make a combined choice between the two.
 
1) Clicking on certain buttons in Form1 opens Form2 (that's working fine).
 
2) Clicking on certain buttons in Form2 must open other forms, depending on which buttons I clicked on both forms.
 
My code:
 
This is in Form1:
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
		 {
		 	 String ^ m_option1 = "1";//Project
			 Form2 ^form2 = gcnew Form2;
	                 form2->Show();
		 }
This is in Form2:
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
		{
			String ^ m_option2 = "1";//Add
			String ^ m_choice  = m_option1 + m_option2;
			Form8_add_tutor ^form8_add_tutor = gcnew Form8_add_tutor;
	                form8_add_tutor->Show();
		}
This is the error: m_option1 undeclared identifier.
NOTE: So far, this has given me the least errors. I tried making m_option1 public and it only made it worse.
THANKS IN ADVANCE
Posted 9-Mar-12 4:13am
Edited 9-Mar-12 4:17am
v4
Comments
SAKryukov at 9-Mar-12 10:21am
   
Do you know what is a variable, scope, visibility, in general?
--SA
Wilmer Lameda at 9-Mar-12 19:41pm
   
Yes, thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you want to share data between forms, your best bet is to make put data into static class that both forms can access. (You're going to have to convert the code below to C++.)
 
public static class Globals
{
    public static string MyString { get; set; }
}
  Permalink  
Comments
Wilmer Lameda at 9-Mar-12 9:24am
   
I tried that already, only I didn't add the { get; set; } part.
Thank you. I'll give it another shot.
SAKryukov at 9-Mar-12 10:35am
   
John, I have an impression that you did not read the question. Did you notice that OP tried to get access to the local (stack) variable from outside? For such person, you advice is no more than a spell... :-)
Please see my answer...
--SA
Wilmer Lameda at 11-Mar-12 11:32am
   
Mr Simmons, I tried your suggestion and I've done some research; but string variables were giving me a headache, so I switched to integers. Here's my code:
//global_variables.h
ref class VarGlobales
{
public:
void setvalue();
int getvalue();
private:
int myvalue;
int xxx;
};
void VarGlobales::setvalue(int xxx)
{
myvalue = xxx;
return;
}
int VarGlobales::getvalue()
{
int myvalue;
return myvalue;
}
And here's the error I'm getting:
error C2511: 'void VarGlobales::setvalue(int)' : overloaded member function not found in 'VarGlobales'
I've tried to fix it; but then I get different ones. Can you help?
By the way, I added the corresponding #include "global_variables.h" in the main program.
John Simmons / outlaw programmer at 11-Mar-12 19:15pm
   
Your setvalue method prototype doesn't have an argument defined for it.
Wilmer Lameda at 12-Mar-12 12:11pm
   
Right. Just didn't see it. Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please see my comment to the question. I don't think you are ready to do any development at this moment — not just yet. You declare a a variable m_option1, which is a stack variable. Not only it is no accessible out of the scope of the stack frame, if does not even exist on exit of the function. Same of different instance of the same of different class, access modifiers — all that is totally irrelevant.
 
You even tried to add an access modifiers to it. This is that kind of "engineering" when someone kicks a TV set until it starts showing picture. You have no chance. You should understand every single line you write. In this case, the problem is not any detail of syntax or something. It shows that you have no idea what is a variable and what is a function. No more no less. And I cannot explain it in a short answer. You need to grab a manual and start learning programming from the very beginning, this time with full understanding. Solve simple problems, to get a grip on the code.
As Yoda wrote:
Unlearn what you have learned.
—SA
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 9 Mar 2012
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