Dragging checked listbox item unchecks it

D

Dean Slindee

In a checked listbox, I am allowing drag/drop of the items within
(resequencing). Problem is, when dropping a checked item, the checked state
always reverts to unchecked (unwanted). Anyone know how to set the checked
state of a checked listbox item in code. Here is the drag/drop code, which
works fine, except for unchecking the dropped item:
Private Sub clbQueryItems_DragDrop(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles clbQueryItems.DragDrop

'ensures that the list item index is contained in the data.

If (e.Data.GetDataPresent(GetType(System.String))) Then

Dim item As Object = CType(e.Data.GetData(GetType(System.String)),
System.Object)

'perform drag and drop, depending upon the effect.

If (e.Effect = DragDropEffects.Copy Or _

e.Effect = DragDropEffects.Move) Then

'insert the item.

If (indexDropItem <> CheckedListBox.NoMatches) Then

clbQueryItems.Items.Insert(indexDropItem, item)

Else

clbQueryItems.Items.Add(item, True)

End If

End If

End If

End Sub



Thanks in advance,

Dean Slindee
 
P

Peter Huang

Hi Dean,

Thanks for your quickly reply!
Thanks for posting in the community. My name is Peter, and I will be
assisting you on this issue.
First of all, I would like to confirm my understanding of your issue.

From your description, I understand that when you do drag-drop in a
CheckedListBox the CheckState will not be copied.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.
I think you may try to Get the CheckState when you call DoDragDrop and you
can use the SetItemCheckState to set the CheckState according to the index.

Here I assume that you set the indexDragItem when you call DoDragDrop.
Since you call
CheckedListBox1.Items.Insert(indexDropItem, item) to insert your item, the
new inserted item's index will be indexDropItem.

Private Sub CheckedListBox1_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles CheckedListBox1.DragDrop
If (e.Data.GetDataPresent(GetType(System.String))) Then
Dim item As Object =
CType(e.Data.GetData(GetType(System.String)), System.Object)
'perform drag and drop, depending upon the effect.
If (e.Effect = DragDropEffects.Copy Or _
e.Effect = DragDropEffects.Move) Then
'insert the item.
Dim st As CheckState =
CheckedListBox1.GetItemCheckState(indexDragItem)
If (indexDropItem <> CheckedListBox.NoMatches) Then
CheckedListBox1.Items.Insert(indexDropItem, item)
CheckedListBox1.SetItemCheckState(indexDropItem, st)
Else
CheckedListBox1.Items.Add(item, True)
End If
End If
End If
End Sub

If you have any concern on this issue,please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
P

Peter Huang

Hi Dean,

Dis my suggestion help you?
If you have any concern on this issue, please post here.

Regards,
Peter Huang

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Dean Slindee

Peter,
Thanks for the two lines of code. They worked just great. And I learned
something new as well.

Next time I will try to scroll *all* the way to the end of the method list
before throwing in the towel. I must have given up long before encountering
the 'S's, as in .SetItemCheckState.

Thanks again, very helpful.
Dean Slindee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top