Results 1 to 17 of 17

Thread: VB6 OCX - How to Event handle in another application

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    VB6 OCX - How to Event handle in another application

    HI,

    I have created OCX in Vb6 which contains only Listview control and coded "drag and drop" functionality and currently I want to implement the OCX in another application but I'm not sure how to handle the event.

    E.g. I have registered the created OCX in Excel VBA, its working fine but I'm unable to pull the listed data's in the Listview, but I'm not sure how to do it - Please help, below are the code which I have tried.


    Code:
    public sub Listviewocx()
    eventvar1 = Data.Files.Count
        
          
    
       For intCOunter = 1 To eventvar1
        strpath = Data.Files(intCOunter)
     msgbox strpath
    next
    end with
    
    End sub
    Thanks
    Thiru

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 OCX - How to Event handle in another application

    I don't see any VB6 question here.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by dilettante View Post
    I don't see any VB6 question here.
    OCX was created in VB6...and even if I use the ocx in VB6 as a component, I'm unable interact with it..eg created listview as ocx and registered as different OCX in vb6 and I created on form with one command button and inserted the listview ocx component from toolbox..and when I press the button i'm unable to interact/Pull data from ocx component.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 OCX - How to Event handle in another application

    We just don't have enough information to help.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: VB6 OCX - How to Event handle in another application

    When you create your user control you have to add the properties, methods and events that you will need to access from the parent.

    There is a handy dandy little control wizard included with VB that can help a lot if you've not did this before.

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,625

    Re: VB6 OCX - How to Event handle in another application

    The OCX should have a declaration for a public event, like
    Public Event FilesDrop(sFiles() As String)
    then whenever the event is triggered in the OCX,
    RaiseEvent FilesDrop(filelist)

    The host form will list that event like any other event for other objects...
    Private Sub YourOCXName_FilesDrop(sFiles() As String)

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by DataMiser View Post
    When you create your user control you have to add the properties, methods and events that you will need to access from the parent.

    There is a handy dandy little control wizard included with VB that can help a lot if you've not did this before.
    - Thank you for the reply but Listitem property is not found in the wizard.

    Example generally I use below code to get the listed items value from Listview control but its not available in the wizard how to add it to the control?

    Code:
    For i = 1 To (ListView1.ColumnHeaders.Count)
        MsgBox ListView1.ListItems(ListView1.SelectedItem.Index).ListSubItems(i).Text
    Next i

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

    Re: VB6 OCX - How to Event handle in another application

    If this is a custom ocx you created, you will need to provide Public properties, functions, etc to access the controls within the ocx, i.e., the listview.

    The ocx you added to Excel. Since you have VB6 (you said you created the ocx in VB6), add your compiled ocx to a test form in a new VB6 project. You should have the same issues.

    One thing that you could possibly try in VBA is to use the Object property of the ocx, but there will be no intellisense.
    Code:
     MsgBox YourControlName.Object.PropertyOrFunction(params if any)
    Last edited by LaVolpe; Apr 19th, 2018 at 03:30 PM.
    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}

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by LaVolpe View Post
    If this is a custom ocx you created, you will need to provide Public properties, functions, etc to access the controls within the ocx, i.e., the listview.

    The ocx you added to Excel. Since you have VB6 (you said you created the ocx in VB6), add your compiled ocx to a test form in a new VB6 project. You should have the same issues.

    One thing that you could possibly try in VBA is to use the Object property of the ocx, but there will be no intellisense.
    Code:
     MsgBox YourControlName.Object.PropertyOrFunction(params if any)
    Can you please help me with listitem code...I'm trying to find the solution for more than 1 week.

    I tried all the property from Wizard option - Properties worked but no evens or methods found.


    I'm attaching file for your referenceproject.zip
    Last edited by sthiru588; Apr 28th, 2018 at 11:23 AM.

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by fafalone View Post
    The OCX should have a declaration for a public event, like
    Public Event FilesDrop(sFiles() As String)
    then whenever the event is triggered in the OCX,
    RaiseEvent FilesDrop(filelist)

    The host form will list that event like any other event for other objects...
    Private Sub YourOCXName_FilesDrop(sFiles() As String)
    May I know how do you do the same for Listview1.listitems


    Please help

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

    Re: VB6 OCX - How to Event handle in another application

    Which listview events do you want raised? Or are you wanting to just get the OCX to give you the listview control so you can do what you want with the listview listitems?

    For example, in your ocx:
    Code:
    Public Function GetListView() As ListView
        Set GetListView = myListviewControl  ' change to your ocx's actual listview name
    End Function
    With the above, you should be able to access the listitems something like this:
    Code:
    Dim n As Long
    With theOcx.GetListView
        For n= 1 to .ListItems.Count
            ' do what is needed
        Next
    End With
    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}

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by LaVolpe View Post
    Which listview events do you want raised? Or are you wanting to just get the OCX to give you the listview control so you can do what you want with the listview listitems?

    For example, in your ocx:
    Code:
    Public Function GetListView() As ListView
        Set GetListView = myListviewControl  ' change to your ocx's actual listview name
    End Function
    With the above, you should be able to access the listitems something like this:
    Code:
    Dim n As Long
    With theOcx.GetListView
        For n= 1 to .ListItems.Count
            ' do what is needed
        Next
    End With

    Yes as you said I want to just get the OCX to give you the listview control so I can do what you want with the listview listitems

    Can you hep me with that

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by LaVolpe View Post
    Which listview events do you want raised? Or are you wanting to just get the OCX to give you the listview control so you can do what you want with the listview listitems?

    For example, in your ocx:
    Code:
    Public Function GetListView() As ListView
        Set GetListView = myListviewControl  ' change to your ocx's actual listview name
    End Function
    With the above, you should be able to access the listitems something like this:
    Code:
    Dim n As Long
    With theOcx.GetListView
        For n= 1 to .ListItems.Count
            ' do what is needed
        Next
    End With
    HI,


    I have researched and found one snippet.

    https://www.youtube.com/watch?v=MZzGAXuqpQ8

    In this video he inputting in the class module of combox and and events were pulled automatically.

    so if i input the below code in class and after compiling will that work if input as lst.listems instead of Listview1.listitems?



    Code:
    Private WithEvents lst As ListView

    *****Update: Unfortunately it doesn't work
    Last edited by sthiru588; Apr 28th, 2018 at 01:00 PM.

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

    Re: VB6 OCX - How to Event handle in another application

    sthiru588,

    I'm not at all clear on what you're doing. Here's all I know (with respect to what it sounds like you're doing).

    You can create a user control (UC). And, on that UC, you can put whatever other controls you want on it. In some cases, there might just be one child control (possibly a ListView). And then, through use of the Event declaration statement and the RaiseEvent statement, you can "pass through" whatever events you like from the child control (to whatever form ultimately uses this UC). And, you can also do similar things for the child's properties, by constructing Get and Let/Set procedures.

    And, if you want to save certain properties specific to the UC during IDE design-time (and not the child control), you can use a combination of UserControl_ReadProperties, UserControl_WriteProperties, and PropertyChanged to save those "extra" properties of your UC.

    Once that's all done, you can either use your UC directly in a project (pulling in the UC's source code), or you can compile it into an OCX ActiveX file, and use it that way. Either way, it should work the same, if you've done things correctly.

    Beyond that, I'm confused as to what you're asking. Everything I've outlined has worked just fine for me for many years.

    Good Luck,
    Elroy

    EDIT1: Also, I'm not sure I've got a WithEvents in any of my UCs.
    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.

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    8

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by Elroy View Post
    sthiru588,

    I'm not at all clear on what you're doing. Here's all I know (with respect to what it sounds like you're doing).

    You can create a user control (UC). And, on that UC, you can put whatever other controls you want on it. In some cases, there might just be one child control (possibly a ListView). And then, through use of the Event declaration statement and the RaiseEvent statement, you can "pass through" whatever events you like from the child control (to whatever form ultimately uses this UC). And, you can also do similar things for the child's properties, by constructing Get and Let/Set procedures.

    And, if you want to save certain properties specific to the UC during IDE design-time (and not the child control), you can use a combination of UserControl_ReadProperties, UserControl_WriteProperties, and PropertyChanged to save those "extra" properties of your UC.

    Once that's all done, you can either use your UC directly in a project (pulling in the UC's source code), or you can compile it into an OCX ActiveX file, and use it that way. Either way, it should work the same, if you've done things correctly.

    Beyond that, I'm confused as to what you're asking. Everything I've outlined has worked just fine for me for many years.

    Good Luck,
    Elroy

    EDIT1: Also, I'm not sure I've got a WithEvents in any of my UCs.
    Thank you so much for the reply.

    But I'm not sure how to code that, I tried multiple ways but it does not work.

    It would be really great if you help with sample file.

    Thanks a billion in advance.
    Attached Files Attached Files

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

    Re: VB6 OCX - How to Event handle in another application

    Quote Originally Posted by sthiru588 View Post
    But I'm not sure how to code that, I tried multiple ways but it does not work.
    Ok sthiru588,

    I'm not going to work on your project, but I will provide a basic example of the things I outlined. I just created a new UC (named UserControl1) with nothing but a single TextBox on it. I could have used a ListView but the principles are all the same, and a TextBox doesn't require an extra dependency for my example. My UC just looks something like the following:

    Name:  UC1.png
Views: 866
Size:  3.4 KB

    And I then wrote the following code for it. Just a few things to show what I outlined in post #14. You can follow those examples and pass through more events and properties, and/or create more custom properties for your own UC.

    Code:
    
    Option Explicit
    '
    Event Change()                                                                  ' Example of how to declare a simple pass-through event in our UC.
    Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)  ' Example of how to declare a pass-through event with arguments in our UC.
    '
    
    Dim msSomeSpecialProperty As String         ' Example of place to store (when running) some "extra" property of our UC.
    '
    
    
    
    
    Private Sub Text1_Change()
        ' Example of how to pass through the Text1's Change event.
        RaiseEvent Change
    End Sub
    
    Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ' Example of how to pass through the Text1's MouseMove event.
        RaiseEvent MouseMove(Button, Shift, X, Y)
    End Sub
    
    
    
    
    
    Public Property Get Text() As String
        ' This, combined with the "Let Text" is an example of how to pass through a property of Text1.
        Text = Text1.Text
    End Property
    
    Public Property Let Text(sNewValue As String)
        ' This, combined with the "Get Text" is an example of how to pass through a property of Text1.
        Text1.Text = sNewValue
    End Property
    
    
    
    
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        ' Example of how to have an "extra" property for our UC.  This reads it from the PropertyBag (which initially comes from the source code file).
        msSomeSpecialProperty = PropBag.ReadProperty("SomeSpecialProperty", "DefaultValue")
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        ' Example of how to have an "extra" property for our UC.  This writes it to the PropertyBag (which is initially saved in the source code file).
        PropBag.WriteProperty "SomeSpecialProperty", msSomeSpecialProperty
    End Sub
    
    Public Property Get SomeSpecialProperty() As String
        ' This, combined with the "Let SomeSpecialProperty" is an example of how to expose our "extra" property during IDE design-time and run-time.
        SomeSpecialProperty = msSomeSpecialProperty
    End Property
    
    Public Property Let SomeSpecialProperty(sNewValue As String)
        ' This, combined with the "Get SomeSpecialProperty" is an example of how to expose our "extra" property during IDE design-time and run-time.
        msSomeSpecialProperty = sNewValue
        PropertyChanged "SomeSpecialProperty"       ' This is necessary to let the IDE know when the Properties Window has changed the property so UserControl_WriteProperties can be called.
    End Property
    
    You should be able to start your own default VB6 project and paste the above code into your own UserControl1. Be sure to read through my comments in the code, as that helps to understand what's what.

    Then, I threw this new UserControl1 onto the default Form1. That looked something like the following:

    Name:  Form1.png
Views: 808
Size:  2.5 KB

    Be sure to recognize that, when I put it onto Form1 from the Toolbox, it named it UserControl11. This is expected behavior, as it took its base name (UserControl1) and placed another 1 onto the name when actually using it on a form. The next one I'd use would be named UserControl12, etcetera.

    And then I wrote the following code to test my new UC, with this code placed in Form1:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Debug.Print "UserControl11 has special property value of: "; UserControl11.SomeSpecialProperty
    End Sub
    
    Private Sub UserControl11_Change()
        ' This tests both the UC's Change event and the UC's Text property.
        Debug.Print "UserControl11 changed to: "; UserControl11.Text
    End Sub
    
    Private Sub UserControl11_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Debug.Print "UserControl11's mouse is at: "; X, Y
    End Sub
    
    This allowed me to test that my properties and events are working as expected. To test, just watch the Immediate Window in the IDE while executing. You'll see your new "SomeSpecialProperty" first off. And then, when you move the mouse over the control, you'll see your X,Y mouse values. And then, if you click the UC and type, you'll see the text's changed values.

    And, while in IDE design-mode, you can see both the "Text" and "SomeSpecialProperty" properties on the Property Window:

    Name:  Props.png
Views: 934
Size:  14.2 KB

    I'm not sure what else to say, other than that this all works absolutely fine for me.

    There's a great deal more that could be said about UCs, but these are the fundamentals.

    Good Luck,
    Elroy
    Last edited by Elroy; Apr 29th, 2018 at 08:49 AM.
    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.

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

    Re: VB6 OCX - How to Event handle in another application

    sthiru588,

    I'll also say, if you'd like to outline a very specific example of a problem you're having, along with the specific procedures in your UC and the specific procedures on some form, where you're trying to implement the situation, we might be able to help further.

    For example, you say you're having trouble with events. Pick an event and show us the UC's code for that event, and show us the code in some form where you're trying to process that UC's event.

    Otherwise, I'm not sure we know what you're talking about, and I suspect we're not willing to just blindly dig through your project.

    Good Luck,
    Elroy
    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.

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