Home All Groups Group Topic Archive Search About

[2008] Collection of DipSwitches

Author
8 Jun 2009 9:12 PM
tim8w via DotNetMonster.com
I have a simple control called DipSwitch. It consists of a PictureBox and an
ImageList. Here is the code:

[CODE]
Imports System.ComponentModel
Imports System.Drawing.Drawing2D

Public Class DipSwitch

    Private m_state As Boolean = False

    <CategoryAttribute("Appearance"), DefaultValueAttribute(""), _
    DescriptionAttribute("State of DipSwitch.")> _
    Public Property State() As Boolean
        Get
            State = m_state
        End Get
        Set(ByVal value As Boolean)
            m_state = value
            Me.Invalidate()
        End Set
    End Property

    Private Sub pbDipSwitch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pbDipSwitch.Click
        If m_state = False Then
            pbDipSwitch.Image = ilDipSwitch.Images(1)
            m_state = True
        Else
            pbDipSwitch.Image = ilDipSwitch.Images(0)
            m_state = False
        End If
    End Sub
End Class
[/CODE]

I am trying to create another control Port, which is a collection of
DipSwitches. I can't seem to get the collection to work correctly. It is
defined simply as:

[CODE]
Public Class Port

    Private m_DipSwitches As DipSwitchCollection

    <CategoryAttribute("Appearance"), DefaultValueAttribute(""), _
    DescriptionAttribute("Collection of DipSwitches.")> _
    Public Property DipSwitches() As DipSwitchCollection
        Get
            DipSwitches = m_DipSwitches
        End Get
        Set(ByVal value As DipSwitchCollection)
            m_DipSwitches = value
            Me.Invalidate()
        End Set
    End Property

End Class

Public Class DipSwitchCollection
    Inherits System.Collections.CollectionBase

    ' Restricts to DipSwitch types, items that can be added to the collection.

    Public Sub Add(ByVal aDipSwitch As DipSwitch)
        ' Invokes Add method of the List object to add a widget.
        List.Add(aDipSwitch)
    End Sub

    Public Sub Remove(ByVal index As Integer)
        ' Check to see if there is a widget at the supplied index.
        If index > Count - 1 Or index < 0 Then
            ' If no widget exists, a messagebox is shown and the operation is

            ' cancelled.
            System.Windows.Forms.MessageBox.Show("Index not valid!")
        Else
            ' Invokes the RemoveAt method of the List object.
            List.RemoveAt(index)
        End If
    End Sub

    ' This line declares the Item property as ReadOnly, and
    ' declares that it will return a DipSwitch object.
    Public ReadOnly Property Item(ByVal index As Integer) As DipSwitch
        Get
            ' The appropriate item is retrieved from the List object and
            ' explicitly cast to the DipSwitch type, then returned to the
            ' caller.
            Return CType(List.Item(index), DipSwitch)
        End Get
    End Property
End Class
[/CODE]


If I create an instance of the Port, I can add DipSwitches to the Collection
through the interface. But they do not show up on the form. Likewise, after
adding DipSwitches through the interface, if I try and add more, the original
ones disappear, although they remain defined in the Designer module.

Does anyone have experience doing anything like this?


Author
8 Jun 2009 11:07 PM
James Hahn
Creating an instance of a Port and adding it to the collection has nothing
to do with displaying it on the form. If you intend that the Collection
represents Ports that actually appear on the form then you need to include
code in the Add method to add the Port to the Form (and presumably the
Remove method should remove the Port from the form and possibly destroy it).
Alternatively, you need to create the control and add it to the form before
adding it to the Colelction.

You haven't included any code to indicate how you are instantiating the
Ports, adding them to the form or adding them to or removing them from the
list.  That's the place where your problem will be.

I have code that does exactly what you are ttrying to do, but instead of
creating a collections class I use a List (Of ...). Controls are created and
positioned on the form in response to a user request, then I add the control
to the list and reference it through the list item.  When the user chooses
Delete I destroy the control and remove it from the list.

"tim8w via DotNetMonster.com" <u31412@uwe> wrote in message
news:974f7faccfbea@uwe...
Show quoteHide quote
>I have a simple control called DipSwitch. It consists of a PictureBox and
>an
> ImageList. Here is the code:
>
> [CODE]
> Imports System.ComponentModel
> Imports System.Drawing.Drawing2D
>
> Public Class DipSwitch
>
>    Private m_state As Boolean = False
>
>    <CategoryAttribute("Appearance"), DefaultValueAttribute(""), _
>    DescriptionAttribute("State of DipSwitch.")> _
>    Public Property State() As Boolean
>        Get
>            State = m_state
>        End Get
>        Set(ByVal value As Boolean)
>            m_state = value
>            Me.Invalidate()
>        End Set
>    End Property
>
>    Private Sub pbDipSwitch_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles pbDipSwitch.Click
>        If m_state = False Then
>            pbDipSwitch.Image = ilDipSwitch.Images(1)
>            m_state = True
>        Else
>            pbDipSwitch.Image = ilDipSwitch.Images(0)
>            m_state = False
>        End If
>    End Sub
> End Class
> [/CODE]
>
> I am trying to create another control Port, which is a collection of
> DipSwitches. I can't seem to get the collection to work correctly. It is
> defined simply as:
>
> [CODE]
> Public Class Port
>
>    Private m_DipSwitches As DipSwitchCollection
>
>    <CategoryAttribute("Appearance"), DefaultValueAttribute(""), _
>    DescriptionAttribute("Collection of DipSwitches.")> _
>    Public Property DipSwitches() As DipSwitchCollection
>        Get
>            DipSwitches = m_DipSwitches
>        End Get
>        Set(ByVal value As DipSwitchCollection)
>            m_DipSwitches = value
>            Me.Invalidate()
>        End Set
>    End Property
>
> End Class
>
> Public Class DipSwitchCollection
>    Inherits System.Collections.CollectionBase
>
>    ' Restricts to DipSwitch types, items that can be added to the
> collection.
>
>    Public Sub Add(ByVal aDipSwitch As DipSwitch)
>        ' Invokes Add method of the List object to add a widget.
>        List.Add(aDipSwitch)
>    End Sub
>
>    Public Sub Remove(ByVal index As Integer)
>        ' Check to see if there is a widget at the supplied index.
>        If index > Count - 1 Or index < 0 Then
>            ' If no widget exists, a messagebox is shown and the operation
> is
>
>            ' cancelled.
>            System.Windows.Forms.MessageBox.Show("Index not valid!")
>        Else
>            ' Invokes the RemoveAt method of the List object.
>            List.RemoveAt(index)
>        End If
>    End Sub
>
>    ' This line declares the Item property as ReadOnly, and
>    ' declares that it will return a DipSwitch object.
>    Public ReadOnly Property Item(ByVal index As Integer) As DipSwitch
>        Get
>            ' The appropriate item is retrieved from the List object and
>            ' explicitly cast to the DipSwitch type, then returned to the
>            ' caller.
>            Return CType(List.Item(index), DipSwitch)
>        End Get
>    End Property
> End Class
> [/CODE]
>
>
> If I create an instance of the Port, I can add DipSwitches to the
> Collection
> through the interface. But they do not show up on the form. Likewise,
> after
> adding DipSwitches through the interface, if I try and add more, the
> original
> ones disappear, although they remain defined in the Designer module.
>
> Does anyone have experience doing anything like this?
>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-vb-net/200906/1
>