[RESOLVED] Control.TypeOf Question
Hello,
it seems that there is no UserControl 'type' of control? - so we can't write
Code:
For Each C In frm.Controls
If TypeOf C Is TextBox Or TypeOf C Is ComboBox Or TypeOf C Is UserControl Then
'do stuff
End If
Next
at least following code does skip over UserControl (in do stuff part), or i am missing something?
Re: Control.TypeOf Question
Correct. Be nice if there was. If you know the control's type, i.e., UserControl1, then you can test for that specifically.
Re: Control.TypeOf Question
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
Re: Control.TypeOf Question
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.
Re: Control.TypeOf Question
"UserControl" is not a type, so of course this doesn't work. And why should it?
If you have a UserControl with the class name (i.e. type) SuperFudge and your Form has an instance of it then this works fine:
Code:
Option Explicit
Private Sub Form_Load()
Dim C As Control
For Each C In Controls
If TypeOf C Is SuperFudge Then Caption = "Found one!"
Next
End Sub
Just as one would expect.
Why should a SuperFudge be any different from a TextBox or any other control type?
Re: Control.TypeOf Question
Quote:
Originally Posted by
Tech99
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).
Quote:
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.
Re: Control.TypeOf Question
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.
Re: [RESOLVED] Control.TypeOf Question
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
Re: [RESOLVED] Control.TypeOf Question
Don't know - yet. Haven't been compiled it yet, because control is still under development and try to avoid 'binary compatibility' hassles.
2 Attachment(s)
Re: Control.TypeOf Question
Quote:
Originally Posted by
jmsrickland
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.
Quote:
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.
Re: Control.TypeOf Question
Thanks, have to look your sample/solution.
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.
Re: Control.TypeOf Question
Quote:
Originally Posted by
Bonnie West
Yes, but you'll have to uncheck the "Remove information about unused ActiveX Controls" checkbox in Project Properties.
The attached archive demonstrates a solution based on the example
here.
Excellent,Classic!
Mark the thread~