Using Picturebox as container

I have a big form with a picturebox that I use to cntain fields, and for scrolling. (It's contained within another picturebox). What I would like to do is to send a reference to the picturebox over to a module, which then loops thru the controls. I only want to reference the controls in the container. ie.

For each Control in PictureContainer.Controls
   MsgBox Control.Name
Next

ANY Help or comments please ??
kinggsAsked:
Who is Participating?
 
kamallConnect With a Mentor Commented:
kinggs,
It is also a great pleasure for us to see that there are people like you here who really appreciate the job we are doing. In addition, we get happy for every individual help we could offer to make life a little bit easier for our VB colleagues.

As an answer, I refer to my code in the comment.

Have a nice day all.
0
 
swiltCommented:
The Picture has no 'Controls' so cycle thru the form finding the controls attached to the picture


'Form code
Private Sub Command1_Click()
    CycleThruControls Picture1, Me
End Sub

'Module code
Public Sub CycleThruControls(cntrl As Control, frm As Form)
    Dim x As Control
   
    For Each x In frm.Controls
        If x.Container.Name = cntrl.Name Then
            If TypeOf x Is TextBox Then
                MsgBox x.Text
            ElseIf TypeOf x Is Label Then
                MsgBox x.Caption
            ElseIf TypeOf x Is CommandButton Then
                MsgBox x.Caption
            End If
        End If
    Next x
End Sub

0
 
kinggsAuthor Commented:
Yea - That's the way that I did it at this stage as well. I sortof thought that there won't be a better way. My concern was that I really have a lot of controls on the page, and since I have to access the controls frequently, this might not be the optimal answer...
I thought whacking the controls into a seperate collection at load time and there after referencing that, but haven't tried it.

If nobody has any other ideas, I guess I will accept this answer in a day or two's time

Thanx for the inputs !!!
;-)
Kennie
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kamallCommented:
Kinggs,
Here is the answer:

Code in Form1
---------------------
Dim MyControls As New Collection   'In the general declarations

Private Sub Command1_Click()
    Call TestMe(MyControls)
End Sub

Private Sub Form_Load()
   For Each Control In Me
       If Control.Container.Name = "Picture1" Then MyControls.Add Control
   Next
End Sub

Code in Module1
-------------------------
Sub TestMe(x As Collection)
    For Each Control In x
        MsgBox Control.Name
    Next
End Sub

Put one command button on the form (command1), one picture box and draw any number and type of controls inside it. Run the project, and click the command1 button.
You will get the names of all the controls that exist inside the picture box only.

Hope that helps.
(P.S. If this what you are looking for, please reject the current answer to enable me to lock the question and get the points.)

Best regards
0
 
kinggsAuthor Commented:
This makes more sense for my specific needs...

Thanx!
Kennie
0
 
yowkeeCommented:
kinggs,

  Instead of using a collections to keep track the controls under the Picture control. You could use EnumChildWindows to enumerate the controls under Picture1. I believe it's a better solution, so I post as answer. If you feel Kamall's answer better, reject this and let Kamall lock it.

  The following code use EnumChildWindows to enumerate controls under Picture1 and hide all of them. Put 1 command button and 1 Picture control at form, then put 4 picture control inside Picture1.

----- ' In BAS module
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE = 0


Public Function EnumChildProc(ByVal hwnd As Long, _
                              ByVal lParam As Long) As Long
                             
    ShowWindow hwnd, SW_HIDE
    EnumChildProc = 1
End Function

Public Sub GetChild(ctlTmp As Control)
    EnumChildWindows ctlTmp.hwnd, AddressOf EnumChildProc, 0
End Sub

----' Code in Form module
Private Sub Command1_Click()
    GetChild Picture1
End Sub

--------

Regards.
0
 
kinggsAuthor Commented:
Great stuff - this is very close to exactly what I need. Only problem I have now (Sorry if I sound extremely inexperienced), is how to do anything with the controls in normal VB Code. Lets say for instance I want to simply Add their names to a list box, or MsgBox their names or whatever...

Thanx a lot for the inputs

0
 
yowkeeCommented:
kinggs,

  Sorry for not much comments on the code. Basically API EnumChildWindows is used to enumerate child windows which belong to specify parent window which passed its handle in 1st parameter. And EnumChildWindows will pass every child window handle to a childwindow procedure. That is EnumChildProc in this case, it is a callback function and thus it must stay at a BAS module.

  So, the hWnd which getting in EnumChildProc is the window handle of child windows under Picture1(which is the parent window). There is no straight forward way to show the child windows name, or said the variable name of the control. For showing their name, we could use GetDlgCtrlID to get its control ID in order to get its name in Controls.

  Change the above code as follow:

----' Code in BAS module
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetDlgCtrlID Lib "user32" (ByVal hwnd As Long) As Long

Public Const SW_HIDE = 0

Public Function EnumChildProc(ByVal hwnd As Long, _
                    ByVal lParam As Long) As Long
    Dim lCtlID As Long
    ' Get the control ID
    lCtlID = GetDlgCtrlID(hwnd)
    MsgBox Form1.Controls(lCtlID - 1).Name
    EnumChildProc = 1
End Function

Public Sub GetChild(ctlTmp As Control)
    EnumChildWindows ctlTmp.hwnd, AddressOf EnumChildProc, 0
End Sub

-----' Code in Form1
Private Sub Command1_Click()
    GetChild Picture1
End Sub

-----

Hope this help. :)
0
 
kamallCommented:
yowkee,
"....I believe it's a better solution"!!!
What was the BETTER in your solution??
First of all, most programmers if not all WILL USE pure VB code to do the job rather than using APIs.
Second, what is wrong with using the POWERFUL collection object?
Third, my code is much simpler and shorter than yours and much easier to use and to understand even by the less experienced VB programmer, and it is WITHOUT APIs.
And fourth, since my code does EXACTLY what kinggs need, you should have posted your answer as comment until kinggs decides which solution is best for him and informs us.
Please don't think that I am trying to criticise you, but this is the truth.

Regards to all.
0
 
yowkeeCommented:
kamall,

  I apologize to you as I post the code as answer. I didn't think too much at the time I posted it. I agree that I should post it as comment.

  I said I think it was better because I thought kinggs is going to do some operation on those picture control. In that way, it could be handled well by using some graphical api, then I thought it take overhead to use the collections. Well, it has its pros and cons.....

  I agree that the code with collections is more simpler and look constructable, especially when kinggs's requirement is to show the control's name ( I start to regret to post as answer when I knew this requirement :P...). Well, experienced VB programmer may happy with API but not VB beginner and this is true.

  So, kinggs, please reject my answer and let kamall lock it.
  And sorry if I make any unconvience here. :)

  Regards.
0
 
yowkeeCommented:
kamall,

  I apologize to you as I post the code as answer. I didn't think too much at the time I posted it. I agree that I should post it as comment.

  I said I think it was better because I thought kinggs is going to do some operation on those picture control. In that way, it could be handled well by using some graphical api, then I thought it take overhead to use the collections. Well, it has its pros and cons.....

  I agree that the code with collections is more simpler and look constructable, especially when kinggs's requirement is to show the control's name ( I start to regret to post as answer when I knew this requirement :P...). Well, experienced VB programmer may happy with API but not VB beginner and this is true.

  So, kinggs, please reject my answer and let kamall lock it.
  And sorry if I make any unconvience here. :)

  Regards.
0
 
yowkeeCommented:
Oops, EE is too slow.... so I click twice :)
0
 
kamallCommented:
yowkee,
Thank you for your kind comment.
I repeat again that I didn't intend to criticise you at all. So I also apologize if my previous comment expressed in a way or another such a spirit.

Have a nice day all.
0
 
kinggsAuthor Commented:
It is an extreme pleasure for me to work with people as experienced and professional as you. While I am using Kamall's answer at this stage, yowkee dit teach me a lot on the API region.

Thanx for all your efforts.
It is genuinely appreciated
;-)
Kinggs
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.