Public Class Form1
Inherits System.Windows.Forms.Form
Private mWafer As Wafer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents BtnAdd As System.Windows.Forms.Button
Friend WithEvents btnDelete As System.Windows.Forms.Button
Friend WithEvents lstJobs As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.BtnAdd = New System.Windows.Forms.Button
Me.btnDelete = New System.Windows.Forms.Button
Me.lstJobs = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'BtnAdd
'
Me.BtnAdd.Location = New System.Drawing.Point(160, 368)
Me.BtnAdd.Name = "BtnAdd"
Me.BtnAdd.TabIndex = 0
Me.BtnAdd.Text = "Add"
'
'btnDelete
'
Me.btnDelete.Location = New System.Drawing.Point(304, 368)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.TabIndex = 1
Me.btnDelete.Text = "Delete"
'
'lstJobs
'
Me.lstJobs.ItemHeight = 20
Me.lstJobs.Location = New System.Drawing.Point(8, 8)
Me.lstJobs.Name = "lstJobs"
Me.lstJobs.Size = New System.Drawing.Size(544, 324)
Me.lstJobs.TabIndex = 2
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
Me.ClientSize = New System.Drawing.Size(574, 450)
Me.Controls.Add(Me.lstJobs)
Me.Controls.Add(Me.btnDelete)
Me.Controls.Add(Me.BtnAdd)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mWafer = New Wafer
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
lstJobs.DataSource = Nothing
mWafer.Add(New Job("newname"))
lstJobs.DataSource = mWafer
lstJobs.DisplayMember = "Name"
lstJobs.Refresh()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim curJob As Job = lstJobs.SelectedValue
lstJobs.DataSource = Nothing
mWafer.remove(curJob)
lstJobs.DataSource = mWafer
End Sub
End Class
Friend Class Wafer
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal curDisp As Job)
If Not AlreadyHaveName(curDisp.Name) Then
Me.List.Add(curDisp)
'if name is unique, then proceed to add
Else
'append a number to the end
Dim trykey As String = curDisp.Name + "#2"
Dim i As Integer = 2
While AlreadyHaveName(trykey)
trykey = curDisp.Name + "#" + i.ToString
i = i + 1
End While
curDisp.Name = trykey
Me.List.Add(curDisp)
End If
End Sub
Private Function AlreadyHaveName(ByVal name As String) As Boolean
Dim contain As Boolean = False
For Each job As Job In Me.List
If job.Name = name Then
contain = True
Exit For
End If
Next
Return contain
End Function
Public Sub remove(ByVal curDisp As Job)
Me.List.Remove(curDisp)
End Sub
Default Public Property Item(ByVal index As Integer) As Job
Get
Return Me.List.Item(index)
End Get
Set(ByVal Value As Job)
Me.List.Item(index) = Value
End Set
End Property
End Class
Friend Class Job
Private mName As String
Public Sub New(ByVal Name As String)
mName = Name
End Sub
Friend Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property
Public Shadows ReadOnly Property ToString() As String
Get
Return mName
End Get
End Property
End Class