Adding Checkboxes to a screen at runtime

C

Craig Lister

Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()

{

CheckBox[] chkBox = new CheckBox[255];

for (int i = 1; i < 256; i++)

{

chkBox.Visible = true;

chkBox.Left = 100;

chkBox.Parent = groupBox1;



}

}
 
R

Richard Blewett [DevelopMentor]

Craig Lister said:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()

{

CheckBox[] chkBox = new CheckBox[255];

for (int i = 1; i < 256; i++)

{

chkBox.Visible = true;

chkBox.Left = 100;

chkBox.Parent = groupBox1;



}

}


I''m not sure what you need the array for - assuming you want to use the
array outside of this method it needs to be a member variable not a local
variable.

However, as to the rest of the code it should be working fine - the problem
is you are drawing all of the checkboxes on top of eachother as you only set
the Left and not the Top

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
C

Craig Lister

I though I would need the array for referring later, but possibly not...
How would I assign the name to the checkbox then?
..name = chkBox + i.ToString(); ?

The code doesn't work at the moment, because the line:

chkBox.Visible = true;

fails, saying that I need the New word before doing this...



"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:[email protected]...
Craig Lister said:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()

{

CheckBox[] chkBox = new CheckBox[255];

for (int i = 1; i < 256; i++)

{

chkBox.Visible = true;

chkBox.Left = 100;

chkBox.Parent = groupBox1;



}

}


I''m not sure what you need the array for - assuming you want to use the
array outside of this method it needs to be a member variable not a local
variable.

However, as to the rest of the code it should be working fine - the
problem is you are drawing all of the checkboxes on top of eachother as
you only set the Left and not the Top

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
J

Jon Skeet [C# MVP]

Craig Lister said:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()
{
CheckBox[] chkBox = new CheckBox[255];
for (int i = 1; i < 256; i++)
{
chkBox.Visible = true;
chkBox.Left = 100;
chkBox.Parent = groupBox1;
}
}


Rather than setting the parent, you should add the checkbox to the
group-box, I believe.
 
R

Richard Blewett [DevelopMentor]

Jon Skeet said:
Craig Lister said:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()
{
CheckBox[] chkBox = new CheckBox[255];
for (int i = 1; i < 256; i++)
{
chkBox.Visible = true;
chkBox.Left = 100;
chkBox.Parent = groupBox1;
}
}


Rather than setting the parent, you should add the checkbox to the
group-box, I believe.


Setting the parent works fine

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
R

Richard Blewett [DevelopMentor]

Craig Lister said:
I though I would need the array for referring later, but possibly not...
How would I assign the name to the checkbox then?
.name = chkBox + i.ToString(); ?

The code doesn't work at the moment, because the line:

chkBox.Visible = true;

fails, saying that I need the New word before doing this...



"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:[email protected]...
Craig Lister said:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a
screen at runtime, and name then cb1, cb2... cb255

The code below does not work, but, how can I get this to work?

public void prepScreen()

{

CheckBox[] chkBox = new CheckBox[255];

for (int i = 1; i < 256; i++)

{

chkBox.Visible = true;

chkBox.Left = 100;

chkBox.Parent = groupBox1;



}

}


I''m not sure what you need the array for - assuming you want to use the
array outside of this method it needs to be a member variable not a local
variable.

However, as to the rest of the code it should be working fine - the
problem is you are drawing all of the checkboxes on top of eachother as
you only set the Left and not the Top

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Ahh - missed what the problem was. The CheckBox array is an array of
CheckBox references not an array of checkboxes. You need to create the
checkboxes to point the array references at.

Add the line

chkBox = new CheckBox();

at the start of the loop

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top