I want to define a vector of TextBox (or any compnents) in VC++ project (windows form).
I wrote :

std:vector <TextBox^> txt_box;
//
//then I push the elements back in the vector using

txt_box.push_back (textbox1)
//
//...
// for all the text_boxs in my form

but this code does not work.
if you have any idea plz help and thanks.

Recommended Answers

All 3 Replies

What do you mean "it does not work"? Would you take your car to a repair shop and tell the mechanic the same thing? I hope not.

Is that CLR/C++ code (e.g. Windows Forms) you posted? If so, you can not use the standard templates like you would in c++. Instead, use the files in include\clrext folder.

I put the code above in the namespace out side the class .And I put the push_back statement in a method in my class .and all of them are in form.h file.
what happened is the compiler displays an error message and it says that :

can't convert from 'TextBox^' to 'TextBox^ const&'

are there any way to solve this problem?

How is the TextBox object declared? Are you trying to use the same instance over and over because that won't work even if the compiler didn't complain.

I got this to work ok. The code I added is marked in Red

#pragma once
#include <cliext\vector>

namespace TestFormks {
	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
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
    private: System::Windows::Forms::TextBox^  textBox1;
    public: 
    private: System::Windows::Forms::TextBox^  textBox2;

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

	private:
        cliext::vector<TextBox^> txt_box;
		/// <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->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            this->txt_box.push_back(textBox1);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(91, 50);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 22);
            this->textBox1->TabIndex = 0;
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(91, 97);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(100, 22);
            this->textBox2->TabIndex = 1;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(282, 255);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
	};
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.