drag list box

Hi,

q1)how do i drag 1 item from one listbox to another in silverlight ? or isnt this possible.
I know how to drag a control.
jagguyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
DhaestConnect With a Mentor Commented:
It's all depending of your own requirements. I usually only use it with strings.
0
 
DhaestCommented:
Here is a complete article about it: Drag and Drop in Silverlight
http://leeontech.wordpress.com/2008/04/11/drag-and-drop-in-silverlight/
0
 
DhaestCommented:
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jagguyAuthor Commented:
I know drag and drop and can do this with a control. I dont know how to select from within a control and drag a item in the list and then drop it onto another list.

This requires a bit more effort so I still need some ideas.
0
 
DhaestCommented:
Isn't that explained in the article ? You select one item in the box and you drop it in another ?
0
 
jagguyAuthor Commented:
I can add data to a listbox like this.

    lb.Items.Add("sss3")
        lb.Items.Add("sss2")
        lb.Items.Add("sss1")

 

q1)How do i use listboxItem in code behind  to  add to a listbox and why do we have this control anyway as it doesnt appear in vb.net forms.

 lb1.Content = "www"
        lb1.Content = "www"

 
0
 
jagguyAuthor Commented:
q1)'lb.ItemsSource = lb1  'this gave me an error when assigning a listboxitem to a listbox

 
0
 
jagguyAuthor Commented:
this inserts but when i select the item in a listbox i get an error
 lb.Items.Insert(0, lb1)
0
 
DhaestCommented:
I can't follow what you are posting !
Can you clarify what you are trying and what the problem exactly is !
0
 
jagguyAuthor Commented:
I have a listbox and i want to populate it with listboxitems.

To do this i use
listboxitem.Content = "www"
 listbox.Items.Insert(0, listboxitem)

When i run the program i see this appear in the listbox.

q1)I select the item "www" and the program crashes? why?

I need listbox items because i will do a drag and drop of these items
0
 
DhaestCommented:
On what code does it crash ? What is the errormessage ?
0
 
jagguyAuthor Commented:
the textbox displays the selected value .

  Private Sub listbox_change(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If Not listbox.SelectedItem Is Nothing Then

            txt1.Text = listbox.SelectedItem  'InvalidCast_FromTo  'error here
        End If


    End Sub
0
 
DhaestCommented:
txt1.Text = listbox.SelectedItem.toString()
0
 
DhaestCommented:
Can also be ListBox.SelectedItem.Text
(or ListBox.SelectedItem.Value)
0
 
jagguyAuthor Commented:
No it still doesnt work, there is a trick to this or a bug.

txt1.Text = listbox.SelectedItem.toString()

'will display     systems.windows.control.listboxitem
which has nothing to do with the value


ListBox.SelectedItem.Text  'doesnt exist
ListBox.SelectedItem.Value 'missing member error
0
 
DhaestCommented:
Can you show the complete code how you fill your listbox. The declaration of your listboxitems....
0
 
jagguyAuthor Commented:
 Dim lb As New ListBox
    Dim lb1 As New ListBoxItem
---
lb.Width = 100
        lb.Height = 150
         lb1.Content = "www"
        lb1.Content = "www"
        lb1.Content = "www"
        'lb.ItemsSource = lb1.Parent

        lb.Items.Insert(0, lb1)
        lb.Items.Add("sss3")
        lb.Items.Add("sss2")
        lb.Items.Add("sss1")

        ' lb.SelectedIndex = 1

        Canvas.SetLeft(lb, 20)
        Canvas.SetTop(lb, 400)
        canvas1.Children.Add(lb)
        AddHandler lb.SelectionChanged, AddressOf Me.lb_change

    Private Sub lb_change(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If Not lb.SelectedItem Is Nothing Then

            '   txt1.Text = lb.SelectedItem
            txt1.Text = lb.SelectedItem.ToString()
            '    txt1.Text = lb.SelectedItem.value
        End If


    End Sub
0
 
DhaestCommented:
1) Why do you perform this 3 times ? (lb1.Content = "www")
2) Is this one item or multiple items ?
lb.Items.Insert(0, lb1)
        lb.Items.Add("sss3")
        lb.Items.Add("sss2")
        lb.Items.Add("sss1")
3) Is your Listbox a multiselect or not ?
4) Windows or webbased ?
0
 
