Initializing a control on a form

asked 2021-11-13T18:20:04.267+00:00
John 121 Reputation points

Now that I know how to reference a form, how would I initialize their controls within the header file?

#pragma once

namespace WinFormCDemo {

    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 MyForm1
    /// </summary>
    public ref class MyForm1 : public System::Windows::Forms::Form
    {
    public:
        MyForm1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm1()
        {
            if (components)
            {
                delete components;
            }
        }

    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->components = gcnew System::ComponentModel::Container();
            this->Size = System::Drawing::Size(300,300);
            this->Text = L"MyForm1";
            this->Padding = System::Windows::Forms::Padding(0);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        }
#pragma endregion
    };
}

Would I need another pragma region for the form's controls prior to initializing them?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,887 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,609 questions
No comments
{count} votes

Accepted answer
  1. answered 2021-11-13T19:22:09.623+00:00
    Castorix31 65,271 Reputation points

    If you place a control on the Form, like a Button, with the Designer, it will generate the code :

    private: System::Windows::Forms::Button^ button1;
    

    It adds in InitializeComponent :

    this->button1 = (gcnew System::Windows::Forms::Button());
    this->SuspendLayout();
    // 
    // button1
    // 
    this->button1->Location = System::Drawing::Point(102, 112);
    this->button1->Name = L"button1";
    this->button1->Size = System::Drawing::Size(75, 23);
    this->button1->TabIndex = 0;
    this->button1->Text = L"button1";
    this->button1->UseVisualStyleBackColor = true;
    //...
    this->Controls->Add(this->button1);
    //...
    this->ResumeLayout(false);
    

0 additional answers

Sort by: Most helpful