简体   繁体   中英

Dynamically added winforms control Not displaying?

I have this custom control which is basically a panel:

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

And I'm trying to add it to a panel in an event handler:

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;

    }

I know that an instance of ResultPanel can be displayed in allResultsPanel because I have added(using designer view) a ResultPanel to allResultsPanel that has the same size as this one at the top of allResultsPanel and that displays.

allResultsPanel is just a normal Panel btw, and its big enough to fit the control because its height is 800.

So why can i see the control added through the design view but not one added dynamically?

While setting up 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;

You are adding another new panel to the allResultsPanel

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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