class types for custom objects

porkchop

New member
Joined
Aug 9, 2011
Messages
2
Programming Experience
5-10
hi there. i'm the most dangerous kind on here, enough knowledge to tackle a big project, but self taught and not 100% professional standard lol.... need help.

i have a bunch of dynamically created buttons of a custom class I have created. let's call it "customButton". it inherits all button functionality and just adds a bunch of custom properties which arent relevant here.

i want to create a sub that when called, will look at the form, find all instances of the customButton class, and destroy them.

however, using code i've researched online, it doesn;t see the custom class at all which is strange. I have something like this :

for i as integer = me.controls.count - 1 to 0 step -1
dim ctrl = me.controls(i)
if typeof (ctrl) is customButton then
ctrl.dispose()
end if
next

this doesn't do a thing. BUT**** if I change it to if typeof (ctrl) is Button then it successfully removes all buttons (dont want to do that of course but it's helpful to prove the code's ok)

so what it seems to me is that the class i created doesn't work with typeof. Is there something I've omitted in the class definition maybe that would say "this class is known as : "
? i just assumed its typeof would be the class name?
 
First off, you will probably need some way to search controls recursively. If some buttons are inside containers like a panel, you won't find them right down at the root. Use this extension module for the Control class:

Imports System.Runtime.CompilerServices

Public Module ControlExtensions

    <Extension()> _
    Public Function FindRecursive(Of T As Control)(ByVal ctrl As Control, ByVal predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
        If ctrl Is Nothing OrElse ctrl.Controls.Count = 0 Then Return New List(Of T)

        Return ctrl.Controls.OfType(Of T)() _
                            .Where(predicate) _
                            .Union(ctrl.Controls.Cast(Of Control) _
                                                .SelectMany(Of T)(Function(c) c.FindRecursive(predicate)))
    End Function

    <Extension()> _
    Public Function FindRecursive(Of T As Control)(ByVal ctrl As Control, ByVal predicate As Func(Of T, Boolean), ByVal depthLimit As Integer) As IEnumerable(Of T)
        If ctrl Is Nothing OrElse ctrl.Controls.Count = 0 Then Return New List(Of T)

        If depthLimit = 0 Then
            Return ctrl.Controls.OfType(Of T).Where(predicate)
        Else
            Return ctrl.Controls.OfType(Of T) _
                                .Where(predicate) _
                                .Union(ctrl.Controls.Cast(Of Control) _
                                                    .SelectMany(Of T)(Function(c) c.FindRecursive(predicate, depthLimit - 1)))
        End If
    End Function

End Module


Then you can use this code to find your buttons:

Dim myCustomButtons = Me.FindRecursive(Of CustomButton)((Function(x) True), 5)


The FindRecursive extension method is a generic that takes in a predicate (to let you choose which controls to select) and a maximum search depth, and returns an IEnumerable(Of T) containing the found controls.

However, why are you destroying buttons at runtime in the first place?
 
However, why are you destroying buttons at runtime in the first place?

welll... good point, i might be doing it more complicated than i need.

basically this is what the application is doing :

1) on load, it looks at a database table to load some customers into clickable buttons, and creates the buttons at runtime
2) if i add another button, i want the form to reload the buttons. if i simply re-call the original makebuttons() procedure, i get all the buttons repeated. so i am attempting to clear all buttons and then re-build the new updated list.
3) the reason i dont just tag a new button on the end is that they are meant to be in numeric order based on the database table. so depending on where the new record is added it could be different every time.
 
Back
Top