DhaestCommented:
Can you give the following code a try
Private Sub lb_change(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' check valid selected item 
    If lb.SelectedItem Is Nothing Then 
        Return 
    End If 
    
    ' store selecteditem (for use in MouseLeftButtonUp) 
    ' we can not close popup here, you will get an error 
    dim _lbi as ListBoxItem
    _lbi = DirectCast(lb.SelectedItem, ListBoxItem) 
    
    ' get the content of the selected item 
    Dim s As String = DirectCast((_lbi).Content, String) 
    
    ' put content in textbox 
    txt1.Text = s 
    
End Sub 

Open in new window

0
 
jagguyAuthor Commented:
1)i changed it to once and it has same problems
2)multiple items and 4 in total and they all display in the list and  i can select the items added but not inserted from a listboxItem
3)not multiselect but i havent changed the default
4)webbased silverlight
0
 
jagguyAuthor Commented:
OK I can select the listboxitem in the list but not the other items that were added directly
  lb.Items.Add("sss3")
        lb.Items.Add("sss2")
        lb.Items.Add("sss1")

Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'.

1)maybe you cant use both ways to insert data in listbox as you either use listboxitems or not.
0
 
jagguyAuthor Commented:
Yes i added this code with above results as i said
OK I can select the listboxitem in the list but not the other items that were added directly like
  lb.Items.Add("sss3") 'error when selecting
        lb.Items.Add("sss2") ''error when selecting
        lb.Items.Add("sss1")'error when selecting

Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'.

complete opposite from before

1)maybe you cant use both ways to insert data in listbox as you either use listboxitems or not.

   If lb.SelectedItem Is Nothing Then
        Return
    End If
   
    ' store selecteditem (for use in MouseLeftButtonUp)
    ' we can not close popup here, you will get an error
    dim _lbi as ListBoxItem
    _lbi = DirectCast(lb.SelectedItem, ListBoxItem)
   
    ' get the content of the selected item
    Dim s As String = DirectCast((_lbi).Content, String)
   
    ' put content in textbox
    txt1.Text = s
   
0
 
DhaestCommented:
The reason is that you add 2 different items in your listbox
You add a listboxitem:
lb.Items.Insert(0, lb1)
And you add strings
        lb.Items.Add("sss3")
        lb.Items.Add("sss2")
        lb.Items.Add("sss1")

Do you reallay want to load different items in one list ?
Private Sub lb_change(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' check valid selected item 
    If lb.SelectedItem Is Nothing Then 
        Return 
    End If 
Dim s As String 
    if typeof(lb.SelectedItem) = typeof(String) then
        s = lb.selectedItem
    else
    ' store selecteditem (for use in MouseLeftButtonUp) 
    ' we can not close popup here, you will get an error 
    dim _lbi as ListBoxItem
    _lbi = DirectCast(lb.SelectedItem, ListBoxItem) 
    
    ' get the content of the selected item 
    s = DirectCast((_lbi).Content, String) 
    end if
    ' put content in textbox 
    txt1.Text = s 
    
End Sub 

Open in new window

0
 
jagguyAuthor Commented:
Hi,
ok i was just testing the control but you ae right i have no need to use 2 items to input data.

 dim _lbi as ListBoxItem
    _lbi = DirectCast(lb.SelectedItem, ListBoxItem)
   
    ' get the content of the selected item
    Dim s As String = DirectCast((_lbi).Content, String)

can i ask another basic question as I am confused as to why you always need to use directcast with some datatypes.
q1) why didnt the tostring work to convert to text instead of direct cast?
q2)why do i need to use a direct cast on an object proerty as why couldnt i use SET
_lbi = DirectCast(lb.SelectedItem, ListBoxItem) ?
0
 
DhaestCommented:
0
 
jagguyAuthor Commented:
I am not talking about simple data type conversions eg doubleto  int etc

I am talking about setting controls or objects  to properties which require conversion. In vb.net you use Set command but now we are using a conversion in some cases
  Dim s As String = DirectCast((_lbi).Content, String)


here you coild have used SET to assign object to the property but we are using directCast
 dim _lbi as ListBoxItem
    _lbi = DirectCast(lb.SelectedItem, ListBoxItem)
0
 
DhaestCommented:
As far as I see it you'll need set to setting controls or objects  to properties.
Whenever you want to read those controls to get a value, we need to cast it back to the object. We don't need to set it, because it exist
So: set at creation, cast whenever you need something from the object
0
 
jagguyAuthor Commented:
ok i understand what you are saying.

q1)So why have a listboxItem type? Is this to just enter in any type of object into a listbox or can you use the listboxitem in other ways?

q2)going back to my original question I want to drag an item from a llstbox like you click on the listbox item and you see a textbox or just the item on the mouse, then you drag it over and release over the other listbox. The example I havent seen it working but i dont think it does that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.