Results 1 to 3 of 3

Thread: [fixed] error Binding Listbox to a Collection

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    20

    [fixed] error Binding Listbox to a Collection

    Here's my application, whenever I delete an item from the collection, I get the error

    "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Additional information: Specified argument was out of the range of valid values."

    Is there anything wrong with the way I delete the item ?


    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Private mWafer As Wafer
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents BtnAdd As System.Windows.Forms.Button
    33.     Friend WithEvents btnDelete As System.Windows.Forms.Button
    34.     Friend WithEvents lstJobs As System.Windows.Forms.ListBox
    35.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    36.         Me.BtnAdd = New System.Windows.Forms.Button
    37.         Me.btnDelete = New System.Windows.Forms.Button
    38.         Me.lstJobs = New System.Windows.Forms.ListBox
    39.         Me.SuspendLayout()
    40.         '
    41.         'BtnAdd
    42.         '
    43.         Me.BtnAdd.Location = New System.Drawing.Point(160, 368)
    44.         Me.BtnAdd.Name = "BtnAdd"
    45.         Me.BtnAdd.TabIndex = 0
    46.         Me.BtnAdd.Text = "Add"
    47.         '
    48.         'btnDelete
    49.         '
    50.         Me.btnDelete.Location = New System.Drawing.Point(304, 368)
    51.         Me.btnDelete.Name = "btnDelete"
    52.         Me.btnDelete.TabIndex = 1
    53.         Me.btnDelete.Text = "Delete"
    54.         '
    55.         'lstJobs
    56.         '
    57.         Me.lstJobs.ItemHeight = 20
    58.         Me.lstJobs.Location = New System.Drawing.Point(8, 8)
    59.         Me.lstJobs.Name = "lstJobs"
    60.         Me.lstJobs.Size = New System.Drawing.Size(544, 324)
    61.         Me.lstJobs.TabIndex = 2
    62.         '
    63.         'Form1
    64.         '
    65.         Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
    66.         Me.ClientSize = New System.Drawing.Size(574, 450)
    67.         Me.Controls.Add(Me.lstJobs)
    68.         Me.Controls.Add(Me.btnDelete)
    69.         Me.Controls.Add(Me.BtnAdd)
    70.         Me.Name = "Form1"
    71.         Me.Text = "Form1"
    72.         Me.ResumeLayout(False)
    73.  
    74.     End Sub
    75.  
    76. #End Region
    77.  
    78.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    79.         mWafer = New Wafer
    80.     End Sub
    81.  
    82.     Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
    83.         lstJobs.DataSource = Nothing
    84.         mWafer.Add(New Job("newname"))
    85.         lstJobs.DataSource = mWafer
    86.         lstJobs.DisplayMember = "Name"
    87.         lstJobs.Refresh()
    88.     End Sub
    89.  
    90.     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    91.         Dim curJob As Job = lstJobs.SelectedValue
    92.         lstJobs.DataSource = Nothing
    93.         mWafer.remove(curJob)
    94.         lstJobs.DataSource = mWafer
    95.     End Sub
    96. End Class
    97.  
    98. Friend Class Wafer
    99.     Inherits System.Collections.CollectionBase
    100.  
    101.     Public Sub Add(ByVal curDisp As Job)
    102.  
    103.         If Not AlreadyHaveName(curDisp.Name) Then
    104.             Me.List.Add(curDisp)
    105.  
    106.             'if name is unique, then proceed to add
    107.         Else
    108.             'append a number to the end
    109.             Dim trykey As String = curDisp.Name + "#2"
    110.  
    111.             Dim i As Integer = 2
    112.             While AlreadyHaveName(trykey)
    113.                 trykey = curDisp.Name + "#" + i.ToString
    114.                 i = i + 1
    115.             End While
    116.  
    117.             curDisp.Name = trykey
    118.             Me.List.Add(curDisp)
    119.  
    120.         End If
    121.     End Sub
    122.  
    123.     Private Function AlreadyHaveName(ByVal name As String) As Boolean
    124.         Dim contain As Boolean = False
    125.         For Each job As Job In Me.List
    126.             If job.Name = name Then
    127.                 contain = True
    128.                 Exit For
    129.             End If
    130.         Next
    131.         Return contain
    132.     End Function
    133.     Public Sub remove(ByVal curDisp As Job)
    134.         Me.List.Remove(curDisp)
    135.     End Sub
    136.  
    137.     Default Public Property Item(ByVal index As Integer) As Job
    138.         Get
    139.             Return Me.List.Item(index)
    140.         End Get
    141.         Set(ByVal Value As Job)
    142.             Me.List.Item(index) = Value
    143.         End Set
    144.     End Property
    145.  
    146. End Class
    147.  
    148. Friend Class Job
    149.     Private mName As String
    150.  
    151.     Public Sub New(ByVal Name As String)
    152.         mName = Name
    153.     End Sub
    154.     Friend Property Name() As String
    155.         Get
    156.             Return mName
    157.         End Get
    158.         Set(ByVal Value As String)
    159.             mName = Value
    160.         End Set
    161.     End Property
    162.  
    163.     Public Shadows ReadOnly Property ToString() As String
    164.         Get
    165.             Return mName
    166.         End Get
    167.     End Property
    168. End Class
    Last edited by superuser666; Nov 2nd, 2004 at 07:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width