HotDog's Blog

Hotdog (Robert Verpalen) about C# and vb.net

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

JulAugust 2004Sep
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

Articles

Archives

Topics

CONTACT

Fun but useful linkies

General

VS 2005

Wolfenstein ET

Thursday, August 12, 2004 #

In .net each ControlContainer can have its own controlcollection. That means if you are using controlcontainers such as panels on a form, you will need to loop through all containers.

Below is the code I generally use for that task, with as small extra addition that you can specify which type of control you want to get back

added 1-6-5: VS2005 (whidbey) code

Code> (doubleclick to copy)
#region FormControls
public
static ArrayList GetControls(ContainerControl cc, Type ctype)
{
ArrayList al = new ArrayList();
AddControls(
ref al,cc.Controls,ctype);
return
al;
}
private
static void AddControls(ref ArrayList al,Control.ControlCollection cc,Type ctype)
{
foreach(Control c in cc)
{
if(ctype==null // ctype.IsAssignableFrom(c.GetType()))
al.Add(c);
if(c.HasChildren)
AddControls(ref al,c.Controls,ctype);
}
}
public
static ArrayList GetControls(ContainerControl cc)
{
return GetControls(cc,null);
}
#endregion

Since you can loop through an arraylist strongly typed, you can loop through the result with just the type you want.For example, to clear all textboxes:

Code> (doubleclick to copy)
foreach(TextBox tb in GetControls(this,typeof(TextBox)))
tb.Clear();

or in vb.net

Code> (doubleclick to copy)
        For Each tb As TextBox In GetControls(Me, GetType(TextBox))
            tb.Clear()
        Next
 
 
 

For those of you lucky enough to have vs2005:
Code Copy HideScrollFull
        public static Collection<ControlType> GetControls<ControlType>(ContainerControl Parent)
            where ControlType:Control
        {

            Collection<ControlType> coll = new Collection<ControlType>();
            findControls<ControlType>(coll, Parent);
            return coll;
        }
        public static void findControls<ControlType>(Collection<ControlType> coll, Control Parent)
            where ControlType : Control
        {
            foreach (Control c in Parent.Controls)
            {
                if (c is ControlType)
                    coll.Add(c as ControlType);
                if (c.HasChildren)
                    findControls(coll, c);
            }
        }

        //example call (procedure within a form):
        void bla()
        {
            foreach (TextBox tb in GetControls<TextBox>(this))
            {
                //.....
            }
        }
. . .
posted @ 4:20 AM

Just a small snippet, but an often asked question none the less ;-)

 

Public Class Person

Public BirthDate As Date

'.... other person data (lastname, first name , etc. )

Public ReadOnly Property Age() As Integer

Get

Dim a As Integer = DateTime.Now.Year - BirthDate.Year

If DateTime.Now.DayOfYear < BirthDate.DayOfYear Then a -= 1

Return a

End Get

End Property

End Class

posted @ 12:18 AM