How to fill one ComboBox from other ComboBox control?

  • Thread starter Sakharam Phapale
  • Start date
S

Sakharam Phapale

Hi All,



How to fill one ComboBox from other ComboBox control?



1) Only setting the reference does the trick but doesn't show items in
control. If you see in immediate window, it shows the item count correctly
in destination ComboBox control.



Me.ComboBox1 = objFrmMain.ComboBox1



Or



2)should I loop through the Items collection of source ComboBox control and
add destination control item?



For intCount = 0 to objFrmMain.ComboBox1.Items.Count-1

Me.ComboBox1 = objFrmMain.ComboBox1.Items.Item(intCount)

Loop





Any help will be appreciated.



Thanks and Regards

Sakharam Phapale
 
C

Cor Ligthert

Sakharam,

The first question is, are you using a datasource or are you filling the
items with objects?

After that answer from you, the answer to you is easy.

Cor

"Sakharam Phapale"> Hi All,
 
S

Sakharam Phapale

Hi Cor,

MainForm is MDI form and cmbSelectUser is member control of it.
I am filling it as follow.

Class MainForm

Public WithEvents cmbSelectUser as ComboBox

Private Sub FillUserComboBox()
Dim SmapiApp As New smapiApplication()
Dim SmapiUser As smapiUser
For Each SmapiUser In SmapiApp.smapiUsers
cmbSelectUser.Items.Add(SmapiUser.Name)
Next
End Sub

End Class

-----

I don't want to use above code again in child forms.
Same elements should be copied in cmbUser2 of child form.

Class ChildForm

Public WithEvents cmbUser2 as ComboBox
Private Sub FillUser()

' I want to write code here...
End Sub

End Class

Thanks and Regards
Sakharam Phapale
 
C

Cor Ligthert

Sakharam,

Have a look for this for that.

Dim myarray(ComboBox1.Items.Count - 1) As Object
ComboBox1.Items.CopyTo(myarray, 0)
ComboBox2.Items.AddRange(myarray)

Here I use the collection from the combobox1, however probably you can use
directly those smapiUsers in both comboboxes when you CopyTo them first to a
one dimensional array.

I hope this helps?

Cor
 
S

Sakharam Phapale

Thanks Cor,

But one more question.
If I use statement as follows

Me.cmbUser2 = objMainForm.cmbselectUser

I don't see any item in ComboBox.
But, If I query in Immediate window as
?cmbUser2.Items.Count
It gives me 10 which is correct.
Then why comboBox doesn't show any item when I try to dropdown it?

Regards,
Sakharam
 
C

Cor Ligthert

Sakharam,
Me.cmbUser2 = objMainForm.cmbselectUser
With this statement are you telling to use for all code for cmbUser2 the
complete combobox on form1 with all its behaviour (events etc) on form1.

I do not know what is the effect. However I don't think that it will work as
you wish.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top