tags:

views:

405

answers:

5

I'm working on a user control that will accept and validate an address. The addresses are all within a specific domain (in particular, Australia).

In the address user control I've got three fields: suburb, state and postcode (aka zip code).

When the user types part of a suburb name and hits TAB and moves into the state field, I query the database to see if I can make sense of the partial suburb. If I can, I fill in the complete suburb, state and postal code for them. As an example, there's only one suburb in Australia called "BORONIA", so the user can type "BORON", hit TAB and the form is populated with "BORONIA", "VIC" and "3155".

The issue I've got is that after I've filled these fields in I want to skip over the state and postal code fields and move on to the next control on the form. Because these address fields are in a user control, I effectively want to set focus to the next control on the parent form.

I know what that control is, but neither .Focus() nor .Select() appears to move the focus. The database query and field filling is done in the Enter event of the state field.

I had an earlier version of the same address handling logic that was NOT a user control: it was all inline on the data entry form, and in that situation everything worked fine. In the Enter method of the state field I could pass control to the next field.

When I've refactored this into a user control, however, it's failing. Can anyone nudge me in the right direction?

EDIT: I should note that I can pass the focus to other controls within my user control, so I know that the basic logic is correct. That's why I believe this is about user controls and parent controls, not my control selection and .Select() call.

A: 

Are you trying to set the focus of the control itself? I would try setting the focus on the textbox within the control.

SwDevMan81
No -- the problem is the other way around. The user control is trying to set focus to a text box on the parent control
Andrew
Like nextcontrol.SuburbTextbox.Focus();
SwDevMan81
Well, it's more like the AddressControl is trying to set focus to the parent form's EditBusinessHoursPhone text box. I've passed that control to the AddressControl via a property, and it's right according to the debugger. But when I call either .Focus() or .Select() on that control focus doesn't change.
Andrew
Might want to check out GetNextControl (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol(VS.71).aspx)
SwDevMan81
+3  A: 

Perhaps you can have the user control raise an event when it's done, and the page form can handle the event by setting focus where it needs to be...

Eric King
That's what I'm leaning toward right now, but it seems messy.
Andrew
A: 

I'm a bit fuzzy on the specifics, since I've moved on to WPF and don't do much ASP.NET anymore but you should be able to call the Page variable from the user control. ((textbox)Page.FindControl("txtMyTextbox)).Focus(); might do it.

Otherwise you could probably toss a javascript function in the page to do it and just call that.

Eric
It's WinForms, not ASP.Net.
Andrew
+1  A: 

I did something like it a while back, it should look like:

private void InputsUserControl_Load(object sender, EventArgs e)
{
    altNameTextBox.GotFocus += new EventHandler(altNameTextBox_GotFocus);
}

void altNameTextBox_GotFocus(object sender, EventArgs e)
{
    string s = nameTextBox.Text.Trim();

    if (!string.IsNullOrEmpty(s))
    {
        this.Parent.SelectNextControl(this, true, true, true, false);
    }

}

Note that GotFocus is not listed in the properties window. And that the Enter event doesn't work for this, it's called too early.

Henk Holterman
Thanks, Henk. This worked perfectly.
Andrew
A: 

You can control the beheviour of TAB in your usercontrol with the following code. In this case the TAB key focused the next control in the parent form or user control.

protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData != Keys.Tab)
            {
                return base.ProcessDialogKey(keyData);
            }
            Parent.SelectNextControl(this, true, true, false,false);
            return false;
        }

Good Luck

Freedeveloper