I have a noob Q:
I'm trying to build a VB.Net command using variables at runtime.
For example, I must populate several (84) comboboxes with the same content and I must do it in several sub's. I could of course write:
But this seems just to cumbersome to be good programming. Instead I want to build a string, and then have Vb read the string as a command. The following code is just fantasy, but highlights what I'm trying to do:
Code:
Private Sub PopulateCombo()
Private Counter As Integer
Private Command As String = "ComboBox" + Counter +".Items.Add("MyContent")
Do you really need 84 combo boxes? That is a very large number and would be cumbersome for the user.
Why would you need to populate them from serveral subs? Typically I create a sub to populate my combo or combos then call that sub when needed. Placong code all over the place is poor design and causes many headaches when something needs to be changed.
To answer your question, no you can not build commands on the fly at run time.
Well, it's a training planning program, and the comboboxes are part of defining a month-long period with the possibility to add up to three activities for each day through a wizard-like form. Further the activities can only be predefined values from another part of the program. I realize there's probably some bad design involved here, but the results from the comboboxes will in turn manipulate a database, so I will somehow nevertheless need some dirty repetitive coding to complete the task, at least if there is no way, as you say, to cheat...
From another forum I came over this (Changed to fit my problem). It didn't work; as in, the loop never do anything, but might it not be a possible way to make code at runtime? Or I'm I just misunderstanding completely? It's the Me.Controls.Item() object I don't understand...
Code:
Private Sub PopulateCombo()
Dim CB As ComboBox
For Counter = 1 To 84
CB = Me.Controls.Item("ComboBox" & Counter)
CB.Items.Add("MyContent")
Next
End Sub
I could probably make the coding easier, but I would need to write the items.add for each combo at least one, but I'm looking to get away with less than that...
Very poor design. I know you probably heared this before, but 84 ComboBoxes! That is too much and will make your User Interface clunky and too cumbersome!
But, to add to the previous two posts... You'll have to use arrays as well tot populate your combo's this long, tedious and wrong way - because of your design. Each array would have to pertain to each combobox.
Give us more details about your program, especially this form in question - perhaps show a screenshot of your design, then we could help you improve on it
Bookmarks