.NET Question.

Hi, im new to .NET C++ programming, and have a simple question to ask. When i create a simple application using Windows Form Creator and add a richTextBox to the form, i get the following code(see below). And Ive added a simple function (EditText()) at the end of the file which isnt in the name space. What i want to know is how can i access the richTextBox1(which i created in the namespace) outside of the namespace. Is that possible Because i'd prefer to have most of my functions in a seperate header file instead of being cramped up into one file. Thanks in advance for any help.

#pragma once

namespace test_app {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

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

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::RichTextBox^ richTextBox1;

protected:

private:

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

void InitializeComponent(void)

{

this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());

this->SuspendLayout();

this->richTextBox1->Location = System::Drawing::Point(133, 158);

this->richTextBox1->Name = L"richTextBox1";

this->richTextBox1->Size = System::Drawing::Size(100, 96);

this->richTextBox1->TabIndex = 0;

this->richTextBox1->Text = L"";

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(292, 266);

this->Controls->Add(this->richTextBox1);

this->Name = L"Form1";

this->Text = L"Form1";

this->ResumeLayout(false);

}

#pragma endregion

}; //end of Form class

} //end of namespace

void EditText()

{

Form1->richTextBox1->AppendText("string here"); //how do i access the richTextBox1 from the namespace when the function is outside of it.

}



Answer this question

.NET Question.

  • emkatu

    Actually, I think the problem is one of accessibility. By default, controls that are added through the Form Designer are private. Which means that the properties and methods associated with the control are not accessible from outside of the Form class. Your choices are to either change the access modifier on the control to be Public or to expose a public method that takes a string as a parameter and appends it to the TextBox1 control.



  • bostik11

    A solution is to create custom controls with "public" (I prefer "Friend") accessors and methods.

    It's a lot of work, but on a huge project, it can be really helpfull

    Regards

    Tukkkko
    Architect


  • Narula

    Someone else helped me out and showed me a solution to my problem.

    using namespace test_app;

    void sendstring()

    {

    String^ s = "test string";

    ((Form1^)(Application::OpenForms[0]))->test(s);

    }

    This lets me access the Forms control's outside of the namespace.


  • Padraic24

    if i understand your question - you want to access some component outside of the current namespace which lies in another namespace.

    if this is correct then usually its:

    theNameSpace.Component.Whatever....

    I program in C# so no idea how you would do this in C++ but i think the idea is the same....



  • image002

    Solution to this problem lies in the situation, where u want to access this function.

    First of all u can access the textbox's object only when form has been displayed ( forms object has been created ).

    problem is not with the namespace its with the object. Solution that ur frnd gave is using the current object of the form, thats why it will work. but only when if either ur form is opned or its object created somewhere before ur function.

    If u want to access the property of a class either create an object first or use static functions. But static functions wont work as ur textbox isnt static.

    can u specify where u would like to call ur function.



  • xyz8994

    Thanks for your replys, I've changed the richEditText control to public, and i still cant access the richEditText from outside of the control, also i created a function which passes a String to a function inside of the namespace, but that also didnt work. Here's what i have aswell as the error code shown below. Any help would be appreciated. Thanks.

    #pragma once

    namespace test_app {

    using namespace System;

    using namespace System::ComponentModel;

    using namespace System::Collections;

    using namespace System::Windows::Forms;

    using namespace System::Data;

    using namespace System::Drawing;

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

    {

    public:

    Form1(void)

    {

    InitializeComponent();

    }

    public:

    ~Form1()

    {

    if (components)

    {

    delete components;

    }

    }

    public: System::Windows::Forms::RichTextBox^ richTextBox1;

    //protected:

    System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code

    void InitializeComponent(void)

    {

    this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());

    this->SuspendLayout();

    //

    // richTextBox1

    //

    this->richTextBox1->Location = System::Drawing::Point(133, 112);

    this->richTextBox1->Name = L"richTextBox1";

    this->richTextBox1->Size = System::Drawing::Size(100, 96);

    this->richTextBox1->TabIndex = 0;

    this->richTextBox1->Text = L"";

    //

    // Form1

    //

    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

    this->ClientSize = System::Drawing::Size(292, 266);

    this->Controls->Add(this->richTextBox1);

    this->Name = L"Form1";

    this->Text = L"Form1";

    this->ResumeLayout(false);

    }

    #pragma endregion

    public: void test(String^ s)

    {

    richTextBox1->AppendText(s);

    }

    };

    }

    void sendstring()

    {

    String^ s = "test string";

    test_app::Form1::test(s); //'test_app::Form1::test' : illegal call of non-static member function

    }


  • .NET Question.