-
Oct 31st, 2012, 10:14 AM
#1
Thread Starter
New Member
[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
-
Oct 31st, 2012, 10:45 AM
#2
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.
-
Oct 31st, 2012, 11:01 AM
#3
Thread Starter
New Member
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.
-
Oct 31st, 2012, 11:16 AM
#4
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:
- Do you have a listbox that's named "ListBox1"?
- Do you have a combobox that's named "ComboBox1"?
- 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:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End Sub
The Handles Button1.Click is mainly what I'm looking for.
-
Oct 31st, 2012, 11:26 AM
#5
Thread Starter
New Member
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
-
Oct 31st, 2012, 11:32 AM
#6
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:
- Double-click anywhere on the form in the designer view
- 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
- 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:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
The reason it isn't working right now is because there is no event that's triggering the code to perform.
-
Oct 31st, 2012, 11:38 AM
#7
Thread Starter
New Member
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?!
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
-
Oct 31st, 2012, 12:36 PM
#8
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.
-
Oct 31st, 2012, 12:38 PM
#9
Thread Starter
New Member
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!
-
Oct 31st, 2012, 12:40 PM
#10
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|