繁体   English   中英

动态添加的winforms控件不显示?

[英]Dynamically added winforms control Not displaying?

我有这个自定义控件,基本上是一个面板:

class ResultPanel : Panel {
    Label scoreValueLabel = new Label();

    public ResultPanel() : base(){
        scoreValueLabel.AutoSize = true;
        scoreValueLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        scoreValueLabel.Location = new System.Drawing.Point(265, 99);
        scoreValueLabel.Name = "scoreValueLabel";
        scoreValueLabel.Size = new System.Drawing.Size(49, 25);
        scoreValueLabel.TabIndex = 10;
        scoreValueLabel.Text = "+10";
        Controls.Add(scoreValueLabel);
    }
}

我正在尝试将其添加到事件处理程序的面板中:

private void ResultsReceivedHandler(object sender, List<QuestionResult> results) {

        ResultPanel resultPanel = new ResultPanel();
        allResultsPanel.Controls.Add(new ResultPanel());
        resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
        resultPanel.BorderStyle = BorderStyle.FixedSingle;
        resultPanel.Location = new Point(0, 155);
        resultPanel.Name = "questionResultPanel";
        resultPanel.Size = new Size(325, 148);
        resultPanel.TabIndex = 0;

    }

我知道ResultPanel的实例可以显示在allResultsPanel中,因为我已经向allResultsPanel添加了(使用设计器视图)一个ResultPanel,该ResultPanel的大小与allResultsPanel顶部的大小相同,并且可以显示。

allResultsPanel只是一个普通的Panel btw,它的高度足以容纳控件,因为其高度为800。

那么,为什么我可以看到通过设计视图添加的控件却看不到动态添加的控件呢?

设置resultPanel

ResultPanel resultPanel = new ResultPanel();
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;

您正在向allResultsPanel添加另一个新面板

allResultsPanel.Controls.Add(new ResultPanel());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM