Solved

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

Posted on 2006-07-18
8
225 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
Comment Utility
Are the items in your "menu" listbox bound to a datasource?  Or do you enter them in by hand?
0
 

Author Comment

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

Expert Comment

by:jake072
Comment Utility
   ' 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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dizzycat
Comment Utility
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
Comment Utility
Who's code?  My code works on a listbox that is not databound, try it.

Jake
0
 
LVL 17

Expert Comment

by:ZeonFlash
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

11 Experts available now in Live!

Get 1:1 Help Now