WithEvents error 
Author Message
 WithEvents error

I've created a collection of ListBoxs now I would like
to be able to recognize when one of the ListBox of
the collection is clicked so I can handle the event.
I've tried the WithEvents as follows but I get an error
stating that lstBoxCollection is not defined.The collection
does becomes populated with the ListBoxs.
What am I missing?

Public lstBoxCollection As Collection = New Collection()
Public WithEvents obj As lstBoxCollection



Mon, 31 Jan 2005 02:17:49 GMT  
 WithEvents error
Hi Tom,

When you create a ListBox, you can use addhandler to set an event handler
for the mouse click.

For example, if you had the following handler:

Private Sub ListBoxClickHandler(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
    MsgBox("List box clicked")
End Sub

When you create the ListBox, you can use addhandler as shown below:

Dim newListBox As New ListBox()
AddHandler newListBox.MouseDown, AddressOf ListBoxClickHandler

Then, you can add the new ListBox to your collection.

Ernest, Steve, and Sunder
VB.Net

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> I've created a collection of ListBoxs now I would like
> to be able to recognize when one of the ListBox of
> the collection is clicked so I can handle the event.
> I've tried the WithEvents as follows but I get an error
> stating that lstBoxCollection is not defined.The collection
> does becomes populated with the ListBoxs.
> What am I missing?

> Public lstBoxCollection As Collection = New Collection()
> Public WithEvents obj As lstBoxCollection



Mon, 31 Jan 2005 03:26:11 GMT  
 WithEvents error
in this case, the lstBoxCollection is a variable you have declared. So you
cannot use it to declare a WithEvent variable because you need to specify a
type when declaring a variable.

one way of doing this could be,

    Dim lstBoxCollection As New Collection()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
       'Add all the listboxes to the collection
        lstBoxCollection.Add(ListBox1)
        lstBoxCollection.Add(ListBox2)
        AddHandlerToListBoxes()
    End Sub

    Sub AddHandlerToListBoxes()
        Dim lstBox As ListBox

        ' Add Sub ListBox_DoubleClick as the handler for each of the
ListBox's DoubleClick Event
        For Each lstBox In lstBoxCollection
            AddHandler lstBox.DoubleClick, AddressOf ListBox_DoubleClick
        Next
    End Sub

    ' this is the handler to handle the DoubleClick for all the ListBoxes
    Private Sub ListBox_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs)
        MsgBox(CType(sender, ListBox).Name)
    End Sub

thanks,
VB Team

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> I've created a collection of ListBoxs now I would like
> to be able to recognize when one of the ListBox of
> the collection is clicked so I can handle the event.
> I've tried the WithEvents as follows but I get an error
> stating that lstBoxCollection is not defined.The collection
> does becomes populated with the ListBoxs.
> What am I missing?

> Public lstBoxCollection As Collection = New Collection()
> Public WithEvents obj As lstBoxCollection



Mon, 31 Jan 2005 03:43:49 GMT  
 WithEvents error

Quote:
> I've created a collection of ListBoxs now I would like
> to be able to recognize when one of the ListBox of
> the collection is clicked so I can handle the event.
> I've tried the WithEvents as follows but I get an error
> stating that lstBoxCollection is not defined.The collection
> does becomes populated with the ListBoxs.
> What am I missing?

> Public lstBoxCollection As Collection = New Collection()
> Public WithEvents obj As lstBoxCollection

Do you have a class called lstBoxCollection? The word after "As" is a type,
not a variable.

Armin



Mon, 31 Jan 2005 17:35:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. WithEvents Error

2. WithEvents and Run-time error 462: The remote server machine does not exists or is unavailable

3. WithEvents and Run-time error 462: The remote server machine does not exists or is unavailable

4. Get error using object in Module when declared WithEvents

5. Error Withevents and control arrays

6. Error 459 - WithEvents using DHTML

7. Automation error when resetting WithEvents HTMLDocument var

8. WithEvents - Closing table too fast after open gives error (and other gotchas with event programming)

9. DCOM + WithEvents = Automation error -2147023149 (800706d3)

10. WithEvents Bug AccessXP?

11. WithEvents and Enum

12. Custom classes and WithEvents programming samples

 

 
Powered by phpBB® Forum Software