How to find control with focus?

P

Peter Steele

I have a form with four text fields, each with sequentially ordered
tabstops. I want to have the fields recognize the return key and
automatically shift the focus to the next control. I created the following
event handler that I've assigned to the KeyUp event of each of the TextBox
controls:

private void Any_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Close();
}
else if (e.KeyCode == Keys.Return)
{
foreach (Control c in this.Controls)
if (c.Focused) this.GetNextControl(c, true).Focus();
}
}

This works when the current control is the first text field, moving the
focus correctly to the second text field. However, when I hit enter in the
second text field, the focus moves to the fourth text field, skipping over
the third? What am I doing wrong here?
 
H

Herfried K. Wagner [MVP]

* "Peter Steele said:
I have a form with four text fields, each with sequentially ordered
tabstops. I want to have the fields recognize the return key and
automatically shift the focus to the next control. I created the following
event handler that I've assigned to the KeyUp event of each of the TextBox
controls:

Have a look at the form's 'ActiveControl' property.
 
P

Peter Steele

That did the trick--thanks very much. I'm curious though--why did my loop
not work?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Peter,
Probably because your textboxes are placed on a panel or other kind of
control. Controls collection keeps references to the immediate children
only. I suppose your textboxes are grand- or grand-grand-, etc children of
the form.

--
B\rgds
100

Peter Steele said:
That did the trick--thanks very much. I'm curious though--why did my loop
not work?
 
P

Peter Steele

Actually, there are no panels or groups. The text boxes are direct children
of the form and their tab stops are numbered from 1 through 4...

Peter

Stoitcho Goutsev (100) said:
Hi Peter,
Probably because your textboxes are placed on a panel or other kind of
control. Controls collection keeps references to the immediate children
only. I suppose your textboxes are grand- or grand-grand-, etc children of
the form.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Peter,
In that case it has to work. I tested and everythink works fine. The only
problem with your loop is that when the last control in the tab order is
selected GetNextControl wouldn't loop back to the first one instead it would
simply returns null value. In that case your loop will throw
NullReferenceException because you don't check the returned value.
Anyways your loop works.
 
P

Peter Steele

That's really bizarre. My loop definitely does not work. I'll have to leave
it as a mystery for now I guess...
 
G

Guest

Your loop is not full. Each component have Controls property. In this collection store controls pointer's, which have Controls property too. You need use recursion algorithm for find focused component
 
P

Peter Steele

That's only an issue if I have nested controls in panels and group boxes. In
my case, all controls are direct children of the form so I knew a simple
loop would work. Except that for some mysterious reason it didn't. The
ActiveControl property is the way to do it though so I don't need the loop
in the end...

Dimacus said:
Your loop is not full. Each component have Controls property. In this
collection store controls pointer's, which have Controls property too. You
need use recursion algorithm for find focused component.
 

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