Resolved Sending tab to external application

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
The question really doesn't explain the issue well.

I have a component that I am building that "hopefully" will be able to send a tab to the parent form to set focus on the next tabstop, I am having limited success.

When you click a button on the component. However, since my control will be getting focus when the button is selected, I can't tell which control had focus just prior.

Using SelectNextControl works ok except it only returns a bool value, so ultimately I need to store the first selected control to start with and index it as needed.

I've toyed with a dozen different scenarios including sendkeys (ugh) and forcing the host application to set a property in my control telling it which control is currently or most recently selected, but that adds overhead to the host application.

I'd really like some ideas on how to accomplish this efficiently.
 
Last edited:
I'm not sure I understand you fully. Your title mentions an external application yet your explanation indicates that it's another form within your application. I think a clearer explanation of exactly what's going on is in order.

That said, if you want to know what the previously focused control was then you simply handle the Leave event of every control using a single method and, in the event handler, assign the 'sender', which is the control losing focus, to a member variable. You can then access that member variable in your Button's Click event handler or whatever.
 
Yeah. maybe the title is not exactly clear .. I'll have to think about how to properly word it ...

Ok, here is the issue, I am building a user control that is self contained within a DLL that can be added to other projects as desired ... just like any other control that you might add to a form.

The user control is designed to work as an add-on to textbox and combobox controls only. Once the user control is added to the form, you can assign a textbox or combobox for the control to act on, either in the designer or programmatically. If no textbox or combobox is added, (via a property assignment) the control doesn't do certain things .. as it is supposed to do ...

Now, I have the need within the user control to be able to cycle through every control on the form, regardless of what type it is, setting the focus on the control, even if temporarily, if and only if it has the TabStop attribute set to true.

Internal to the user control, I store the FocusedControl as m_Focused so I can quickly access it to do the work of the user control. I can utilize that control to select the next control in the form, starting with that control ...

VB.NET:
m_Focused.SelectNextControl(m_Focused, True, True, True, True)

Now the next control in the tab order is selected, however, I can't know which control it is from within the user control. The caveat is that if the selected control happens to be a textbox or combobox that I need (or want) the user control to act upon, I can (and will) use the GotFocus event of the control to set the m_Focused property of the user control.

I hope I have not lost you ...

Now, what happens is that m_Focused will only contain a textbox or combobox that the user control is supposed to work on. However, the next control that is selected by "SelectNextControl" may not be a textbox or combobox, so I now have to determine which control "SelectNextControl" selected .. it returns a bool value so that isn't of use.

I considered having an internal global to hold the last known selected control then when SelectNextControl is called, update the global to that control, then my dll will always know which control was selected last, the problem is, that whenever you use the user control it takes the focus so, I can't determine which control has focus, I must consider which one just lost focus ...

Now, if I use GetNextControl, it works until I reach the last control, then it throws an exception because there is no next control .. thats ok, I can handle the exception and start back at the beginning, except GetNextControl returns all controls(in taborder), regardless of the TabStop property .. so I have to filter for those that are not tab stops.

Everything is fine then until the first control is selected, then it skips a whole group of controls because they are nested within a groupbox ...

I am going bald here ...

Certainly there is an efficient way of setting the focus, even if it is temporary, to the next control in the taborder (from a form perspective) and retrieving that control from within a user control when a button is clicked in the user control.
 
Ahhh ... I have found the solution .. as simple as it was ....

VB.NET:
 Private Sub btnTabNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTabNext.Click
        If m_Tabbed Is Nothing Then
            m_Tabbed = m_Focused
        End If
        Dim TopForm As Form = CType(m_Tabbed.TopLevelControl, Form)
        TopForm.SelectNextControl(m_Tabbed, True, True, True, True)
        m_Tabbed = TopForm.ActiveControl
End Sub

I knew if I thought about it long enough there would be a simple solution :-\
 
Back
Top