The code below is from my form.. I declared a string GLOBALLY.. Then upon FormLoad, It inserts values for the string... When I do button click, the string is blank.. Why is it blank? Shouldnt the Values from FormLoad Event be in it? :

public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	public:
		System::String^ StringF;     //<-- Tried to declare my string globally..

and then I did:

public: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
		Form1::Visible = false;
		Query ^ F = gcnew Query();   //<-- Form 2 for input.
		F->ShowDialog();
		System::String^ StringF = F->textBox1->Text;  //<-- Set StringF as Value from text box!!!
                MessageBox::Show(StringF);  //<---- Tells me the string has values :)
}

public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                   System::String^ AddStr = "FFox - " + StringF;
                   IntPtr ptr2 = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(AddStr);
				 HWND FFox = FindWindow(0, (LPCSTR)ptr2.ToPointer());
				 if(FFox == NULL)
				 {
					 MessageBox::Show("Cant Find FF :(");
					 MessageBox::Show(StringF);     //<--- Tells me the string is blank!!  WHY?!?!

				 }

Recommended Answers

All 3 Replies

The StringF in Form1_Load() is a local variable, which masks the "global" (really it's a member of the Form1 class) variable. Remove the System::String ^ portion off of the one in Form1_Load and it should be all set.

The StringF in Form1_Load() is a local variable, which masks the "global" (really it's a member of the Form1 class) variable. Remove the System::String ^ portion off of the one in Form1_Load and it should be all set.

Wow it worked thanks!! Although I tried that before and got some weird error.. Seems you may just be really good luck!

First post solved today :)


Also do u happen to know how to make my application constantly check if another app is open? Because I tried to add a loop but then my app stops responding. Maybe I added it in the wrong place :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.