• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1080
  • Last Modified:

access activecontrol from within usercontrol

usercontrol inside usercontrol

I create an usercontrol that will put into another parent usercontrol.
How can i access activecontrol from my usercontrol to parent usercontrol?
0
EDDYKT
Asked:
EDDYKT
  • 10
  • 8
  • 2
2 Solutions
 
EDDYKTAuthor Commented:
The basic idea is how do i find out what is activecontrol name on my parent usercontrol
0
 
jkaiosCommented:
How about this?

   Debug.Print UserControl.Parent.ActiveControl.Name
0
 
EDDYKTAuthor Commented:
cannot access. Said property not found

I can see the UserControl.Parent.ActiveControl.Name from the watch window

but if i do

?UserControl.Parent.ActiveControl.Name

on immediate window, i got the above error.

Looks like to me it is private property.
But how can watch window (VB) can see it?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
VR4Commented:
Ok, if you are desperate do the following:

On you Usercontrol that will host another user control create a timer and a Label1 (or any name)
In the timer do something like:
Label1.Caption = UserControl.ActiveControl.Name
make Label1 not visible.
From now on This label will be you activeControl on this usercontrol.

now on the inserted/second  Usercontrol you can get to it by doing the following:

Dim Control As Object
For Each Control In UserControl.ParentControls
    If Control.Name = "Label1" Then Msgbox Control.Caption
Next
Set Control = Nothing
0
 
EDDYKTAuthor Commented:
I create a usercontrol that will be put on the other usercontrol .

I'm not the container. I would like to find out what is the container activecontrol.
I've no control on the container.

I just want to know UserControl.Parent.ActiveControl.Name from my usercontrol but i cannot access it
0
 
jkaiosCommented:
Was the "other" UserControl (which is the container of your usercontrol) written in VB?  If not, then that might be the problem.
0
 
EDDYKTAuthor Commented:
yes both written in vb
0
 
VR4Commented:
This is not what you are looking for, I know. But if you need to bypass the other usercontrol you can do
Screen.ActiveControl.Name

This will at least tell you that the active control is the other usercontrol, or any item on the main form..
I'll have to look at available API to do this, since event GetFocus returns just the Usercontrol
0
 
EDDYKTAuthor Commented:
Screen.ActiveControl.Name does not work on the container is usercontrol. Only works on form

GetFocus will return wrong info if the usercontrol is not active
0
 
VR4Commented:
Ok, I told you that the above is not the solution, but maybe was enough for whatever you where doing.
I do have a solution for your issue though :)
0
 
VR4Commented:
In your UserControl Definition:

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Type GUITHREADINFO
   cbSize As Long
   flags As Long
   hwndactive As Long
   hwndFocus As Long
   hwndCapture As Long
   hwndMenuOwner As Long
   hwndMoveSize As Long
   hwndcaret As Long
   rcCaret As RECT
End Type

Private Declare Function GetGuiThreadInfo Lib "user32" Alias "GetGUIThreadInfo" (ByVal idThread As Long, lpgui As GUITHREADINFO) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
0
 
VR4Commented:
code:
Function GetActiveWindowGuiInfo() As Long

   Dim gui As GUITHREADINFO

   gui.cbSize = Len(gui)

   If GetGuiThreadInfo(0&, gui) <> 0 Then
        Debug.Print "g1" & GetHWNDText(gui.hwndCapture)
        Debug.Print "g2" & GetHWNDText(gui.hwndactive)
        Debug.Print "g3" & GetHWNDText(gui.hwndMenuOwner)
        Debug.Print "g4" & GetHWNDText(gui.hwndcaret)
        Debug.Print "g5" & GetHWNDText(gui.hwndFocus)
    End If
   
End Function


 Function GetHWNDText(ByVal hwnd_handle As Long) As String

   Dim nLength As Long
   Dim res As Long
   Dim buff As String
   
   If hwnd_handle <> 0 Then
   
      nLength = SendMessage(hwnd_handle, &HE, 0&, ByVal 0&)
     
      If nLength <> 0 Then
         
         buff = Space$(nLength + 1)
         
         res = SendMessage(hwnd_handle, &HD, nLength + 1, ByVal buff)
         
         If res <> 0 Then
            GetHWNDText = Left$(buff, res)
            Exit Function
         End If  'if res
         
      End If  'if nlength
   
   End If  'if hwnd_handle
   
   GetHWNDText = "(not available)"
   
