Solved

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

Posted on 2006-07-18
8
226 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now