Solved

drag list box

Posted on 2008-06-18
28
216 Views
Last Modified: 2013-11-12
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.
0
Comment
Question by:jagguy
  • 14
  • 14
28 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
0
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Isn't that explained in the article ? You select one item in the box and you drop it in another ?
0
 

Author Comment

by:jagguy
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
q1)'lb.ItemsSource = lb1  'this gave me an error when assigning a listboxitem to a listbox

 
0
 

Author Comment

by:jagguy
Comment Utility
this inserts but when i select the item in a listbox i get an error
 lb.Items.Insert(0, lb1)
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
I can't follow what you are posting !
Can you clarify what you are trying and what the problem exactly is !
0
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
On what code does it crash ? What is the errormessage ?
0
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
txt1.Text = listbox.SelectedItem.toString()
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Can also be ListBox.SelectedItem.Text
(or ListBox.SelectedItem.Value)
0
 

Author Comment

by:jagguy
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Can you show the complete code how you fill your listbox. The declaration of your listboxitems....
0
 

Author Comment

by:jagguy
Comment Utility
 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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
0
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
 

Author Comment

by:jagguy
Comment Utility
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
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
Comment Utility
It's all depending of your own requirements. I usually only use it with strings.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now