CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Control Array and WithEvents Keyword

    In my project, I have 2 checkboxes in a control array. I have a class that watches for data modified for each of the controls on a passed in form. The code looks similar to the following:

    Code:
    Private WithEvents m_chkNewCheckbox1 As CheckBox
    
    Private Sub Command1_Click()
        
        Dim ctl As Control
        
        For Each ctl In Controls
            
            If TypeName(ctl) = "CheckBox" Then
                Set m_chkNewCheckbox1 = ctl
            End If
            
        Next ctl
        
        m_chkNewCheckbox1.Value = vbChecked
        
    End Sub
    
    Private Sub m_chkNewCheckbox1_Click()
        'Do Something
    End Sub
    I need the WithEvents in order for the rest of the code to work properly, but I keep getting an error (Run-time error '459': Object or class does not support the set of events). If I take WithEvents out, the code works fine (no errors), but then I can't trap stuff like 'm_chkNewCheckbox1_Click', etc.

    Is there any way to make the above code work with control arrays?
    I'd rather be wakeboarding...

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Control Array and WithEvents Keyword

    Instead of having WithEvents, why don't you put one checkbox on the form with index=0 and then add the other checkboxes using that checkbox.. this way you will be able to trap the Click event, you just need to have one checkbox present on the form first(u cud also make it visible=false)... and write all the code in its click event...

    and during run time u cud add other checkboxes to this control array and trap all the events associated with that control array in the first click event...

  3. #3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Re: Control Array and WithEvents Keyword

    Thanks vb_the_best, but that won't work for me. I have a class that gets passed a form. The class interates through each control on the form and creates a new class for each control that can be changed. I have to use the WithEvents keyword because the class, not the Form, needs to be able to see the Click event.

    BTW, I did not write this code, I'm just trying to add a control array to a form and make it work with the existing code... As a work around, I just made 3 separate checkbox controls instead of using a control array with 3 elements. But I would still like to try to find a way to make the control arrays work.
    I'd rather be wakeboarding...

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Control Array and WithEvents Keyword

    Hey,
    I tried using withevents in Class as well Form and it is working fine.. Could you be more specific as to what the problem is and if you could post some more code, it will be really helpful.

  5. #5
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Re: Control Array and WithEvents Keyword

    To reproduce the problem, open a new project, add a Command Button and a Checkbox. Copy and paste the checkbox and click Yes when it asks if you want to create a control array. Paste all the code in my previous post into the Form's Code Window. Run the Project. It will error at: Set m_chkNewCheckbox1 = ctl.

    Now, remove the WithEvents and run it again. It will do what it's supposed to (check the checkbox), but the Click Event will not fire (since WithEvents was removed).

    I need to be able to keep the control array and have the Click event fire on the Checkbox variable (m_chkNewCheckbox1), not on the Checkbox itself.
    I'd rather be wakeboarding...

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Control Array and WithEvents Keyword

    This is what i found on MS support site
    http://support.microsoft.com/default...25120121120120

    i think you cannot use WithEvents here!!

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

Featured