Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone please help.
I have 16 combo boxes which I want to use the same list of items.
When I add a new item, I want to add it to all combo boxes collections.
I tried using this code;
VB
Dim updateComboName As String
Dim updateCombo As ComboBox
For i = 1 To 16
     updateComboName = "Player" & i
     updateCombo = updateComboName
     updateCombo.Items.Add(curComboItem)
 Next
but the line "updateCombo = updateComboName" fails as the left side is a combo and the right a string. How do I set the updateCombo as a variable with a name of Player1 through to Player16 as I step through the for loop?
In addition when the item is added it is lost as soon as the application is closed.How do I ensure the new item is retained for future use?
Posted
Updated 1-Mar-15 7:29am
v3
Comments
Maciej Los 1-Mar-15 12:37pm    
You need to store items data somewhere...
Dave the Golfer 2-Mar-15 16:22pm    
Maciej Los

Thank you I will create a CSV file and save the items to that.

Dave

1 solution

Maybe just change your second line to the following?

VB
...
updateCombo = CType(Me.Controls.Find(updateComboName, True), ComboBox)
...
 
Share this answer
 
v3
Comments
Dave the Golfer 2-Mar-15 16:20pm    
Gideon
Tried the solution but got this error
'FindControl' is not declared. It may be inaccessible due to its protection level.

My code is now
If NewName = 1 Then
MsgBox("Adding Name")
For i = 1 To 16
updateComboName = "Player" & i
updateCombo = CType(FindControl(updateComboName), ComboBox)
updateCombo.Items.Add(curComboItem)
Next
End If
Should FindControl be related to the Form?

Dave
Gideon van Dyk 2-Mar-15 16:51pm    
The code I provided was for a web page, if your solution is a windows form, then try:

Me.Controls.Find(updateComboName, true)
Dave the Golfer 3-Mar-15 7:06am    
Gideon

My code is for a windows form.
I am an absolute beginner so please forgive me if I ask naive questions BUT
where do I put Me.Controls.Find(updateComboName, True)?
Tried various way but kept getting the code error:
Value of type '1-dimensional array of System.Windows.Forms.Control' cannot be converted to 'System.Windows.Forms.ComboBox'.

Dave
Gideon van Dyk 3-Mar-15 16:08pm    
Hi Dave, I updated the solution up to for a WinForms solution.

It basically replaces the FindControl function call.
Dave the Golfer 5-Mar-15 6:39am    
Gideon

Code is now
If NewName = 1 Then
MsgBox("Adding Name")
For i = 1 To 16
updateComboName = "Player" & i
updateCombo = CType(Me.Controls.Find(updateComboName, True), ComboBox)
updateCombo.Items.Add(curComboItem)
Next
End If

But I get this error
Value of type '1-dimensional array of System.Windows.Forms.Control' cannot be converted to 'System.Windows.Forms.ComboBox'.

Dave

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900