Winforms 无法从第二个控件显示窗口窗体

Winforms 无法从第二个控件显示窗口窗体,winforms,visual-c++,Winforms,Visual C++,我正在尝试在运行时添加自定义控件。因此,我使用自己的绘制方法创建了一个自定义控件。每当单击“添加”按钮时,就会创建一个新控件并将其添加到主窗体中。但在添加控件时,我无法看到其他控件,只能看到第一个控件。我不知道发生了什么事,有人能帮忙吗?。提前谢谢 public ref class CustomLine : public UserControl { private: Point P1,P2; Pen ^pen; public: CustomLine(Point p1, P

我正在尝试在运行时添加自定义控件。因此,我使用自己的绘制方法创建了一个自定义控件。每当单击“添加”按钮时,就会创建一个新控件并将其添加到主窗体中。但在添加控件时,我无法看到其他控件,只能看到第一个控件。我不知道发生了什么事,有人能帮忙吗?。提前谢谢

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();
         }


立法会的意见解决了这个问题。谢谢你

在自定义类中,没有初始化控件的属性“Location”和“Size”。初始化这些属性后,控件开始按预期显示在表单中

public ref class CustomLine : public UserControl
{
private:
    Point P1,P2;
    Pen ^pen;
public:
    CustomLine(Point p1, Point p2)
    {
        P1 = p1;
        P2 = p2;
        this->Location = P1;
        this->Size = System::Drawing::Size(Point(P2.X - P1.X, P2.Y - P1.Y));
        pen = gcnew Pen(Color::Red,2);
    }
protected:
    virtual void OnPaint(System::Windows::Forms::PaintEventArgs ^e) override
    {
        e->Graphics->DrawLine(pen,P1,P2);
    }
};

您的控件没有位置和大小…谢谢。添加位置和大小后,所有控件都可见。回答您自己的问题很好-事实上,在这里这样做是正确的。但是对于下一个来找你的人,你应该把问题是什么以及你是如何解决的放在你的答案中。请删除这个答案