1 Attachment(s)
[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
Re: [2005] Edit Item In Checked ListBox(......help help)
i am loosing alot of hair! any luck folks?
Re: [2005] Edit Item In Checked ListBox(......help help)
Still loosing hair.....any hero in here!
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:
Sub EditItems(ByRef lstControl As CheckedListBox)
If lstControl.CheckedItems.Contains(lstControl.SelectedItem) Then ' Check WHETHER ITS SELECTED AND CHECKED.
_frmEditItem = New frmEditItem
_frmEditItem.tbItem.Text = lstControl.SelectedItem.ToString
If _frmEditItem.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
_frmEditItem.InputName(inPut)
'==============================================
'==============================================
Dim renamedItem As New cls_ListComboItem()
renamedItem.Name = inPut
renamedItem.ID = DirectCast(lstControl.SelectedItem, cls_ListComboItem).ID
If lstControl.Items.Contains(renamedItem) Then ' Check Whether It contains The Value
MessageBox.Show("Item Already Exits" & " " & Chr(34) & inPut & Chr(34), "Exits", MessageBoxButtons.OK)
Exit Sub
Else
lstControl.Items.Item(lstControl.SelectedIndex) = renamedItem
If Me.lstAdminLevel1.Focused Then
UpdateTable(Me.lstAdminLevel1, "tblAdminBdry1", "AdminBdry1", "AdminBdry1", CType(Me.lstAdminLevel1.SelectedItem, cls_ListComboItem).Name)
ElseIf Me.lstAdminLevel2.Focused Then
UpdateTable(Me.lstAdminLevel2, "tblAdminBdry2", "AdminBdry2", "AdminBdry2", CType(Me.lstAdminLevel2.SelectedItem, cls_ListComboItem).Name)
End If
'=============================================================
'=============================================================
End If
End If
Else
MessageBox.Show("Choose Item To Edit", "Choose", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
End Sub
Private Sub PopulateAdminBdry2List() ' gets the Items For The Counties And Appends Other Counties
'Dim ctrl As CheckedListBox = Me.lstAdminLevel1
With Me.lstAdminLevel1
If Me.lstAdminLevel1.CheckedItems.Count = 0 Then
Else
Dim strSQL As String = "SELECT * FROM tblAdminBdry2 WHERE AdminBdry1_ID IN ("
For i As Integer = 0 To .CheckedItems.Count - 1
Dim itm As Object = .CheckedItems.Item(i)
If Not IsNothing(itm) Then
strSQL += "" & CType(itm, cls_ListComboItem).ID & ","
End If
Next
strSQL = strRight(strSQL, 1) & ")"
PopulateColumnCheckedList(Me.lstAdminLevel2, strSQL, "AdminBdry2_ID", "AdminBdry2")
End If
End With
End Sub
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!