Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-18
8
Medium Priority
?
236 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
ZeonFlash earned 750 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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