Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:14:20+00:00 2026-05-31T03:14:20+00:00

I added a panel to Form1 component. That panel is named panel1 . When

  • 0

I added a panel to Form1 component. That panel is named panel1.

When browsing a generic list, I want to add labels dynamically. Look to my little code:

if (list.Count > 0)
{

    foreach (TLClass item in list)
    {
        Label key = new Label();
        key.Text = item.Key;
        panel1.Container.Add(key);  //here throws an exception

        Label code = new Label();
        code.Text = item.Code.ToString();
        panel1.Container.Add(code);

        Label en = new Label();
        en.Text = item.Languages["EN-EN"].ToString();
        panel1.Container.Add(en);


        Label fr = new Label();
        fr.Text = item.Languages["FR-FR"].ToString();
        panel1.Container.Add(fr);

        Label nl = new Label();
        nl.Text = item.Languages["NL-NL"].ToString();
        panel1.Container.Add(nl);

        Label ro = new Label();
        ro.Text = item.Languages["RO-RO"].ToString();
        panel1.Container.Add(ro);

        Form1.ActiveForm.Container.Add(panel1);
    }
}

After line panel1.Container.Add(key);, immediately throws an exception NullReferenceException.

Why ? Where’s my mistake ?

enter image description here

Problem solved
I called with Controls instead of Container

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:14:21+00:00Added an answer on May 31, 2026 at 3:14 am

    You add controls to the container.
    Which should be the parent form, but when you Add the parent form is not set.
    However the right way is to add to the controls collection of the panel

    panel1.Controls.Add(key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form where controls are dynamically added to a Panel. However, when
I'm trying to add a collapsible panel to a panel I added in the
I'm currently working on a website admin panel, I added a form that adds
I have a JPanel that I want to add some components. in particular JButton
I have a form1.cs and in that form I have a panel1, in the
I have added checkbox selection model to grid panel. It works fine in IE8
I'm using an Ajax update panel and have recently added ASP.NET tracing code to
I added a custom install action to my installer to add one of my
Windows 7 has added on its Control Panel, Appearance and Personalization, Display setting, an
I have ExtJS View-Port panel , that contain center panel, that contain tablpanel ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.