Solved

drag list box

Posted on 2008-06-18
28
218 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
ID: 21820076
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
ID: 21820082
0
 

Author Comment

by:jagguy
ID: 21820592
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 53

Expert Comment

by:Dhaest
ID: 21820674
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
ID: 21820982
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
ID: 21821157
q1)'lb.ItemsSource = lb1  'this gave me an error when assigning a listboxitem to a listbox

 
0
 

Author Comment

by:jagguy
ID: 21821353
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
ID: 21821416
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
ID: 21821456
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
ID: 21821746
On what code does it crash ? What is the errormessage ?
0
 

Author Comment

by:jagguy
ID: 21821781
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
ID: 21821883
txt1.Text = listbox.SelectedItem.toString()
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21821894
Can also be ListBox.SelectedItem.Text
(or ListBox.SelectedItem.Value)
0
 

Author Comment

by:jagguy
ID: 21821974
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
 
LVL 53

Expert Comment

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

Author Comment

by:jagguy
ID: 21822086
 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
ID: 21822128
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
ID: 21822183
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
ID: 21822211
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
ID: 21822310
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
ID: 21822325
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
ID: 21822346
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
ID: 21828013
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
ID: 21828739
0
 

Author Comment

by:jagguy
ID: 21828908
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
ID: 21829499
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
ID: 21829788
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
ID: 21829817
It's all depending of your own requirements. I usually only use it with strings.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fso.FolderExists("\\server\HiddenFolder$") 4 64
I need help converting a bitmap to an image in VB.Net 1 32
Regex validation 2 24
.NET 2008 VB and C# 6 27
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

785 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