There are lots of controls in the Toolbox under Common Controls, we're not going to implement all of them here because most of them are easy to use. But some of them are interesting.
The WebBrowser control makes it easy to add a document viewer to our application that can view HTML documents online, just like IE.
WebBrowser1.Navigate(TextBox1.Text)
Let's look at the code for event handler for the Show button of the previous example.
Syntax highlighting:
Visual Basic objects have methods that are called to make the object perform an action. This works almost the same way that we can write code to assign property values to dynamically change an object's characteristics.
We've done similar things but let's take a fresh look and see how we use methods.
TigerLabel.SetBounds(50, 50, 100, 150)
Public Class Form1 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click TigerLabel.Show() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TigerLabel.SetBounds(50, 50, 100, 150) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TigerLabel.Hide() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click TigerLabel.SendToBack() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click TigerLabel.BringToFront() End Sub End Class
When creating code for several properties or methods of an object it can be tedious to type the object name repeatedly. So, With block can specify all the values and even calls to methods.
Let's see what can be done with the label WithBlock in the picture below.
We type in properties and methods in the Form's Load event using a With block.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With TestingWithBlock .AutoSize = False .BackColor = Color.Cyan .Width = 50 .Height = 15 .SendToBack() End With End Sub
If we run it, we see the changes we made with With block.
When we double-click on a Form or Button to open the Code Editor, we see that each event-handler begins and ends like this:
Private Sub Button1_Click(..) ... End Sub
The Sub is short for subroutine and each event-handler subroutine is a Private method of that Form's class. We can create our own subroutine method from scratch so that we can call from other code.
Public Class Form1 Private Sub ClearForm() TextBox1.Text = "" TextBox2.Text = "" End Sub ... End Class
And we also need to the event-handling call when we click the Clear button.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.ClearForm() End Sub
Let's use the example Form of the previous section. We need to type in the following line of code to the Declarations sections.
Private Sub User(ByVal fname As String, ByVal lname As String) TextBox1.Text = fname TextBox2.Text = lname End Sub
Also, we need to call this subroutine when the User button is clicked:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.User("K","Hong"); End Sub
Then, run it and click the User button: