Solved

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

Posted on 2006-07-18
8
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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