Click to See Complete Forum and Search --> : [2008] Collection of DipSwitches...


tim8w
June 8th, 2009, 05:11 PM
I have a simple control called DipSwitch. It consists of a PictureBox and an ImageList. Here is the 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


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:


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



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?

dglienna
June 8th, 2009, 10:30 PM
Call both of them from the form.paint or control.paint event, then call .invalidate to draw it