Here's a short routine which can distinguish UserControls/ActiveX controls from intrinsic VB6 controls:
Code:
Private Function IsActiveXControl(ByRef Ctrl As Control) As Boolean
On Error Resume Next
IsActiveXControl = TypeName(Ctrl) = TypeName(Ctrl.Object)
End Function
Code:
For Each C In frm.Controls
Select Case True
Case TypeOf C Is TextBox, TypeOf C Is ComboBox, IsActiveXControl(C)
'Do stuff
End Select 'Unlike If, Select Case is able to short-circuit Boolean expressions
Next
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Thanks Bonnie, that is nice solution and more useable when there are several user controls in form.
Other solution is to directly point with control name like;
Code:
If TypeOf C Is TextBox Or TypeOf C Is ComboBox Or UCase(C.Name) = "GRDORDERROWS" Then...
Another question. I am doing dynamic sql clause by code, control name and it's data is dealt with part of the sql queries where clause. Control name is same as db field name and data is search key. Controls - ownerdrawn and/or created dynamically by controls.add method fex. UserControl.Controls.Add("VB.TextBox", "tbEdit")) in usercontrol, are not detected either with frm.Controls.
So.. Opinions what is the elegant way to get the control name inside usercontrol?
Controls are named after db fieldnames. I am thinking on putting field names to array, but if names could be read by code (public method inside usercontrol), then that is obviously unneccessary?
Code:
Public Property Get ucControl() As Object
Set ucControl = UserControl.Controls
End Property
but calling it's data by
grdOrderRows.ucControl.Text
Generates 438 object does not support this method - error.
So how to correct that? Tried with Set ucControl = UserControl.Controls.tbEdit, UserControl.Controls.TbEdit.Text and UserControl.Controls.TextBox.TbEdit.Text as per above example - no worky. Idea is to get the control name and it's data as per above.
So.. Opinions what is the elegant way to get the control name inside usercontrol?
Exposing the nested controls in your UserControl via a property such as your ucControl property looks fine to me. I would, however, give it the same name as the property it is exposing (i.e., Controls) and I would also consider making it the default property (unless another property makes more sense as the default).
Originally Posted by Tech99
but calling it's data by
grdOrderRows.ucControl.Text
Generates 438 object does not support this method - error.
So how to correct that?
Try:
Code:
grdOrderRows.ucControl("tbEdit").Text
Or:
Code:
grdOrderRows.ucControl!tbEdit.Text
Remember, your ucControl property returns a Collection object, so you'll have to specify either an index or key* if you want to access any of its items.
* The keys in a UserControl's Controls Collection are the names of the controls it contains.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Bonnie, thank you very much, that cleared usercontrols collection things out.
One thing which i am still not able to do, is creating control array using Controls.Add method. However adding to control array works fine, when first item (zero index) is created at the desgin time.
Could you add a usercontrol using Controls.Add if you compiled the usercontrol to an ocx and then add the ocx to VB's toolbox
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Could you add a usercontrol using Controls.Add if you compiled the usercontrol to an ocx and then add the ocx to VB's toolbox
Yes, but you'll have to uncheck the "Remove information about unused ActiveX Controls" checkbox in Project Properties.
Originally Posted by Tech99
One thing which i am still not able to do, is creating control array using Controls.Add method.
The attached archive demonstrates a solution based on the example here.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
PS. something has changed in vbforums.com code within few days - as now opening a topic 'hangs' in facebook http request. Happens on our development machines in 'somewhat restricted' (read hardened) network where fex. facebook http traffic is not allowed.