Ruslan Ibragimov
Ruslan Ibragimov

Reputation: 37

How to access to Form components from another class

I have WinForm and a class, I am trying to set text to Form component from class, but it let me access any components when calling from class, even if this components are declared as public. Any ideas why this is not working? Would be very thankful, I am sorry if this stupid question, I am kind of noob. Thank you!

OpenGL_on_a_Windows_Form.cpp

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


using namespace OpenGL_on_a_Windows_Form;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}

Form1.h

#pragma once

#include "OpenGL.h"
#include <iomanip>
#include <msclr/marshal_cppstd.h>

namespace OpenGL_on_a_Windows_Form {

    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 OpenGLForm;
    using namespace System::Diagnostics;
    using namespace System::IO;


    public ref class Form1 : public System::Windows::Forms::Form
    {

    public:
        Form1(void)
        {
            InitializeComponent();
            OpenGL = gcnew COpenGL(splitContainer1->Panel2, splitContainer1->Panel2->Width, splitContainer1->Panel2->Height);

        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::ComponentModel::IContainer^ components;
    protected:
    private: System::Windows::Forms::Timer^ timer1;
    public: System::Windows::Forms::TextBox^ MessageLog;

    private:

        OpenGLForm::COpenGL^ OpenGL;

#pragma region Windows Form Designer generated code
        

        void InitializeComponent(void)
        {
            this->components = (gcnew System::ComponentModel::Container());
            System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
            this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
            this->splitContainer1 = (gcnew System::Windows::Forms::SplitContainer());
            this->splitContainer2 = (gcnew System::Windows::Forms::SplitContainer());

            this->MessageLog = (gcnew System::Windows::Forms::TextBox());
            //....etc

            // MessageLog
            // 
            this->MessageLog->Dock = System::Windows::Forms::DockStyle::Fill;
            this->MessageLog->Location = System::Drawing::Point(0, 0);
            this->MessageLog->Multiline = true;
            this->MessageLog->Name = L"MessageLog";
            this->MessageLog->ReadOnly = true;
            this->MessageLog->Size = System::Drawing::Size(140, 261);
            this->MessageLog->TabIndex = 0;
            this->MessageLog->Text = L"Message Log:";
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(1183, 674);
            //....etc

        }
#pragma endregion
    private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
    {
        UNREFERENCED_PARAMETER(sender);
        UNREFERENCED_PARAMETER(e);
        OpenGL->Render();

    }


};
}

OpenGL.h

#pragma once

#include <windows.h>

#include <iostream>
#include <cstdlib> 
#include "lib.h"
#include <algorithm>
#include <array>
#include <vector>
#include <GL/gl.h>
#include <glut.h>
#include <msclr/marshal_cppstd.h>



using namespace System::Windows::Forms;
using namespace System::Diagnostics;



namespace OpenGLForm
{
    public ref class COpenGL : public System::Windows::Forms::NativeWindow
    {



    public:


        COpenGL(System::Windows::Forms::SplitterPanel^ parentForm, GLsizei iWidth, GLsizei iHeight)
        {
            CreateParams^ cp = gcnew CreateParams;

            cp->X = 5;
            cp->Y = 5;
            cp->Width = parentForm->Height - 5;
            cp->Height = parentForm->Width - 5;


            cp->Parent = parentForm->Handle;

            cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED;

            this->CreateHandle(cp);

            m_hDC = GetDC((HWND)this->Handle.ToPointer());

            if (m_hDC)
            {
                MySetPixelFormat(m_hDC);
            }

        }


        System::Void Render(System::Void)
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

        }


    private:
        HDC m_hDC;
        HGLRC m_hglrc;
        GLfloat rtri;               
        GLfloat rquad;          

    protected:
        ~COpenGL(System::Void)
        {
            this->DestroyHandle();
        }

        GLint MySetPixelFormat(HDC hdc)
        {
            return 1;
        }

        void set_text()
        {
            Form1::MessageLog::Text = "some text"; //---Problem is here, Form1 is not a class or numspace name

        }



    };

    
}

Upvotes: 1

Views: 319

Answers (2)

Александр
Александр

Reputation: 11

Class.cs: public void Function(Form form) { form.component.something(); } In Form class: ... Class class = new Class(); class.Function(this); ...

Upvotes: 0

Doc Brown
Doc Brown

Reputation: 20044

The set_text function needs to use a specific Form object, like this

    void set_text()
    {
        mainForm->MessageLog->Text = "some text"; 
    }

To make this work, mainForm needs to be a member variable of COpenGL of type System::Windows::Forms::Form^, and this member should be initialized in the constructor (resulting in a constructor call like this):

    OpenGL = gcnew COpenGL(this, splitContainer1->Panel2, splitContainer1->Panel2->Width, splitContainer1->Panel2->Height);

Alternatively, instead of passing the whole Form object to the COpenGL object, just pass a reference to the message log and store it in a member variable. Something along the lines of

public ref class COpenGL : public System::Windows::Forms::NativeWindow
{

   System::Windows::Forms::TextBox^ messageLog;

   public:
   
   COpenGL( System::Windows::Forms::TextBox^ messageLog,
       System::Windows::Forms::SplitterPanel^ parentForm, 
       GLsizei iWidth, GLsizei iHeight)
    {
        this->messageLog=messageLog;
        // ...
    }

    void set_text()
    {
       messageLog->Text = "some text"; 
    }
}

Upvotes: 1

Related Questions