Reputation: 698
I want to be able to add some amount of progressbars into the form (the number of progressbars can vary from user to user).
I have written this code to add 1 progressbar:
ProgressBar progressbar = new System.Windows.Forms.ProgressBar();
progressbar.Name = "progressBar1";
progressbar.Size = new Size(280, 30);
progressbar.TabIndex = 9;
progressbar.Show();
flowLayoutPanel.Container.Add(progressbar);
When I run this code, I get an Exception: "Object reference not set to an instance of an object."
.
What bothers me is that both the progressbar and flowLayoutPanel aren't null when I look at them using IDE. What have I done wrong?
Upvotes: 0
Views: 2053
Reputation: 152501
I'm guessing flowLayoutPanel.Container
is null. I think you want
flowLayoutPanel.Controls.Add(progressbar);
Upvotes: 4
Reputation: 9418
progressbar.Show();
this line should not be necessary
did you put this code before InitializeComponents
? then flowLayoutPanel
is null because in this function all form elements are initialized
then, the sub elements are in FlowLayoutPanel.Controls
, not container
Upvotes: 1