Click here to Skip to main content
12,250,257 members (47,601 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++/CLI .NET
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:
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 24-Sep-12 4:10am
Edited 24-Sep-12 20:17pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It's pretty straightforward.
Have a different class with a public property
class Helper 
{
    public static string Form1Label { get; set; }
}
You set its value from Form1:
Helper.Form1Label = Form1Textbox.Value;
and then in Form2 you simply access it:
Form2Label.Text = Helper.Form1Label;
I am also downvoting your question for lack of any apparent effort on your part.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
#pragma once

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

Form1.h:
#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:
#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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2016
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