creating custom control

Apr 4, 2012 at 12:22am
I am trying to create a custom control that can be placed on forms.

I created a new component and got it to show in the tool box. I think I did something wrong because when I try to drag it into form1, the icon drops to the bottom of the design page. When I try to change the parent property I get a compiler error saying that parent is not a member of my component.


Below is the source code of my control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;


namespace GameOfLife {

	/// <summary>
	/// Summary for grid
	/// </summary>
	public ref class grid :  public System::ComponentModel::Component
	{
	public:
		grid(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
		grid(System::ComponentModel::IContainer ^container)
		{
			/// <summary>
			/// Required for Windows.Forms Class Composition Designer support
			/// </summary>

			container->Add(this);
			InitializeComponent();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~grid()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
	private: System::Windows::Forms::Button^  but_advance;
	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->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
			this->but_advance = (gcnew System::Windows::Forms::Button());
			// 
			// tableLayoutPanel1
			// 
			this->tableLayoutPanel1->ColumnCount = 2;
			this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
				50)));
			this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
				50)));
			this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Top;
			this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
			this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
			this->tableLayoutPanel1->RowCount = 2;
			this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
			this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
			this->tableLayoutPanel1->Size = System::Drawing::Size(200, 100);
			this->tableLayoutPanel1->TabIndex = 0;
			// 
			// but_advance
			// 
			this->but_advance->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->but_advance->Location = System::Drawing::Point(0, 0);
			this->but_advance->Name = L"but_advance";
			this->but_advance->Size = System::Drawing::Size(75, 23);
			this->but_advance->TabIndex = 0;
			this->but_advance->Text = L"button1";
			this->but_advance->UseVisualStyleBackColor = true;

		}
#pragma endregion
	};
}
Apr 4, 2012 at 5:12am
1. You created a component, not a control. Inherit from Control and not from Component.
2. Almost nobody knows C++/CLI around here. If you have more questions/issues, best if you post @ the MSDN forums.
Apr 4, 2012 at 8:08pm
Thank you for the information

I solved the problem by making 2 changes:

1) adding the following line at the top of the code:
using namespace System::Windows::Forms;

2) changing the class declaration as follows:
public ref class grid : public System::Windows::Forms::Panel

----------------------------.------------------------------------------------------------------------------------

I'm having another problem now. My custom control grid has a button as a child control. When I drag grid onto form1, I see the button on the design pane of the compiler. However when I compile and run the program the button does not appear!

grid code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once

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


namespace GameOfLife {

	/// <summary>
	/// Summary for grid
	/// </summary>
	public ref class grid :  public System::Windows::Forms::Panel 
	{
	public:
		grid(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			but_advance->Parent = this;

		}
		grid(System::ComponentModel::IContainer ^container)
		{
			/// <summary>
			/// Required for Windows.Forms Class Composition Designer support
			/// </summary>

			container->Add(this);
			InitializeComponent();
		}

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

	private: System::Windows::Forms::Button^  but_advance;
	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->but_advance = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// but_advance
			// 
			this->but_advance->Location = System::Drawing::Point(100, 100);
			this->but_advance->Name = L"but_advance";
			this->but_advance->Size = System::Drawing::Size(75, 23);
			this->but_advance->TabIndex = 0;
			this->but_advance->Text = L"button1";
			this->but_advance->UseVisualStyleBackColor = true;
			// 
			// grid
			// 
			this->Dock = System::Windows::Forms::DockStyle::Fill;
			this->ResumeLayout(false);

		}
#pragma endregion
	};
}


form1 code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

namespace GameOfLife {

	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 GameOfLife;

#include "grid.h"

	/// <summary>
	/// Summary for Form1
	/// </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;
			}
		}
	private: GameOfLife::grid^  grid1;
	protected: 

	protected: 


	protected: 
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#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->grid1 = (gcnew GameOfLife::grid(this->components));
			this->SuspendLayout();
			// 
			// grid1
			// 
			this->grid1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->grid1->Location = System::Drawing::Point(0, 0);
			this->grid1->Name = L"grid1";
			this->grid1->Size = System::Drawing::Size(284, 262);
			this->grid1->TabIndex = 0;
			this->grid1->Text = L"grid1";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->grid1);
			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) {
		
			 
			 }
	};
}


Apr 4, 2012 at 9:21pm
Refer to #2 above.
Topic archived. No new replies allowed.