Results 1 to 10 of 10

Thread: How to add OCX control at runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    How to add OCX control at runtime

    Can someone please show me how to add a Pinnacle-BPS Graph Control (graph32.ocx) to a form at runtime?

    I believe this control came with VB4 or VB5 - it's shown in my Component list.

    I can add it to form in IDE but want to do it in Runtime.

    Project Name: Project1
    Form Name: Form1
    Control Name: ctrlGraph (Name I'd like to assign to control)

    Any coding help is appreciated.

    Lee

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: How to add OCX control at runtime

    the easy thing to do is to have one(or several) on the form already with visible = false or
    position outside viewable area and then show on demand

    what is the use case where you would want it added at runtime only?

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Re: How to add OCX control at runtime

    Quote Originally Posted by dz32 View Post
    the easy thing to do is to have one(or several) on the form already with visible = false or
    position outside viewable area and then show on demand

    what is the use case where you would want it added at runtime only?
    I want to create control at runtime because don't want Graphic Server process resident if no graph control being used.

    I'm aware of the fact that one option is to have control array setup and can add additional controls of same type. Additionally can have control off screen and move it to visible area as needed.

    Lee

  4. #4
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: How to add OCX control at runtime

    ocx controls are loaded on demand the first time the form that hosts them is loaded.

    If you could get away with using a separate form for graph display that should keep the
    ocx and its dependencies out of memory until that form is shown.

    Somewhere I saw an example years and years ago of site-ing an ocx on a form manually
    but it was rather involved and I dont remember if it supported events. You probably have
    to add a subclass to get events manually.

    You also might have to manually implement extra interfaces:
    https://docs.microsoft.com/en-us/win...objectwithsite

    vb does a lot of magic behind the scenes for us.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: How to add OCX control at runtime

    You can add controls from scratch during runtime, but it's a bit tricky. First, when doing this, it's typically advisable to turn off the project's "Remove information about unused ActiveX Controls" as that will often remove the necessary reference.

    And secondly, it's often tricky to figure out how to get the reference name correct, but the error system will often help you with this when you've got it wrong. Also, you can look up how to correctly declare them in the Object Browser.

    Here's an example of adding an intrinsic control, and also adding an RTB control. To use this code, be sure the Microsoft Rich Textbox Control 6.0 is added to your project as a component.

    Starting with a blank form (but again, the RTB is set as a component reference):
    Code:
    
    Option Explicit
    '
    Dim WithEvents txt As VB.Textbox
    Dim WithEvents rtb As RichTextLib.RichTextBox
    '
    
    Private Sub Form_Activate()
    
    
            ' Intrinsic control.
            Set txt = Me.Controls.Add("VB.Textbox", "txt", Me)
            txt.Visible = True
    
    
            ' Control referenced in project from OCX.
            Set rtb = Me.Controls.Add("RichText.RichTextCtrl.1", "rtb", Me)
            rtb.Visible = True
            rtb.Top = 1200
    
    End Sub
    
    Private Sub rtb_Click()
        Debug.Print "rtb_Click"
    End Sub
    
    Private Sub txt_Click()
        Debug.Print "txt_Click"
    End Sub
    
    
    You can also explore the VBControlExtender object, but I'll let you explore that one on your own.

    Now, if you're adding an OCX control that's not registered and/or not referenced in your project (via Components), that's a whole different issue. I can do SxS stuff, but I don't know how to add those controls from scratch at runtime.

    Also, adding control arrays is even trickier, and I haven't addressed that.
    Last edited by Elroy; Oct 22nd, 2021 at 12:06 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to add OCX control at runtime

    Quote Originally Posted by Elroy View Post
    You can add controls from scratch during runtime, but it's a bit tricky. First, when doing this, it's typically advisable to turn off the project's "Remove information about unused ActiveX Controls" as that will often remove the necessary reference.

    And secondly, it's often tricky to figure out how to get the reference name correct, but the error system will often help you with this when you've got it wrong. Also, you can look up how to correctly declare them in the Object Browser.

    Here's an example of adding an intrinsic control, and also adding an RTB control. To use this code, be sure the Microsoft Rich Textbox Control 6.0 is added to your project as a component.

    Starting with a blank form (but again, the RTB is set as a component reference):
    Code:
    
    Option Explicit
    '
    Dim WithEvents txt As VB.Textbox
    Dim WithEvents rtb As RichTextLib.RichTextBox
    '
    
    Private Sub Form_Activate()
    
    
            ' Intrinsic control.
            Set txt = Me.Controls.Add("VB.Textbox", "txt", Me)
            txt.Visible = True
    
    
            ' Control referenced in project from OCX.
            Set rtb = Me.Controls.Add("RichText.RichTextCtrl.1", "rtb", Me)
            rtb.Visible = True
            rtb.Top = 1200
    
    End Sub
    
    Private Sub rtb_Click()
        Debug.Print "rtb_Click"
    End Sub
    
    Private Sub txt_Click()
        Debug.Print "txt_Click"
    End Sub
    
    
    You can also explore the VBControlExtender object, but I'll let you explore that one on your own.

    Now, if you're adding an OCX control that's not registered and/or not referenced in your project (via Components), that's a whole different issue. I can do SxS stuff, but I don't know how to add those controls from scratch at runtime.

    Also, adding control arrays is even trickier, and I haven't addressed that.
    I hope you don't mind Elroy but I used this post as part of the the ongoing debate in my VB6/VB.Net thread. If you come across it, please don't take it as an attack on you or anything like that. It's no secret that I have a lot of gripes with VB6 when compared to .Net and this one just bites my craw. My post is not meant as an insult to you nor is it intended to be disrespectful to you in any way shape or form. I feel like I have to say this because I've noticed a tendency of VB6 programmers interpreting my constant criticism of VB6 as personal attacks.

    This is the post in case you're even the slightest bit curious.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: How to add OCX control at runtime

    Cool I didnt know Controls.Add worked with external ocx controls. Confirmed the ocx still loads on demand after the .add call (wasnt sure with the withevents declaration)

    Ninya looks like your example works with a built in .NET control. How does it work for an external OCX?

    The complexities in vb6 are for external ocx controls specifically.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Re: How to add OCX control at runtime

    Elroy:
    Thank You.
    That works - see my code below!
    Only issue is.... Control added can't be a Control Array from what I see.

    Code:
    Option Explicit
    Dim WithEvents ctlGraph as GraphLib.Graph
    
    Private Sub Form_Load()
      Dim IX as Integer
      For IX=1 to 3
        Set ctlGraph = Me.Controls.Add("GraphLib.Graph","ctlGraph" & cstr(IX),Me)
        ctlGraph.Visible=true
        ctlGraph.left=IX*1200
        ctlGraph.Height=1200
        ctlGraph.Width=1200
      Next
    End Sub
    Last edited by Trims30; Oct 22nd, 2021 at 02:48 PM.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: How to add OCX control at runtime

    Quote Originally Posted by Trims30 View Post
    Only issue is.... Control added can't be a Control Array from what I see.
    You can, but, like I said, it's a bit involved. Here's a thread where Trick showed us how to do it. I've only done it for testing (and it works), but I'll leave you on your own to work through that thread. (Start reading at post #17, and then read the rest. It's all there.)

    Quote Originally Posted by Niya View Post
    I hope you don't mind Elroy but I used this post as part of the the ongoing debate in my VB6/VB.Net thread. If you come across it, please don't take it as an attack on you or anything like that. It's no secret that I have a lot of gripes with VB6 when compared to .Net and this one just bites my craw. My post is not meant as an insult to you nor is it intended to be disrespectful to you in any way shape or form.
    I appreciate you letting me know and I don't take it as a personal attack. And, since I've posted this on a public forum, I assume no rights to what happens to it. However, I must say that I really don't want to be pulled into this debate. Please keep me out of it. I know that .NET has features that VB6 doesn't have, but I see nothing productive out of a better-than-worse-than debate on the two. They both (VB6 vs VB.NET) have different uses and purposes, and I think we're all quite aware of them, so I just don't see the point.
    Last edited by Elroy; Oct 22nd, 2021 at 04:07 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to add OCX control at runtime

    Quote Originally Posted by Elroy View Post
    I appreciate you letting me know and I don't take it as a personal attack. And, since I've posted this on a public forum, I assume no rights to what happens to it. However, I must say that I really don't want to be pulled into this debate. Please keep me out of it. I know that .NET has features that VB6 doesn't have, but I see nothing productive out of a better-than-worse-than debate on the two. They both (VB6 vs VB.NET) have different uses and purposes, and I think we're all quite aware of them, so I just don't see the point.
    Oh no. I have no intent to pull you into it. I just don't want there to be any bad blood between us because of it. I have been maliciously attacked for stating my stance many times because people don't get that it's not about them.

    Quote Originally Posted by dz32 View Post
    How does it work for an external OCX?
    Errr.....Net really has no concept like an OCX. .Net simplifies all of this. Controls are just ordinary objects in .Net and you don't need a special type of binary to export them. Any normal .Net library can export controls and you can create and add them the exact same way you would any of the common controls like Buttons and Textboxes.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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