You've got 200 buttons in your new program, and each one has to be initialized with a long series of code statements. Is there some easy way to organize this process? There is—you can pass the buttons to a procedure, and place the initialization code in that procedure.
Here's an example—we can set a button's caption by passing it to a Sub procedure named SetCaption like this:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click SetCaption(Button1) End Sub
In the SetCaption Sub procedure, I pass the button as an object of class Button, and I pass it by reference; passing it by reference makes it explicit that I want to be able to change the Button object:
And in the SetCaption Sub procedure, I can change the Text property of the button, like this:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click SetCaption(Button1) End Sub Private Sub SetCaption(ByRef TargetButton As Button) TargetButton.Text = "I've been clicked" End Sub
The result appears in Figure 6.12; when you click the command button, the SetCaption Sub procedure changes its caption, as you see in that figure.