如果未将Windows窗体C#组件添加到窗体中,则不会将其添加到窗体中。

因此,基本上,我有一个Filter类,该类生成一个面板,其中包含我需要使用该过滤器的组件(以及其他东西)

   public Panel panel;
    public Filter(string labelText)
    {
        Panel panel = new Panel();
        Label label = new Label();
        label.Text = labelText;

        panel.Controls.Add(label);
        panel.Visible = true;
        panel.BackColor = Color.Black;

    }

    public Panel GetFilter()
    {
        return panel;
    }
}

然后在表单代码上,调用GetFilter()以添加面板

 Filter f = new Filter("name")
 Controls.Add(f.GetFilter());

过滤器未显示,hmm可以查看过滤器中的代码是否编写正确。

        //copy of the Fiter constructor
        Panel panel = new Panel();
        Label label = new Label();
        label.Text = "label text";

        panel.Controls.Add(label);
        panel.Visible = true;
        panel.BackColor = Color.PowderBlue;


        Controls.Add(panel);

做得好,最后一件事,就是让我看看是否可以使用过滤器类中的面板

  public Panel panel;
    public Filter(string labelText,Form f)
    {

        Panel panel = new Panel();
        Label label = new Label();
        label.Text = labelText;

        panel.Controls.Add(label);
        panel.Visible = true;
        panel.BackColor = Color.Black;
        f.Controls.Add(f);
    }

    public Panel GetFilter()
    {
        return panel;
    }
哎呀,那也行得通,我在这里想念的是什么?以上工作示例都不适合我,因为将从抽象类调用过滤器以将其添加到父面板。要显示面板我必须做什么?

pd:背景色用于测试是否添加了面板。

zchng 回答:如果未将Windows窗体C#组件添加到窗体中,则不会将其添加到窗体中。

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3147215.html

大家都在问