Results 1 to 12 of 12

Thread: [RESOLVED] Control.TypeOf Question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    692

    Resolved [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?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    692

    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.
    Last edited by Tech99; Aug 4th, 2015 at 07:02 PM.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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?

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Control.TypeOf Question

    Quote Originally Posted by Tech99 View Post
    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 View Post
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    692

    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.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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


    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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    692

    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.

  10. #10
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Control.TypeOf Question

    Quote Originally Posted by jmsrickland View Post
    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 View Post
    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.


    Name:  Control Arrays via Controls.Add Demo.png
Views: 10745
Size:  4.6 KB
    Attached Files Attached Files
    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    692

    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.

  12. #12
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: Control.TypeOf Question

    Quote Originally Posted by Bonnie West View Post
    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.


    Name:  Control Arrays via Controls.Add Demo.png
Views: 10745
Size:  4.6 KB
    Excellent,Classic!
    Mark the thread~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width