Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
How do I set the value of the Text property of my label1 on Form1, from within a function in the main project cpp file?

I can do it within the event handler inside the class, but that would mean writing all my code within the header file (Form1.h). Surely that's not correct practice????

Within fnTest() the intellisense prompts to the WindowsForms::Form1::label1, so that seems to be recognised.

I'm guessing the answer is simple (probably almost embarrasingly so - a head-slapping moment!), but I've searched the net (& this site) trying to get an answer without success. Maybe I'm using the wrong search terminology. The examples I can find all show the code residing in the event handlers in the header file, which I can already get to work, but I don't like the idea of that for code portability.

Posting the code but it is the stock standard IDE generated code. All I've added is fnTest() & the code inside the Form1_Load() handler.

Thanks in advance,
Dave.

Form1.h
#pragma once

void fnTest(int);

namespace WindowsForms {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    public: System::Windows::Forms::Label^  label1;
    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            //
            // label1
            //
            this->label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
            this->label1->Location = System::Drawing::Point(34, 38);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(100, 20);
            this->label1->TabIndex = 0;
            this->label1->Text = L"label1";
            this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 264);
            this->Controls->Add(this->label1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 label1->Text = "10";
                 fnTest();
             }
    };
}


C++ Test WinForms.cpp
// C++ Test WinForms.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace WindowsForms;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}

void fnTest() {
    WindowsForms::Form1::label1->Text = "11"; // <- this is the line that fails to build

}
Posted
Updated 12-Mar-11 14:19pm
v2

You need to access an instance object of your Form1 class - you can do something like this:

1. Add the following to your Form1 class definition - leaving label1 as a public field is usually not considered a good idea.
public:
 void SetLabel1Text(String^ txt)
 {
   label1->Text = txt;
 }


2. Access your new method through an instance reference to an object of type Form1
Form1^ mainForm = gcnew Form1();
mainForm->SetLabel1Text( L"Some text" );
// This should also work as long as label1 is public:
//   mainForm->label1->Text = L"Some text";
Application::Run(mainForm);


C++
private: System::Void Form1_Load(System::Object^   sender, System::EventArgs^ e) 
{
 label1->Text = "10"; // <-- works because Form1_Load a non-static member function of the Form1 class
 fnTest(); // <-- this method is not part of your class and needs access to an instance of your Form1 class
}


Update
This works like a charm (You should only have one message loop - Application::Run - in your program):
// SampleWinforms.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace SampleWinforms;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    // Create the main window and run it
    Form1^ mainForm = gcnew Form1();
    mainForm->label1->Text = L"Some Text";
    Application::Run(mainForm);
    return 0;
}


I've added a button "button1" and assigned the following event handler
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
 Form1^ newForm = gcnew Form1();
 newForm->label1->Text = L"Some Text On New Form";
 newForm->Show();
}

This lets me create a new window and modify the label before showing it.

Update 2
To change the Text property of label1 on the current form:
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{ 
 label1->Text = L"Some Text On the Current Form"; 
}


Assuming that there will only be a single instance of Form1, the following adds a static property Instance enabling you to write something like Form::Instance->label1->Text = L"Simple singleton";.
Since Form::Instance is static it will allow you to access the form, as long as it exists, from any code that includes the "form1.h"
C++
public ref class Form1 : public System::Windows::Forms::Form
 {
private:
  typedef System::Windows::Forms::Form base;

  static Form1^ instance;
public:
  Form1(void)
  {
   InitializeComponent();
   instance = this;
  }

  property static Form1^ Instance
  {
   Form1^ get()
   {
    return instance;
   }
  }

protected:
  virtual void OnFormClosed(FormClosedEventArgs^ e) override
  {
   base::OnFormClosed(e);
   instance = nullptr; 
  }
// Rest of definition
}



Regards
Espen Harlinn
 
Share this answer
 
v6
Comments
DaveLock 12-Mar-11 7:51am    
Thanks so much for your reply, Espen, much appreciated.

It bulds ok, but at runtime throws an error at the Application::Run(mainForm); line.
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead."

If I just comment out the Application::Run(mainForm); line, or if I also try the line you commented should work, then it builds & runs without error, but doesn't change the value of the label1 on Form1 (it does nothing).
Dave.
DaveLock 12-Mar-11 21:40pm    
UpdateThat builds & runs ok, but doesn't do what I was asking for.

Firstly, it seems to me that this way the text is being set BEFORE the form opens. I want to change the text of label1 on the form that is ALREADY OPEN (now called mainForm), whilst it is open. I don't want to create new forms with the text in label1 already set.

Secondly, putting the code in the event handler worked for me right from the start, but as I said in my 2nd para of the question above, that means all of the code is in the header files, because the handlers are in the class which is in the form's header file.

And thirdly, the button just creates new forms, so the more it is clicked, the more forms are opened.

I like the way you declared mainForm as a Form1 object before putting that into Application::Run(). Is there any way to make that declaration of the mainForm object, or a pointer to the mainForm object, global throughout that cpp file? Couldn't I then use that pointer as my reference to change the text from a function in the cpp file? Something like 'ptr_mainForm->label1->Text = L"some text";', where 'ptr_mainForm' is the global pointer to the mainForm object.
Thanks for your help,
Dave.
DaveLock 13-Mar-11 4:39am    
Update 2:

That handler code only builds for me if it is located WITHIN THE CLASS, which is WITHIN THE HEADER FILE.

I sincerely appreciate your help, but the solution you are giving me I stated in the 2nd paragraph of my original question, that I have already done. I want to achieve that same result, but WITHIN A FUNCTION IN THE SampleWinforms.cpp FILE (such as fnTest is). Is this possible?

I'm starting to think that maybe VC++ sucks at WinForms development. Hmmm.

Dave.
Espen Harlinn 13-Mar-11 5:18am    
It's possible, you just need a way to let your code get access to an instance of Form1 - this requirement doesn't change with the programming language.
DaveLock 14-Mar-11 6:15am    
It works!

FYI, as typed it had 4 build errors with:
Form::Instance->label1->Text = L"Simple singleton";

But it builds & worked when I changed it to:
Form1::Instance->label1->Text = L"Simple singleton";

That is exactly what I was wanting. Thank you so much for your help & patience, it is very much appreciated. Now, as you said, the properties can be changed from any code file that includes the Form.h. Beautiful!

Woohoo!
Dave.
It chops the code for Form1.h

...

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// label1
//
this->label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
this->label1->Location = System::Drawing::Point(34, 38);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(100, 20);
this->label1->TabIndex = 0;
this->label1->Text = L"label1";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 264);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "10";
fnTest();
}
};
}
 
Share this answer
 

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