Results 1 to 10 of 10

Thread: [RESOLVED] VS 2012, How do I get the text contents from combobox, and put it into a ListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Resolved [RESOLVED] VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    As the title says, how do I get the text contents from a combobox, and put it into a List Box. Thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    If you want to get just the selected item:
    Code:
    Dim str As String = ComboBox1.SelectedItem.ToString
            ListBox1.Items.Add(str)
    If you want to get all the items:
    Code:
    For i As Integer = 0 To ComboBox1.Items.Count - 1
                Dim str As String = ComboBox1.Items(i).ToString
                ListBox1.Items.Add(str)
            Next
    Generally speaking we like to see an effort on your behalf first.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    Thanks for the reply, but I'm getting "An unhandled exception of type 'System.InvalidOperationException' occured in WindowsApplication1.exe"

    I am new to all this, so I do apologize.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    A couple of questions, and don't get mad if I ask them, these are legit questions:

    1. Do you have a listbox that's named "ListBox1"?
    2. Do you have a combobox that's named "ComboBox1"?
    3. What are you using to trigger that code I provided?


    Could you also post the control you're using to trigger the code that I provided including any event handlers? Like for example, if you're using a button the event handlers would look something like this:

    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.  
    3.     End Sub

    The Handles Button1.Click is mainly what I'm looking for.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    As I said I am new, but yeh I do have have a ListBox1 and ComboBox1

    I am using the form itself to trigger it:

    Code:
    Public Class Form1
        Dim Str As String = ComboBox1.SelectedItem.ToString
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            ListBox1.Items.Add(Str)
        End Sub
    End Class
    I also tried using a button, but again it didn't load and gave me the same error

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    Ah, ok. What you'd want to do is put it into the form_load event. You have 3 ways of doing so:

    1. Double-click anywhere on the form in the designer view
    2. In the designer view:
      • Click any where on the form
      • Click the events. That's the ligtning bolt next to the form's properties
      • Double-click the load event
    3. In the code view:
      • Click on the upper left combobox and choose "Form1 Events"
      • Click on the upper right combobox and choose "Load"


    Either way to decide to use you should have this:

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    2.  
    3.     End Sub

    The reason it isn't working right now is because there is no event that's triggering the code to perform.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    I tried what you just said, and still nothing. Below is an image of the error message, maybe this will help?!

    Name:  ScreenHunter_85 Oct. 31 16.35.jpg
Views: 331
Size:  40.3 KB

    After this code:


    Code:
    Public Class Form1
        Dim Str As String = ComboBox1.SelectedItem.ToString
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.Items.Add(Str)
        End Sub
    End Class

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    Ah, ok I see... I misread your post in post # 5. I would suggest still using the selectedindexchanged event(double click on the combobox), but it should look like this:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim str As String = ComboBox1.SelectedItem.ToString
            ListBox1.Items.Add(str)
        End Sub
    
    End Class
    The reason being is you want to set the variable str right before you add str to the listbox. Take a look at this MSDN article on variable levels.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    Brilliant, it works, I really appreciate your help!

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: VS 2012, How do I get the text contents from combobox, and put it into a ListBox

    No problem, be sure that you mark the thread resolved under thread tools(At the top of this page).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width