Click here to Skip to main content
16,021,905 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi there,

in my WinForms application, i want to get the value of a TextBox in Form1, and then show this value on a label in Form2.

Could you tell me how to make that true?

Thanks very much!

Alright, firstly thanks Andrei. But what you give me is not acturally what i want.
I've sure searched it in internet already. And got what you wrote. But that's not exactlly
the key.

Maybe i should tell you what i wrote and where is my confusion.

GlobalClass.h:
C#
public class GlobalClass
{
public: String^ STR; //when "static int const i" it works!

};

also, the prog tells me it only accepts constant intergers, WHY?

i put this headerfile under Form1 and Form2, and try to use GlobalClass::STR, but the declaration in the "GlobalClass.h" caused already errors, so...
Need your help!
Posted
Updated 24-Sep-12 20:17pm
v2

Okay! I endlly got it by myself. As said, it's not such easy to use "set" & "get"
under C++/WinForm. You always need a class for the variables to be transfered. Because normal global variables are not any more possible to declare.
And make sure "using namespace System" to be included.

So, see my codes if it would help you:

GlobalClass.h:
C#
#pragma once

namespace Globals
{
    using namespace System;
    public ref class GlobalClass
    {
    public: static String^ g_STR;

    };
}


Form1.h:
C#
#pragma once
#include "Form2.h"
#include "GlobalClass.h"
namespace TEST {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace Globals; //Don't forget this!
    ...
private: System::Void textBox1_TextChanged(System::Object^  sender,  System::EventArgs^  e) {
				 GlobalClass::g_STR = textBox1->Text;
			 }
    ...

Save the value of textBox1 into the global string g_STR. With a button click to show Form2(method Show() or ShowDialog()).

Form2.h:
C#
#pragma once
#include "GlobalClass.h"
namespace TEST {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace Globals;//Don't forget this!
    ...
private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) {
			 label1->Text=GlobalClass::g_STR;
		 }
    ...

When Form2 is loaded, then label1 will show us the value of textBox1 from Form1.
Which is acturally transfered per g_STR.

There is also an article which says that it differs when you use Show()(modeless) as ShowDialog()(modal). But i didn't prove this. See link:
http://www.dreamincode.net/forums/topic/206458-how-to-get-values-from-form1-to-form2-or-any-other-forms/[^]

In conclusion, this method is for making a global variables class under C++/CLR/WinForm that helps you to get variables from one form to another.
I'm pleasured if this helps.
 
Share this answer
 
It's pretty straightforward.
Have a different class with a public property
C#
class Helper 
{
    public static string Form1Label { get; set; }
}

You set its value from Form1:
C#
Helper.Form1Label = Form1Textbox.Value;

and then in Form2 you simply access it:
C#
Form2Label.Text = Helper.Form1Label;

I am also downvoting your question for lack of any apparent effort on your part.
 
Share this answer
 
v2

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