By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

copying combos

P: n/a
hi all

i should copy the items of a combo to n other combos.

it's not a cycle:
With cboColor1
.Items.Add("ffffff")
.Items.Add("cecece")
...
.SelectedIndex = 0
End With
and the list may be long enough.

is there a quick way to replicate the content of combo1 to others without
copying its code?

tia, z.

Jan 5 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Jan 5, 10:07 pm, "Zebra Code" <ZebraCode2...@gmail.comwrote:
hi all

i should copy the items of a combo to n other combos.

it's not a cycle:
With cboColor1
.Items.Add("ffffff")
.Items.Add("cecece")
...
.SelectedIndex = 0
End With
and the list may be long enough.

is there a quick way to replicate the content of combo1 to others without
copying its code?

tia, z.
Just right click combo control which is placed on your form, then
right click on it "copy" then "paste" into another form you desire.

Hope this helps.
Jan 5 '08 #2

P: n/a

"kimiraikkonen" <ki*************@gmail.comkirjoitti viestissä
news:ee**********************************@i12g2000 prf.googlegroups.com...
>
Just right click combo control which is placed on your form, then
right click on it "copy" then "paste" into another form you desire.

Hope this helps.
This can be done only is design mode and I don't think that this was the
original problem.

You might declare a function or sub that adds items to combos.

Public Sub AddItems (ByRef source as ComboBox)

' Combos where to copy items from source
Dim Combos() as ComboBox={combo2, combo3, ... }

For Each Combo as ComboBox In Combos
For Each Item as Object in source.Items

Combo.Items.Add(Item)

Next
Next

End Sub
-Teemu

Jan 5 '08 #3

P: n/a
On Sat, 5 Jan 2008 21:07:33 +0100, "Zebra Code"
<Ze***********@gmail.comwrote:
>hi all

i should copy the items of a combo to n other combos.

it's not a cycle:
With cboColor1
.Items.Add("ffffff")
.Items.Add("cecece")
...
.SelectedIndex = 0
End With
and the list may be long enough.

is there a quick way to replicate the content of combo1 to others without
copying its code?

tia, z.
Untested code:

Private Sub CopyCombo(src as ComboBox, dest as ComboBox)

dest.Items.Clear()
For indx As Integer = 0 To src.Items.Count - 1
dest.Items.Add(src.Items.Item(indx).ToString())
Next
desc.SelectedIndex = 0

End Sub

Another approach that I like better is to create an ArrayList with the
items in it, and then build a Sub that populates a ComboBox from an
ArrayList.

Or create an ArrayList and use it as the DataSource for all of the
comboboxes. Just be sure to use a new currency manager for each
combobox, otherwise when you change one all will change.
Jan 5 '08 #4

P: n/a

"Teemu" <ts*****@hotmail.com>
"kimiraikkonen" <ki*************@gmail.com>
>Just right click combo control which is placed on your form, then
right click on it "copy" then "paste" into another form you desire.
Hope this helps.

This can be done only is design mode and I don't think that this was the
original problem.
that's right. at designtime the combo is just empty. it gets loaded at
runtime. meantime by code, in future maybe by means of a textfile.
You might declare a function or sub that adds items to combos.
Public Sub AddItems (ByRef source as ComboBox)
' Combos where to copy items from source
Dim Combos() as ComboBox={combo2, combo3, ... }
For Each Combo as ComboBox In Combos
For Each Item as Object in source.Items
Combo.Items.Add(Item)
Next
Next
End Sub
i'm going to test it. it sounds just perfect.
thanks so much
ciao, z.

Jan 5 '08 #5

P: n/a

"Jack Jackson"
Untested code:

Private Sub CopyCombo(src as ComboBox, dest as ComboBox)

dest.Items.Clear()
For indx As Integer = 0 To src.Items.Count - 1
dest.Items.Add(src.Items.Item(indx).ToString())
Next
desc.SelectedIndex = 0

End Sub

Another approach that I like better is to create an ArrayList with the
items in it, and then build a Sub that populates a ComboBox from an
ArrayList.

Or create an ArrayList and use it as the DataSource for all of the
comboboxes. Just be sure to use a new currency manager for each
combobox, otherwise when you change one all will change.
thank you, really. i'm gonna check it.
ciao, z.
Jan 5 '08 #6

P: n/a
On Jan 5, 11:36 pm, "Zebra Code" <ZebraCode2...@gmail.comwrote:
"Teemu" <tsir...@hotmail.com>
"kimiraikkonen" <kimiraikkone...@gmail.com>
Just right click combo control which is placed on your form, then
right click on it "copy" then "paste" into another form you desire.
Hope this helps.
This can be done only is design mode and I don't think that this was the
original problem.

