locked
custom controls and inherits RRS feed

  • Question

  • ok so i have 3 custom controls which i have ultra-cleverly named: shelf, cabinet, & bin

    these three controls also inherit the panel class

    So heres my question:

    when i run this:
    1for each ctl in me.controls 
    2 if typeof ctl is panel then 
    3    msgbox("panel"
    4 elseif typeof ctl is cabinet then 
    5    msgbox("Cabinet"
    6 elseif typeof ctl is bin then 
    7    msgbox("Bin"
    8 elseif typeof ctl is shelf then 
    9    msgbox("shelf"
    10 end if 
    11next 
    (psuedo'd the code --> may not be exact but you get the jist)

    for each control i get 2 message boxes (1 for panel, and another for the actual type of ctl)


    but when i run this:

    1for each ctl in me.controls 
    2 if typeof ctl is shelf then 
    3    msgbox("shelf"
    4 elseif typeof ctl is cabinet then 
    5    msgbox("Cabinet"
    6 elseif typeof ctl is bin then 
    7    msgbox("Bin"
    8 elseif typeof ctl is panel then 
    9    msgbox("Panel"
    10 end if 
    11next 
    (psuedo'd the code --> may not be exact but you get the jist)

    i only get 1 msgbox (that of the type of control)


    so my question is, when you use the inherits ___ does it actually give the control 2 types IE: (panel & shelf)


    - learning visual basic
    Tuesday, November 4, 2008 8:33 PM

Answers

  • The problem with your Pseudo code is that it doesn't exemplify what you say it does. It looks like both for/each loops give the same result - one message box for each control (can't paste it easily because you have put line numbers in there).

    Regardless, a control which inherits from another control is both types: in your example, a Shelf is also a Panel, both of which are Controls.

    Stephen J Whiteley
    • Marked as answer by nobugz Wednesday, November 5, 2008 1:44 PM
    Tuesday, November 4, 2008 9:54 PM

All replies

  • The problem with your Pseudo code is that it doesn't exemplify what you say it does. It looks like both for/each loops give the same result - one message box for each control (can't paste it easily because you have put line numbers in there).

    Regardless, a control which inherits from another control is both types: in your example, a Shelf is also a Panel, both of which are Controls.

    Stephen J Whiteley
    • Marked as answer by nobugz Wednesday, November 5, 2008 1:44 PM
    Tuesday, November 4, 2008 9:54 PM
  • ah, you are correct, as i psuedo'd it i assumed it would produce the same results as my project at work, in which the controls were being loaded from a DB and created.

    when i loaded the controls, i had the panel one at the top, thus creating all panels, not shelves, cabinets and bins.

    when i moved it to the bottom it fixed this problem, but i wanted to know if it did cause the class to have 2 types.

    Thanks

    here some copy-paste code that will show this as an example, just paste into a form called form1
    use left click to cycle through one if-then and right click to cycle the other

    Public Class Form1 
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
            Dim X As New shelf 
            Me.Controls.Add(X) 
        End Sub 
        Sub subb1()                              ' 
            For Each ctl In Me.Controls          ' 
                If TypeOf ctl Is Panel Then      ' 
                    MsgBox("Panel")              ' This is cycle one 
                ElseIf TypeOf ctl Is shelf Then  ' It is in the order of 
                    MsgBox("shelf")              ' check if ctl is panel 
                End If                           ' then check if ctl is shelf 
            Next                                 ' 
        End Sub                                  '============================= 
        Sub subb2()                              '============================= 
            For Each ctl In Me.Controls          ' 
                If TypeOf ctl Is shelf Then      ' 
                    MsgBox("shelf")              ' 
                ElseIf TypeOf ctl Is Panel Then  ' This is Cycle two 
                    MsgBox("panel")              ' It is in the order of 
                End If                           ' Check if ctl is shelf 
            Next                                 ' then check if ctl is panel 
        End Sub                                  ' 
     
        Private Sub Form1_MouseClick(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick 
            If e.Button = Windows.Forms.MouseButtons.Left Then 
                subb1() 
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then 
                subb2() 
            End If 
        End Sub 
    End Class 
    Public Class shelf : Inherits Panel 
     
    End Class 
     
     


    - learning visual basic
    Wednesday, November 5, 2008 1:53 AM
  • I like nice cut/copy/paste code...makes it easy to replicate an issue :)

    I do notice that you have Option Explicit set to Off on your projects (I always have it on) since the ctl variable in the For/Each loops brings up an error. I'd recommend having that set to On. Can save a lot of bugs further on down the line. It'll force you to explicitly declare variable types.

    As you can see, your example demonstrates that the Shelf object is both a Panel and a Shelf.

    Stephen J Whiteley
    Wednesday, November 5, 2008 12:36 PM