質問

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?

役に立ちましたか?

解決

I'm guessing flowLayoutPanel.Container is null. I think you want

flowLayoutPanel.Controls.Add(progressbar);

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top