How to add panel into another panel [duplicate]
This question already has an answer here:
What is a NullReferenceException, and how do I fix it?
31 answers
I want to add one of panel1,2,... into panel0 when I click on button1 .
It's my code
private void button3_Click(object sender, EventArgs e)
{
int i = 0;
int j = 0;
Panel pnl = new Panel[3];
while ( i<2)
{
pnl[i].Parent = panel0; // erorr
pnl[i].BackColor = Color.PeachPuff;
pnl[i].Size = new Size(50, 10);
pnl[i].Location = new Point(j+5,10);
panel0.Controls.Add(pnl[i]);
pnl[i].BringToFront();
i++;
j = j + 13;
}
}
Have a suggestion ?
c# forms panel
marked as duplicate by Liam, slugster
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is a NullReferenceException, and how do I fix it?
31 answers
I want to add one of panel1,2,... into panel0 when I click on button1 .
It's my code
private void button3_Click(object sender, EventArgs e)
{
int i = 0;
int j = 0;
Panel pnl = new Panel[3];
while ( i<2)
{
pnl[i].Parent = panel0; // erorr
pnl[i].BackColor = Color.PeachPuff;
pnl[i].Size = new Size(50, 10);
pnl[i].Location = new Point(j+5,10);
panel0.Controls.Add(pnl[i]);
pnl[i].BringToFront();
i++;
j = j + 13;
}
}
Have a suggestion ?
c# forms panel
marked as duplicate by Liam, slugster
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
2
well, your panel hasn't initialized yet, you only call the array constructor, but you should still usepnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls ofpanel0
, as both do pretty much the same
– Icepickle
Jan 2 at 12:06
1
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
It's windows form
– jsmlz
Jan 2 at 12:13
add a comment |
This question already has an answer here:
What is a NullReferenceException, and how do I fix it?
31 answers
I want to add one of panel1,2,... into panel0 when I click on button1 .
It's my code
private void button3_Click(object sender, EventArgs e)
{
int i = 0;
int j = 0;
Panel pnl = new Panel[3];
while ( i<2)
{
pnl[i].Parent = panel0; // erorr
pnl[i].BackColor = Color.PeachPuff;
pnl[i].Size = new Size(50, 10);
pnl[i].Location = new Point(j+5,10);
panel0.Controls.Add(pnl[i]);
pnl[i].BringToFront();
i++;
j = j + 13;
}
}
Have a suggestion ?
c# forms panel
This question already has an answer here:
What is a NullReferenceException, and how do I fix it?
31 answers
I want to add one of panel1,2,... into panel0 when I click on button1 .
It's my code
private void button3_Click(object sender, EventArgs e)
{
int i = 0;
int j = 0;
Panel pnl = new Panel[3];
while ( i<2)
{
pnl[i].Parent = panel0; // erorr
pnl[i].BackColor = Color.PeachPuff;
pnl[i].Size = new Size(50, 10);
pnl[i].Location = new Point(j+5,10);
panel0.Controls.Add(pnl[i]);
pnl[i].BringToFront();
i++;
j = j + 13;
}
}
Have a suggestion ?
This question already has an answer here:
What is a NullReferenceException, and how do I fix it?
31 answers
c# forms panel
c# forms panel
edited Jan 2 at 12:23
slugster
42.3k1282127
42.3k1282127
asked Jan 2 at 12:02
jsmlzjsmlz
32
32
marked as duplicate by Liam, slugster
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Liam, slugster
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
2
well, your panel hasn't initialized yet, you only call the array constructor, but you should still usepnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls ofpanel0
, as both do pretty much the same
– Icepickle
Jan 2 at 12:06
1
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
It's windows form
– jsmlz
Jan 2 at 12:13
add a comment |
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
2
well, your panel hasn't initialized yet, you only call the array constructor, but you should still usepnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls ofpanel0
, as both do pretty much the same
– Icepickle
Jan 2 at 12:06
1
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
It's windows form
– jsmlz
Jan 2 at 12:13
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
2
2
well, your panel hasn't initialized yet, you only call the array constructor, but you should still use
pnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls of panel0
, as both do pretty much the same– Icepickle
Jan 2 at 12:06
well, your panel hasn't initialized yet, you only call the array constructor, but you should still use
pnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls of panel0
, as both do pretty much the same– Icepickle
Jan 2 at 12:06
1
1
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
It's windows form
– jsmlz
Jan 2 at 12:13
It's windows form
– jsmlz
Jan 2 at 12:13
add a comment |
1 Answer
1
active
oldest
votes
change Panel pnl = new Panel[3] to:
var pnl = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
change Panel pnl = new Panel[3] to:
var pnl = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
add a comment |
change Panel pnl = new Panel[3] to:
var pnl = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
add a comment |
change Panel pnl = new Panel[3] to:
var pnl = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
change Panel pnl = new Panel[3] to:
var pnl = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
answered Jan 2 at 12:20


CodeManCodeMan
557311
557311
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
add a comment |
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
existingPanel1,2,3 are in your form.
– CodeMan
Jan 2 at 12:21
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
thank you , How can I do this if the number of panels is too high?
– jsmlz
Jan 2 at 12:30
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
foreach (Control control in Controls) if (control is Panel) // and other conditions to add to pnl list
– CodeMan
Jan 2 at 12:37
Thanks my Friend
– jsmlz
Jan 2 at 12:40
Thanks my Friend
– jsmlz
Jan 2 at 12:40
add a comment |
Could you let us know what's the error you got?
– Imantas
Jan 2 at 12:04
2
well, your panel hasn't initialized yet, you only call the array constructor, but you should still use
pnl[i] = new Panel()
before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls ofpanel0
, as both do pretty much the same– Icepickle
Jan 2 at 12:06
1
what technology is this? Winforms, WPF, webforms, Xamarin?
– Liam
Jan 2 at 12:07
It's windows form
– jsmlz
Jan 2 at 12:13