Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controls Contained in a Picture Box 2

Status
Not open for further replies.

xinyin

Programmer
Jan 16, 2003
81
HK
I have several picture boxes in a form. Each picture box contains some controls (mostly labels and textboxes). How can I do some operation to those controls in a CERTAIN PICTURE BOX? For example, if I want to change the backcolor of all textboxes in picutebox#2 ONLY. I know the method:

Dim varCtrl as Controls
For Each varCtrl in Controls
...(check if it is a textbox, if yes then perform operations)
Next

But this trick is not useful here because it will change the backcolor of ALL textboxes in ALL pictureboxes in this form. So I tried

For Each varCtrl in Picbox2
...(operations)
Next

results in error.I also tried

For Each varCtrl in Controls
If varCtrl.Parent = Picbox2 then
...(operations)
End If
Next

error again.
Please help!
 
You could use the .Container property:
Code:
 Dim varCtrl as Control
For Each varCtrl in Me.Controls
  If varCtrl.Container Is Picbox2 then
  ...(operations)
  End If
Next varCtrl
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
There you go!

Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox And ctl.Container Is Picture1 Then
ctl.BackColor = vbRed
End If
Next
End Sub
 
Yes it works, thanks both of you!
I also realize that the tricky part is the "is" in "varCtrl.Container Is Picbox2". If I put it in "varCtrl.Container = Picbox2" it won't work.
 
I also realize that the tricky part is the "is" in "varCtrl.Container Is Picbox2". If I put it in "varCtrl.Container = Picbox2" it won't work."

Do you understand why it doesn't work with the = sign?
 
Do you understand why it doesn't work with the = sign?" Not quite understand... I am still in the primary learning stage of Visual Basic. Would you please tell me the answer?
 
The "=" works like:
1. checking conditions: If (a = 3) then ...
2. assign values: myString = "hello" ot myInt = 45 etc

In C it is a little clearer because:
at 1. it is "==" and
at 2. it is "=
 
Do you understand why it doesn't work with the = sign?" Not quite understand... I am still in the primary learning stage of Visual Basic. Would you please tell me the answer?"

Because you are comparing objects "is" and "=" are not the same thing.

When you ask VB6 "if varCtrl.Container = Picbox2", VB checks to see if the objects are of the same type. varCtrl.Container is a Picturebox, but it may be a different picturebox than Picbox2. VB does not care if it's a different Picbox, as long as it's the same type of control, the statement will return a true value.

This is the same for any object, control or class.

When you change the statement and use Is, then VB checks to see if the two items being compared are the same exact instance of the object, not just the same type of object.


Hope that was clear.

Robert
 
>VB checks to see if the objects are of the same type.

This is not correct. When we compare two objects using = operator, VB tends to perform a value-based comparison as it normally does with the = operator. For that purpose, VB tries to evaluate the defualt property or method of the object if it is available.

The default property of a picturebox is its "Picture" property. But it is again an object so VB again evaluates the default member of that object (the Picture object) which is "Handle" property.

So if VB sees some code like this:

[tt]If Picbox1 = Picbox2 Then[/tt]

It is actually evaluated like this:

[tt]If Picbox1.Picture.Handle = Picbox2.Picture.Handle Then[/tt]

[tt]Is[/tt] operator on the other hand is used just to compare two object instances, if they are same. Thats why we needed:

[tt]ctl.Container Is Picture1[/tt]

instead of:

[tt]ctl.Container = Picture1[/tt]

To check the type of an object, VB has TypeName function and TypeOf operator.

See another example.

Start VB. Place two text boxes and two check boxes on the form, and insert the following code.
___
[tt]
Private Sub Form_Load()
If Text1 = Text2 Then MsgBox True Else MsgBox False
If Check1 = Check2 Then MsgBox True Else MsgBox False
End
End Sub[/tt]
___

Run the program, once you get False (for textboxes) and then True (for checkboxes)

The reason being that for textbox control, the default property is .Text, which is different for two textboxes, and the comparison returns False.

For checkboxes the default property is .Value, which is same (0) for the two controls, resulting True in comparison.

You can change these properties at design time for experimentation, and see how it affects the result of these comparisons.

Note that not all objects necessarily have a default user-interface member property or method associated with them. In such cases, comparing objects with = operator is not possible.
See the following code which complains a compiler error.
___
[tt]
Private Sub Form_Load()
If Me = Form1 Then MsgBox True
End Sub[/tt]
___

The above comparison with [tt]Is[/tt] operator works just fine.
 
Here's some additional info that may help some people:

fred Is jim

is basically equivalent to

objptr(fred)=objptr(jim)
 
Hypetia is correct in pointing out my error. I'd always thought that the = sign was checking objects of the same type when dealing with controls. Sorry.
 
Thank you very much to all of you. I learn a lot from your posts.
 
Clearly and eloquently explained, with good examples. Easily worthy of an additional star.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top