End Function


In your Timer of the usercontrol
GetActiveWindowGuiInfo


in the Debug window
you will notice that G5: is what you are alooking for
Plus some more stuff for free... in another variables
0
 
EDDYKTAuthor Commented:
The routine is good but it's not what i want.

I want to know what is UserControl.Parent.ActiveControl.Name

The method likes gotfoucs which will get wrong info if the container is not in focus.
0
 
EDDYKTAuthor Commented:
I may need to check with Ioleobject to get the parent usercontrol object?

Does anyone know how to do in VB?
0
 
VR4Commented:
The method works for what you want, pleas try the code and look carefully at what it does.
It gets everything under the sun for the selected / active control.

Keep in mind the event that gets data has to be in the timer, since the minute you click on anything to get your status, you moved the active control.

If you think it does not work I need a specific example when you see that.
0
 
VR4Commented:
By the way the key in this solution is not the GetText api, but the ability to get a reference to the object in question.
You can take the Hwnd and check it against the ParentCollection of your Usercontrol to get the Control in question, then you can get any property out of that.

0
 
VR4Commented:
Ok, I looked a little more into it, some controls like the calendar will not return as being active. (only because GetText returns nothing from these, since these controls have no Text, by the way you are not after the Text anyways, I just thought it be easy to see that way).
By the way in most these cases Screen.ActiveControl.Name will correctly id the item, so you could potentially use both as well.

But in any case here is the Timer event ( I know show the handles in the textbox) Put Text1 textbox on your usercontrol.
On Error Resume Next
Dim Control As Object
Dim ltmp As Long
ltmp = hGetFocus

GetActiveWindowGuiInfo
Text1 = ""
For Each Control In UserControl.ParentControls
   ' If Control.hWnd = ltmp Then
        Label1.Caption = Screen.ActiveControl.Name
        Text1 = Text1 & Control.Name & "(" & Control.hwnd & ")" & vbCrLf
   ' End If
Next

'Label1.Caption = Screen.ActiveControl.Object.ActiveControl.Caption


'Label1.Caption = UserControl.ParentControls.ParentControlsType
Set Control = Nothing

End Sub


And here is a modified GetActiveWindowGUIInfo functin: Note that I return handle there as well.
You can see that one can easily match these

Function GetActiveWindowGuiInfo() As Long

   Dim gui As GUITHREADINFO

   gui.cbSize = Len(gui)

   If GetGuiThreadInfo(0&, gui) <> 0 Then
        Debug.Print "g1" & GetHWNDText(gui.hwndCapture) & ":" & gui.hwndCapture
        Debug.Print "g2" & GetHWNDText(gui.hwndactive) & ":" & gui.hwndactive
        Debug.Print "g3" & GetHWNDText(gui.hwndMenuOwner) & ":" & gui.hwndMenuOwner
        Debug.Print "g4" & GetHWNDText(gui.hwndcaret) & ":" & gui.hwndcaret
        Debug.Print "g5" & GetHWNDText(gui.hwndFocus) & ":" & gui.hwndFocus
    End If
   
End Function

Once again the hwndFocus is your ticket. It is NOT the same thing as the GetFocus API that I also have tried earlier, but realized it would not return correctly.

0
 
VR4Commented:
scratch the Screen.ActiveControl... I was doing something else with it.
0
 
EDDYKTAuthor Commented:
Keep in mind there may be a lot of textbox inside another usercontrol.

If usercontrol is the container(hold my user control), then screen.activecontrol will not work. ONLY work if container is form. Just like UserControl.Parent.ActiveControl.Name
0
 
VR4Commented:
I give up,  I said scratch - i.e. DO NOT USE Screen.Activecontrol right above your reply.

Also the routine I gave you returns perfectly when its inside the other user control.
I've tested the code for buttons text boxes, list boxes, Tabs, Calendar control, in and out of the second user control, everything works.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now