Library code snippets

Doing without Control Arrays

The included zip file contains complete source code of a vb.net windows forms project that contains 2 command buttons and a textbox when you run it. Entering an amount of controls to create and clicking the create button adds the selected amount of command buttons and links their click event to the design time command button.

When you click any of the dynamically created buttons or the design time button the same event procedure checks to see which control was clicked and informs you.

The key parts to the source code are:-

1. The DesignTime button's click event

Private Sub cbDesignTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDesignTime.Click
   'get a reference to the sender onjects name
   Dim cName As String = CType(sender, System.Windows.Forms.Button).Name
   'check to see which control was clicked by name
   If cName = "cbDesignTime" Then
      MsgBox("You clicked the Button placed here in design time")
   Else
      Dim x As Integer
      For x = 0 To cbCount
        If cName = "RunTime" & x Then
           MsgBox("You clicked RunTime" & x & " button")
        End If
      Next
   End If
End Sub


CType is a function which casts the sender object into whatever object you need if possible, in this case a button and we can then reference the name of the clicked button and use standard code to check for each name.

2. The CreateControls button click code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim x As Integer
   'create controls for the amount in the textbox
   Try
       For x = 0 To CType(tbButtonAmount.Text, Integer) - 1
           Dim cb As New System.Windows.Forms.Button()
           cb.Size = New System.Drawing.Size(200, 30)
           cb.Location = New System.Drawing.Point(250, 40 + x * 40)
           cb.Name = "RunTime" & CStr(cbCount)
           cb.Text = "RunTime" & CStr(cbCount)
           Me.Controls.Add(cb)
           cbCount += 1
           'add the click event and point to existing click event
           AddHandler cb.Click, AddressOf cbDesignTime_Click
       Next
   Catch
       MsgBox("Please ensure you have entered a number of controls to create in the exbox!")
       tbButtonAmount.Select()
   End Try
End Sub


Again CType is used to convert the amount entered in the textbox to an integer and further on we convert the form level variable cbCount to a string for naming of the buttons.
The key from an event point of view is the AddHandler method. This allows us to dynamically apply the new control to an event handler.
Finally the whole thing is contained in a Try..Catch..End Try block in case no proper number is entered (most likely error), a good example of the new error trapping techniques in vb.net.
Happy Programming.

Comments

  1. 09 Jan 2009 at 05:58
    Thanks for the Code..it Really Helped me a lot.. Thanks and Regards, Jayaprakash
  2. 21 May 2008 at 16:31

    Hello,

     Have you get some solutions for your problem?

    I´m with similar problem.

    If you can send your solution, I will thanks so much.

    I need change the text of many textbox in a While or For, too.

     Best Regards.

    André

  3. 30 Aug 2007 at 08:05

    Could you tell me how to add menu strip with sub menus (drop down list) and name it using VB 2005? In VB 6 I could add those using a for next loop at run time and make the menu as control array in which I used a text file to give the captions.

    Is it possible to give name like mnu(1), mnu(2) . . in VB 2005 so that i can use a loop at run time?

  4. 18 Jul 2007 at 15:27
    Your code worked, it created the controls, and for buttons works very fine.

    But i have a problem with textboxes, for example i create 5 textboxes names Text1 Text2 Text3 Text4 Text5, how can i in a while or For to work with them ?



  5. 11 May 2005 at 00:41
    Hey I got a question:

    Say for example I have a text file with the following values in it:

    Red
    Green
    Blue

    I want to read in from that file every 10 seconds or so, and list all those
    values that are in the text file in context menu as menuitems...

    If I use the technique you offered (which is really nice) in a loop,
    I will just clear the list and then read in the values again...
    Then I will have a problem: new space will be constantly created
    which will give me a memory leak... (Because of the New commands,
    and AddHandler). How can I get rid of that memory leak?

    If there is any other way of implementing what I need, I would gladly
    appreciate hearing about it.

    Thanks in advance,
    Jon
  6. 17 Jan 2003 at 11:29

    Bless your cotton socks. Am new to VB.NET, aware of great possibilities but weighed down by the learning of new ways to do stuff. Building a menu screen of dynamically created buttons was worrying me until I saw from your beautifully succinct article that adding an event handler was just as easy as adding the control itself.


    Now, if you can find a way of replacing the DataLinks object so I can get a UDL interface straight in my program like I did in VB6, I'll be like a dog with 3 tails, not just the 2.

  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Doing without Control Arrays.

Leave a comment

Sign in or Join us (it's free).

Brian O'Connell Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web applicat...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback