471,570 Members | 937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Panel and Checkbox have been created and NullReferenceException wasthrown ...

Hello

I've the following code

Panel pnlData = new System.Windows.Forms.Panel();
CheckBox checkBox = new System.Windows.Forms.CheckBox();
checkBox.AutoSize = true;
checkBox.Dock = System.Windows.Forms.DockStyle.Top;
checkBox.Name = "checkBox";
checkBox.TabIndex = 1;
checkBox.Text = "checkBox";
checkBox.UseVisualStyleBackColor = true;
try
{
pnlData.Container.Add(checkBox);
}
catch (NullReferenceException a)
{ }

And there the NullReferenceException will be thrown alwas.

Can me someone tell why?

Thanks for all

Best regars
Wolfgang Uhr
Feb 9 '06 #1
4 3083
Hey there Wolfgang,

The problem is that container is null when you are trying to add. I'm
assuming you are wanting to make the checkbox appear in the panel, in which
case you actually just need to replace:

pnlData.Container.Add(checkBox);

with:

pnlData.Controls.Add(checkBox);

and that will place you checkbox onto the panel control. Hope that helps!

"Wolfgang Uhr" wrote:
Hello

I've the following code

Panel pnlData = new System.Windows.Forms.Panel();
CheckBox checkBox = new System.Windows.Forms.CheckBox();
checkBox.AutoSize = true;
checkBox.Dock = System.Windows.Forms.DockStyle.Top;
checkBox.Name = "checkBox";
checkBox.TabIndex = 1;
checkBox.Text = "checkBox";
checkBox.UseVisualStyleBackColor = true;
try
{
pnlData.Container.Add(checkBox);
}
catch (NullReferenceException a)
{ }

And there the NullReferenceException will be thrown alwas.

Can me someone tell why?

Thanks for all

Best regars
Wolfgang Uhr

Feb 10 '06 #2
Hi Wolfgang,

Thanks for posting!

From the snippets of the current code, I have added the code into the Form
Load event in my own application. I can not reproduce the current issue
because all codes work fine. So I think maybe the null reference error is
related to the context for the current application. Could you please make a
simple demo and send it to me? This will help me to repro the current issue
and find some factors. My alias is v-****@online.microsoft.com (remove
..online). Thanks for your understanding!

I'm looking forward your reply.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support

Feb 10 '06 #3
Justin Swan (MSFT) schrieb:
pnlData.Container.Add(checkBox);

with:

pnlData.Controls.Add(checkBox);


Thank you very much this helps well.

Wolfgang Uhr
Feb 10 '06 #4
Yuan Ren[MSFT] schrieb:
Could you please make a
simple demo and send it to me? This will help me to repro the current issue
and find some factors. My alias is v-****@online.microsoft.com (remove
online). Thanks for your understanding!


I have just installed the very last Studio-Version 8.0.50727.42
(RTM.050727-4200) - German.

And realy: If I use pnlTreeData.Container.Add(checkBox), then the
command throws an error and if I use pnlTreeData.Controls.Add(checkBox),
the program works well.

I have used Container because this command will be used if i put a
Checkbox from the toolbox to the panel manually.

Ok, I'll use Container.

Best Regards
Wolfgang Uhr
Feb 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Doug | last post: by
3 posts views Thread by *no spam* | last post: by
9 posts views Thread by Craig Andrews | last post: by
1 post views Thread by Manny Chohan | last post: by
2 posts views Thread by Peteroid | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.