Iterate through Controls

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hello,

could someone post how to iterate through all the controls
on a form and return which object has focus. I feel like
someone would have asked this before - but I can't seem to
find it.

I would say it would be something like this:
Dim c as control
For Each c In Me.Controls
.....
Next

What I'm really trying to get at is - create some Cut,
Copy and Paste code for a PocketPC app. I'm guessing it
is harder than one would think - because I cannot see to
find any code that accomplishes this.

thank you.
 
I'm not much of a C# programmer. In fact, most of the
time - I cannot follow it. And this is one of these cases.

Can anyone covert to VB.NET. I'll try - but I'm not sure
I can.

I tried one of the auto converter programs - but they
don't like it either.

I do appreciate the information however - Tim. Thank you.

Yes - I was definitely going for textboxes.
 
VB.NET and C# are basically the same. There are the same structures in C#
as there are in VB.NET and, for the most part, the reverse.

Sequence (a series of operations performed serially):
-----------------------------------------------------

C# and VB.NET are identical in this. Both list operations from top to
bottom within a block, whether it's a function, a procedure, or whatever you
want to call it.

Repetition (a structure to perform one or more operations multiple times):
------------------------------------------------------------------------

C# has several forms of repetition. For loops look like this:

for ( start condition; repeat condition; increment condition )
do..while( continue condition )
while ( continue condition )

In each case, the continue condition defines some expression which can tell
the loop whether to continue or not. If the expression is true, the loop
continues; if not, the loop exits. The for loop is a special case which
allows for some initialization before the first loop (setting the loop
index, maybe, to 0), as well as a specific increment step (increment the
index before checking the repeat condition, maybe). So, a simple for loop
might be something of this nature:

for ( i = 0; i < 10; i++ )
{
// Stuff to be repeated.
}

do
{
} while ( expression );

while ( expression )
{
}

do..while and while loops differ only in when the expression is evaluated:
before the first loop or after.

Selection (a structure for executing certain code depending on an expression
value):
----------------------------------------------------------------------------
-------

select( expression )
{
case value:
// Stuff to do if expression evaluates to value.
break;
default:
// Stuff to do if none of the case values match the expression
value.
break;
}

The C# select..case structure is essentially identical to the VB.NET
Select...Case structure. Same idea, virtually same syntax.

if ( expression )
{
// Stuff to do if expression is true.
}
else
{
// Stuff to do if expression is false.
}

Again, basically identical to VB.NET.

Random syntactic bits
----------------------

Obviously, declarations of things are somewhat different in C# than in
VB.NET. Variable types are listed before the variable name, not after with
an "As" in the middle. There's really *nothing* to cause you not to be able
to read C# just as easily as VB.NET, though. Maybe your first couple of
attempts to *write* code in C# would generate a lot of errors, but reading
it should be very easy to pick up. I doubt that those of us who would
normally use C# first ever took a class or something in VB.NET. We just
knew enough about programming to know that VB.NET needed to have these
constructs and looked up the syntax in the help. You can do the same with
C#.

Paul T.
 
This should work.

Public Overridable Property ActiveControl() As Control
Get
Return GetFocusedControl(Me)
End Get
Set(ByVal Value As Control)
If (Not (Value.Focused)) Then
Value.Focus()
End If
End Set
End Property

Private Function GetFocusedControl(ByRef parent As Control) As Control
If (parent.Focused) Then
Return parent
End If
For Each ctrl As Control In parent.Controls
Dim temp As Control = GetFocusedControl(ctrl)
If (Not (temp Is Nothing)) Then
Return temp
End If
Next
Return Nothing
End Function
 
Tim Wilson said:
This should work.

Private Function GetFocusedControl(ByRef parent As Control) As Control

This one is actually ByVal. Not that it matters much
 
Back
Top