0

I want to make a Button that -in click- must copy all items in ComboBox1 and put them into ComboBox2 located in another Form. I am sure that it is doable with looping through the ComboBox Items but, I have no idea how to do it . Any help will be appreciated.

Thanks and Regards

aybe
  • 15,516
  • 9
  • 57
  • 105
Amrouane
  • 143
  • 3
  • 16
  • no i didn't write any code, I created the new form from Project->Add new windows form .. that's all – Amrouane Aug 24 '16 at 21:18
  • 3
    You need to do your own research. This is very basic stuff, but SO is not a tutorial site or code factory. Also, please read [Ask] and also take the [Tour]. – Ňɏssa Pøngjǣrdenlarp Aug 24 '16 at 21:23
  • Possible duplicate of [Best way to copy a comboBox from form1 to form2](http://stackoverflow.com/questions/12369332/best-way-to-copy-a-combobox-from-form1-to-form2) – washcloth Aug 24 '16 at 21:24

3 Answers3

5

Try this sir

but 1st where your button -in click- located?

in my sample combobox1 and button1 is located at form1 then the other combo box is at the form 2.

maybe this is what you are looking for.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     For i = 0 To ComboBox1.Items.Count - 1
        form2.ComboBox2.Items.Add(ComboBox1.Items(i))
        'adding items of combobox1 to another combobox
     Next
End Sub

hope this will help you

2

You can do it in a single line by converting to an array:

form2.ComboBox2.Items.AddRange(ComboBox1.Items.Cast(Of String).ToArray())
PKanold
  • 139
  • 7
-1
Dim x(ComboBox1.Items.Count - 1) As Object
    ComboBox1.Items.CopyTo(x, 0)
    form2.ComboBox2.Items.AddRange(Array.ConvertAll(Of Object, String)(x, New Converter(Of Object, String)(AddressOf Convert.ToString)))
Nikhil
  • 1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 12 '22 at 00:17