Solved

Moving only desired row from 1 listbox to another when clicked.

Posted on 2006-07-18
8
228 Views
Last Modified: 2011-09-20
Hi Experts

In VB.NET i have 2 listboxs,the first listbox has a menu ie:

       STARTERS

1. Crab Meat & Sweetcorn Soup
2. Chicken & Sweetcorn Soup
3. Chicken & Noodle Soup

When an item from the menu is selected ie: 1, 2 or 3 i want that item displayed on the second listbox
but only the items from the menu,if the word starters or the blank space below it are selected i want
a message to come up say "Please chose an item from the menu only".

Hope you can help
0
Comment
Question by:dizzycat
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17129844
Are the items in your "menu" listbox bound to a datasource?  Or do you enter them in by hand?
0
 

Author Comment

by:dizzycat
ID: 17129876
The items on the menu are bound to a textfile on my computer.
0
 
LVL 14

Expert Comment

by:jake072
ID: 17129892
   ' You could use the SelectedIndexChanged event to trap them selecting an item, and pass it to the other box (and remove if you like).
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim strSearch As String = "0123456789"

        If ListBox1.SelectedIndex > -1 Then
            If ListBox1.SelectedItem.ToString().IndexOfAny(strSearch.ToCharArray()) > -1 Then
                ' Add to the other Box.
                ListBox2.Items.Add(ListBox1.SelectedItem)
                ' Remove the item from the list.
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            End If
        End If

    End Sub

Let me know if you need more help,

Jake
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 17

Accepted Solution

by:
ZeonFlash earned 250 total points
ID: 17129948
If you have set the datasource on the menu listbox, then you'll need to use a DataRowView to view the items.  Start by putting a blank entry in your second listbox, then use the following:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim drv As DataRowView
        Dim strSelection As String

        If ListBox1.SelectedIndex < 0 Then Exit Sub

        drv = ListBox1.Items.Item(ListBox1.SelectedIndex)
        strSelection = drv.Item("name")

        If String.Compare(strSelection.Trim, "") = 0 Or String.Compare(strSelection.Trim, "starters", True) = 0 Then
            strSelection = "Please choose from the menu"
        End If

        ListBox2.Items.Item(0) = strSelection
    End Sub
0
 

Author Comment

by:dizzycat
ID: 17130097
I am getting this error message when i use the above code.

************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
   at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
   at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
   at System.Windows.Forms.ListBox.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
0
 
LVL 14

Expert Comment

by:jake072
ID: 17130144
Who's code?  My code works on a listbox that is not databound, try it.

Jake
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17130159
That error occurred because you don't have your listbox bound to a datasource.  Try this instead:

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim strSelection As String

        If ListBox1.SelectedIndex < 0 Then Exit Sub

        strSelection = ListBox1.Items.Item(ListBox1.SelectedIndex)

        If String.Compare(strSelection.Trim, "") = 0 Or String.Compare(strSelection.Trim, "starters", True) = 0 Then
            strSelection = "Please choose from the menu"
        End If

        ListBox2.Items.Item(0) = strSelection
    End Sub
0
 
LVL 14

Expert Comment

by:jake072
ID: 17130178
My code can be reused with any non-bound list with any items in it, not just starters...  (Assuming you will have a number in the items that can be chosen.)

Jake
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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