Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

VB.NET Faces Off Against Classic VB : Page 4

VB.NET, the .NET Framework, and Visual Studio.NET together form a potent combination that can simplify your code and reduce errors; however, to truly supplant existing versions of VB as a RAD tool, Visual Studio needs Break-Edit-Continue.


advertisement

Create a control dynamically at run-time
You couldn't do this at all in classic VB prior to version 6, You could fake it by using a control array, hiding the first control to simulate control creation, but there was a major drawback—all the created controls were bound to the same event procedure code! To determine which control fired the event, you could write Select Case statements based on a control index number passed to the event.

Finally, VB6 added a slightly improved version. By declaring a control in advance you could "create" it at run time. But the control's events were still bound to pre-determined code. In the classic VB example below, I've used the much more prevalent control array method. Fortunately, VB.NET has none of classic VB's limitations. The first two examples are roughly equivalent—both create a control that uses the same handler as its model. The third example shows how easy it is to create a new control and bind it to any existing method that has a matching signature—for the button example shown, the Click event signature is a subroutine that has no parameters and no return value.




Classic VB:

   Private Sub cmdAddButton_Click(Index As Integer)
      Static counter As Integer
      counter = counter + 1
      If counter < 4 Then
         Load cmdAddButton(counter)
         With cmdAddButton(counter - 1)
            cmdAddButton(counter).Move _
               .Left + .Width + _
               (10 * Screen.TwipsPerPixelX), .Top
         End With
         cmdAddButton(counter).Visible = True
      End If
   End Sub



VB.NET (Version 1)

   Private Sub btnAddButton_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles btnAddButton.Click
   
      Static counter As Integer = 1
      Static lastButton As System.Windows.Forms.Button
      counter += 1
      If lastButton Is Nothing Then
         lastButton = btnAddButton
      End If
      If counter < 4 Then
         Dim b As New System.Windows.Forms.Button()
         With lastButton
            b.Width = .Width
            b.Text = .Text
            Controls.Add(b)
            b.Left = .Left + .Width + 10
            b.Top = .Top
            lastButton = b
         End With
      AddHandler b.Click, AddressOf _
         Me.btnAddButton_Click
      End If
   End Sub



VB.NET ( Version 2)



   Private Sub btnNewAdd_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles btnNewAdd.Click
   
      Dim b As New System.Windows.Forms.Button()
      With sender
         b.Width = .width
         b.Text = "Click Me!"
         Controls.Add(b)
         b.Left = .Left + .Width + 10
         b.Top = .Top
      End With
      AddHandler b.Click, AddressOf Me.newButtonClick
   End Sub
' This is the function called by the newly created button

   Private Sub newButtonClick(ByVal sender As _
      System.Object, ByVal e As System.EventArgs)
      MsgBox("A new button was clicked! The index of " _
         " this button is: " & Me.Controls.GetChildIndex _
         (sender))
   End Sub
A few final notes on this topic.

VB.NET doesn't support classic VB's control arrays directly. Adding controls takes more code, but also adds considerable flexibility. You're perfectly free to add controls to an array or collection, which you can then use to simulate the functionality of a control array.

A control's event procedures no longer have to start with the name of the control, for example, "button1_click;" instead, the procedure may have any name.

You can dynamically change the procedures bound to control events.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap