Solved

Using Picturebox as container

Posted on 1998-07-06
14
271 Views
Last Modified: 2010-05-03
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 ??
0
Comment
Question by:kinggs
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 2

Expert Comment

by:swilt
Comment Utility
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
 

Author Comment

by:kinggs
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 

Author Comment

by:kinggs
Comment Utility
This makes more sense for my specific needs...

Thanx!
Kennie
0
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 

Author Comment

by:kinggs
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
Oops, EE is too slow.... so I click twice :)
0
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 

Author Comment

by:kinggs
Comment Utility
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
 
LVL 7

Accepted Solution

by:
kamall earned 700 total points
Comment Utility
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now