How to create command at runtime in V.Net?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How to create command at runtime in V.Net?

  1. #1
    Join Date
    May 2012
    Posts
    2

    How to create command at runtime in V.Net?

    Hello!

    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:

    Code:
    Private Sub PopulateCombo()
    
    ComboBox1.Items.Add("MyContent") ComboBox2.Items.Add("MyContent") ComboBox3.Items.Add("MyContent") '... ComboBox84.Items.Add("MyContent")
    End Sub
    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")
    For Counter = 1 To 84
    Command.ExecuteAsCommand 'Fantasy
    Next
    End Sub
    Get it?

    Thx for any help...

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: How to create command at runtime in V.Net?

    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.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    May 2012
    Posts
    2

    Re: How to create command at runtime in V.Net?

    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...

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: How to create command at runtime in V.Net?

    I think to use the controls collection you would want to do something more like
    Code:
    For Each CB in Me.Controls
       cb.items.add .....
    Next
    Still 84 combos doesn't make sense. 3 for each day of a month would require 93
    Would make more sense to have a calander and only 3 combos
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Apr 2012
    Posts
    29

    Re: How to create command at runtime in V.Net?

    You can use something like:-
    Code:
    Array.ForEach(Me.Controls.OfType(Of ComboBox).ToArray, Function(c As ComboBox) c.Items.Add("My Content"))
    I agree with DataMiser though, a design like this is not really the best way.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,092

    Re: How to create command at runtime in V.Net?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center