that's right. at designtime the combo is just empty. it gets loaded at
runtime. meantime by code, in future maybe by means of a textfile.
Yes, but if you run your project you will get same items on runtime
and also you can modify combobox in designer with whole properties of
your combobox clone.
Jan 5 '08 #7

P: n/a

"kimiraikkonen"
>that's right. at designtime the combo is just empty. it gets loaded at
runtime. meantime by code, in future maybe by means of a textfile.

Yes, but if you run your project you will get same items on runtime
and also you can modify combobox in designer with whole properties of
your combobox clone.
ok, thanks. i'm newby with vbnet and was hoping there ws a straight clone
method. i'll search for it.
ciao, ZC

Jan 5 '08 #8

P: n/a
Taking Jack's code a little bit else

\\\
dest.Items.Clear()
For Each srcItem As String In src.Items
dest.Items.Add(srcItem)
Next
///
While in version 2008 this goes like this too (strongly typed).
\\\
dest.Items.Clear()
For Each srcItem In src.Items
dest.Items.Add(srcItem)
Next
///

There is nothing wrong with the code from Jack by the way, just to show how
nice version 2008 is in this.

Cor
"Zebra Code" <Ze***********@gmail.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
hi all

i should copy the items of a combo to n other combos.

it's not a cycle:
With cboColor1
.Items.Add("ffffff")
.Items.Add("cecece")
...
.SelectedIndex = 0
End With
and the list may be long enough.

is there a quick way to replicate the content of combo1 to others without
copying its code?

tia, z.
Jan 6 '08 #9

P: n/a

"Cor Ligthert[MVP]"
Taking Jack's code a little bit else

\\\
dest.Items.Clear()
For Each srcItem As String In src.Items
dest.Items.Add(srcItem)
Next
///
While in version 2008 this goes like this too (strongly typed).
\\\
dest.Items.Clear()
For Each srcItem In src.Items
dest.Items.Add(srcItem)
Next
///

There is nothing wrong with the code from Jack by the way, just to show
how nice version 2008 is in this.

Cor
this group is a mine.
thank you.
ciao, ZC

Jan 6 '08 #10

P: n/a

"kimiraikkonen" <ki*************@gmail.comkirjoitti viestissä
news:34**********************************@t1g2000p ra.googlegroups.com...
On Jan 5, 11:36 pm, "Zebra Code" <ZebraCode2...@gmail.comwrote:
>"Teemu" <tsir...@hotmail.com>
"kimiraikkonen" <kimiraikkone...@gmail.com>
Just right click combo control which is placed on your form, then
right click on it "copy" then "paste" into another form you desire.
Hope this helps.
This can be done only is design mode and I don't think that this was
the
original problem.

that's right. at designtime the combo is just empty. it gets loaded at
runtime. meantime by code, in future maybe by means of a textfile.

Yes, but if you run your project you will get same items on runtime
and also you can modify combobox in designer with whole properties of
your combobox clone.
But this works only if items can be added in design mode and I think that
this can't be done in this case.

And besides if you copy and paste a control in design mode you'll get two
controls which have same properties at that time. But those two controls
aren't linked so if you change properties it'll affect only one control not
both. Also if you add an item to combobox when program is running it will be
added only to one combobox not to both.

-Teemu

Jan 6 '08 #11

P: n/a
just to tel that i tried another way, using an array of combos.
there's thing to tune and i dont know yet ho to set the font (i'm a newby
here), but it seems to work.

thank you all
ZC

Public Class Form1

Dim cboColor() As ComboBox, txtValue() As TextBox, txtLabel() As TextBox
Dim nValues As Integer ' how many arrayed controls are actually on the
form

....

Private Sub cboValues_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboValues.SelectedIndexChanged
Dim j As Integer
If nValues 0 Then ' 1<nvalues<5, but for frm_load (nvalues=0)
For j = nValues To 0 Step -1
Me.Controls.Remove(cboColor(j))
Next
End If
nValues = CInt(cboValues.Text) - 1
Dim maxVal As Integer = CInt(cboValues.Text) - 1
For j = 0 To maxVal
' colors
ReDim Preserve cboColor(j)
cboColor(j) = New ComboBox
With cboColor(j)
.DropDownStyle = ComboBoxStyle.DropDownList
'.Font = system.Drawing.Font "Courier New"
.Top = lblCboColor.Top
.Left = lblCboColor.Left + lblCboColor.Width + (j * 90) + (j
* 5)
.Width = 90
.Items.Add("ffffff :: white")
...
.Items.Add("000000 :: black")
.SelectedIndex = j
End With
Me.Controls.Add(cboColor(j))

Jan 10 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.