New to C#

  • Thread starter Thread starter Karl
  • Start date Start date
K

Karl

I have a group of controls and I would like to leave the first one visible
but be able to turn the other 7 visible or invisible dynamically. They are
all named the same except the number at the end (i.e.: Control1, Control2,
Control3, etc...) What I attempted to do was use a for loop but this
doesn't work (or at least I don't think I'm doing it correctly. What I'm
doing is something like this.

for (int i = 2, i <= 8, i++)
{
(Control + i).visible = false
}

I know this is wrong, but I would like to find out the correct way to do
this.

Any help would be appreciated.
 
Karl said:
I have a group of controls and I would like to leave the first one visible
but be able to turn the other 7 visible or invisible dynamically. They are
all named the same except the number at the end (i.e.: Control1, Control2,
Control3, etc...) What I attempted to do was use a for loop but this
doesn't work (or at least I don't think I'm doing it correctly. What I'm
doing is something like this.

for (int i = 2, i <= 8, i++)
{
(Control + i).visible = false
}

I know this is wrong, but I would like to find out the correct way to do
this.

To my mind, the best way of doing this is to create the controls in an
array. You'd then do something like:

m_controls.Visible = false;

If you're using the designer (and want to keep doing so) you'll need to
create the array somewhere after the designer code, and populate it:

m_controls[0] = control0;
m_controls[1] = control1;
// etc

I believe there is a way of finding a control by name, but for some
reason it escapes me right now...

Jon
 
Thanks Jon, I'll give that a try... I came from VB to C# so the concepts
are there, its the process (syntax) that gets me some times.

Jon Skeet said:
Karl said:
I have a group of controls and I would like to leave the first one
visible
but be able to turn the other 7 visible or invisible dynamically. They
are
all named the same except the number at the end (i.e.: Control1,
Control2,
Control3, etc...) What I attempted to do was use a for loop but this
doesn't work (or at least I don't think I'm doing it correctly. What I'm
doing is something like this.

for (int i = 2, i <= 8, i++)
{
(Control + i).visible = false
}

I know this is wrong, but I would like to find out the correct way to do
this.

To my mind, the best way of doing this is to create the controls in an
array. You'd then do something like:

m_controls.Visible = false;

If you're using the designer (and want to keep doing so) you'll need to
create the array somewhere after the designer code, and populate it:

m_controls[0] = control0;
m_controls[1] = control1;
// etc

I believe there is a way of finding a control by name, but for some
reason it escapes me right now...

Jon
 
Hi,

if you create a GroupBox on the form and place your controls within it, you
can control visibility via the GroupBox.

e.g. MyGroupBox.visible = false; etc.

for existing controls, you can create a GroupBox then drag them into it.

Regards,

Dave
 
Thanks Dave... The only problem is that I want to turn individual ones on
and off dynamically. The group box would give me an all or nothing
scenario.
 
Hi Karl,

your loop scenario indicated that 2 to 8 acted simultaneously.

I'm not sure what the problem is if you want to access them individually.
Can you give a bigger picture?

Regards,

Dave
 
I'm attempting to create a dynamic menu system that will show options based
on database entries.

What I would like to do it leave the first one visible (since I'll always
have at least one item) and turn the rest invisible until I populate it with
an entry from the database. If I only have 3 entries, then I will only show
3 of them and leave the rest invisible.

The problem is not so much on making them invisible (I can do that one by
one) but rather how to do it utilizing a for loop. I know it can be done,
just not sure how.

Does that help at all?
 
Use the tag property of the control and do something like this if they are
sitting on a form:
IEnumerator enumerator;
int iValue;

enumerator = this.Controls.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current is Button)
{
try
{
iValue = Convert.ToInt32((enumerator.Current as Button).Tag);
if (iValue > 0)
(enumerator.Current as Button).Visible = false;
}
catch
{
// Tag was not a integer
}
}
}

You could also check the controls name if you like.....

Dave
 
Keeping it simple then:

Create an Arraylist on the form:

private ArrayList al = new ArrayList();
Add the required controls to the ArrayList: (text boxes in this case, but
could be anything)
private void Form1_Load(object sender, System.EventArgs e)
{
al.Add(txtBox1);
al.Add(txtBox2);
al.Add(txtBox3);
al.Add(txtBox4);
}

e.g. Hide the controls:

IEnumerator en;
en = al.GetEnumerator();
while (en.MoveNext())
{
Control cont = (Control)en.Current;
cont.Visible = true;
}

The controls can also be addressed directly thus:

Control cont = (Control)al[0]; (array enumerated from 0)
cont.Visible == true;

So you could do a for loop also if you wanted too for just the required
number.

Any help?

Regards,

Dave Farr
 
"David Farr" <news".at."the-farrs.com> a écrit dans le message de [email protected]...

| IEnumerator en;
| en = al.GetEnumerator();
| while (en.MoveNext())
| {
| Control cont = (Control)en.Current;
| cont.Visible = true;
| }

Even simpler :

foreach(Control cont in al)
cont.Visible = true;

:-)

Joanna
 
Back
Top