I am trying to add a custom control at runtime. So i created a custom control with its own paint method. Whenever the "Add" button clicked, a new control is created and added to main form. But While adding controls,I cannot see others, but first control only. I have no idea about what is happening, Can anyone help?. Thanks in advance.
public ref class CustomLine : public UserControl
{
private:
Point P1,P2;
Pen ^pen;
public:
CustomLine(Point p1, Point p2)
{
P1 = p1;
P2 = p2;
pen = gcnew Pen(Color::Red,2);
}
protected:
virtual void OnPaint(System::Windows::Forms::PaintEventArgs ^e) override
{
e->Graphics->DrawLine(pen,P1,P2);
}
};
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int xx1 = Convert::ToInt32(this->x1->Text);
int yy1 = Convert::ToInt32(this->y1->Text);
int xx2 = Convert::ToInt32(this->x2->Text);
int yy2 = Convert::ToInt32(this->y2->Text);
CustomLine ^cline = gcnew CustomLine(Point(xx1,yy1),Point(xx2,yy2));
this->Controls->Add(cline);
this->Invalidate();
}