Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Edit Item In Checked ListBox(......help help)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] [2005] Edit Item In Checked ListBox(......help help)

    I stripped part of my project and included where i need help.

    ISSUE:
    As in the attached sample, i have two checked listboxes ona form and a contextmenu for editing the items in the listboxes.
    When i click on one or more items in the first Listbox, it brings the values related to the checked item in(Admin Level 1) to the Admin Level 2 listbox.
    I populate and retrieve the values of the listboxes using an array class.

    MAIN ISSUE:
    When i populate (Admin Level 2 listbox) using the selectedIndexChanged Event for (Admin Level 1 listbox), i can't edit the items in the (listbox 1)!, yet i can edit the items in listbox 2!
    To check this, try commenting out the routine that populates listbox 2 and then Edit the items in listbox 1. It works perfect, but when you uncomment it, it gives an error "Unable to cast object of type 'System.String' to type 'EditCheckedListBox.cls_ListComboItem'"! I get puzzled.
    What am i getting wrong here. The SQL Statement is correct and it seems it gets called again.
    Please help me.

    Note: To Edit, right click an item in the listbox and click Edit.
    I am sorry for not commenting out the code. But i presume its pretty
    simple to understand for an average man
    Attached Files Attached Files
    Last edited by maps; Nov 2nd, 2006 at 06:17 AM. Reason: ...

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Edit Item In Checked ListBox(......help help)

    i am loosing alot of hair! any luck folks?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Edit Item In Checked ListBox(......help help)

    Still loosing hair.....any hero in here!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Edit Item In Checked ListBox(......help help)

    Hi,
    I found couple the errors in your code. In the edit sub, you were trying to replace a cls_ListComboItem object with a String object. It would've been OK if you don't use that item again (that's why you can edit items in listbox2, but it crashes when you edit items in listbox1). However, you want to update listbox2 after the edit, thus it requires the use of the newly edited item (which is now a string)... Trying to cast a string to cls_ListComboItem object blew the application.... So I fixed it for you.
    Another minor problem is you forget to check for null objects in another sub. I fixed that for you too.
    VB Code:
    1. Sub EditItems(ByRef lstControl As CheckedListBox)
    2.         If lstControl.CheckedItems.Contains(lstControl.SelectedItem) Then ' Check WHETHER ITS SELECTED AND CHECKED.
    3.             _frmEditItem = New frmEditItem
    4.             _frmEditItem.tbItem.Text = lstControl.SelectedItem.ToString
    5.  
    6.             If _frmEditItem.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
    7.                 _frmEditItem.InputName(inPut)
    8.                 '==============================================
    9.                 '==============================================
    10.                 Dim renamedItem As New cls_ListComboItem()
    11.                 renamedItem.Name = inPut
    12.                 renamedItem.ID = DirectCast(lstControl.SelectedItem, cls_ListComboItem).ID
    13.                 If lstControl.Items.Contains(renamedItem) Then ' Check Whether It contains The Value
    14.                     MessageBox.Show("Item Already Exits" & " " & Chr(34) & inPut & Chr(34), "Exits", MessageBoxButtons.OK)
    15.                     Exit Sub
    16.                 Else
    17.                     lstControl.Items.Item(lstControl.SelectedIndex) = renamedItem
    18.  
    19.                     If Me.lstAdminLevel1.Focused Then
    20.                         UpdateTable(Me.lstAdminLevel1, "tblAdminBdry1", "AdminBdry1", "AdminBdry1", CType(Me.lstAdminLevel1.SelectedItem, cls_ListComboItem).Name)
    21.                     ElseIf Me.lstAdminLevel2.Focused Then
    22.                         UpdateTable(Me.lstAdminLevel2, "tblAdminBdry2", "AdminBdry2", "AdminBdry2", CType(Me.lstAdminLevel2.SelectedItem, cls_ListComboItem).Name)
    23.                     End If
    24.                     '=============================================================
    25.                     '=============================================================
    26.                 End If
    27.  
    28.             End If
    29.         Else
    30.             MessageBox.Show("Choose Item To Edit", "Choose", MessageBoxButtons.OK, MessageBoxIcon.Information)
    31.             Exit Sub
    32.         End If
    33.  
    34.     End Sub
    35.  
    36. Private Sub PopulateAdminBdry2List()  ' gets the Items For The Counties And Appends Other Counties
    37.         'Dim ctrl As CheckedListBox = Me.lstAdminLevel1
    38.         With Me.lstAdminLevel1
    39.             If Me.lstAdminLevel1.CheckedItems.Count = 0 Then
    40.  
    41.             Else
    42.                 Dim strSQL As String = "SELECT * FROM tblAdminBdry2 WHERE AdminBdry1_ID IN ("
    43.                 For i As Integer = 0 To .CheckedItems.Count - 1
    44.                     Dim itm As Object = .CheckedItems.Item(i)
    45.                     If Not IsNothing(itm) Then
    46.                         strSQL += "" & CType(itm, cls_ListComboItem).ID & ","
    47.                     End If
    48.                 Next
    49.                 strSQL = strRight(strSQL, 1) & ")"
    50.                 PopulateColumnCheckedList(Me.lstAdminLevel2, strSQL, "AdminBdry2_ID", "AdminBdry2")
    51.             End If
    52.         End With
    53.     End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Edit Item In Checked ListBox(......help help)

    Stanav! You are the man.
    You really took the time to look into and understand my code.
    Some times all i can say is, thanks so much. Merci Beacoup!

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