Multiple buttons on a form can be updated by coding a procedure once, and using it over and over again. This can be done using called procedures and changing parameters each time the procedure is called/run:
In this case, the code has been placed in a form_load procedure, so it checks for booked rooms as the form opens. Any rooms which have already been booked are turned green and are disabled so that they cannot be clicked on.
The room bookings have been saved in a Bookings textfile:
This code sets up the parameters and calls the sub-procedure:
Here is the called procedure:
In this example, a tournament bracket is displayed, using a called procedure with multiple parameters passed to it. The code has been specifically written for a tournament bracket with 8 teams at the start, but the called procedure concept and parameter passing can be used for any scenario where you want to use the same code for different buttons, labels, etc.
The code needs to update different objects (labels) on the form, depending on the stage of the tournament (for an 8-player tournament, the stages would be qualifier(8 players), semi-final (4 players), final (2 players) and winner (1 player) )
The code needs to update different objects (labels) on the form, depending on the tournament stage and the game within each stage.
The first bit of code sets up the parameters (labels and stage) and calls the populatebracket procedure each time, to update each specific label, as follows:
Qualifiers Round
If stage = Qualifier Game 1, then the labels to be updated are Qualifier Team 1 and Qualifier Team 2
If stage = Qualifier Game 2, then the labels to be updated are Qualifier Team 3 and Qualifier Team 4
If stage = Qualifier Game 3, then the labels to be updated are Qualifier Team 5 and Qualifier Team 6
If stage = Qualifier Game 4, then the labels to be updated are Qualifier Team 7 and Qualifier Team 8
Semi Finals Round
If stage = Semi Final Game 1, then the labels to be updated are Semi Team 1 and Semi Team 2
If stage = Semi Final Game 2, then the labels to be updated are Semi Team 3 and Semi Team 4
Final Round
If stage = Final Game, then the labels to be updated are Final Team 1 and Final Team 2
Show Winner
If stage = Winner, then the labels to be updated are Winner
Private Sub btnrefresh_Click(........)
Dim lbl_player1 As New Label ‘each game will have 2 players, these labels are
Dim lbl_player2 As New Label ‘like global labels, needed for the code to work.
Dim stage As String
‘sets up parameters before the procedure is called
stage = "Qualifier1"
lbl_player1 = lbl_qual_team1 ‘lbl_qual_team1 is the label that will be updated
lbl_player2 = lbl_qual_team2 ‘lbl_qual_team2 is the label that will be updated
Call populatebracket(stage, lbl_player1, lbl_player2) ‘calls the procedure
stage = " Qualifier2"
lbl_player1 = lbl_qual_team3 ‘lbl_qual_team3 is the label that will be updated
lbl_player2 = lbl_qual_team4 ‘lbl_qual_team4 is the label that will be updated
Call populatebracket(stage, lbl_player1, lbl_player2)
stage = " Qualifier3"
lbl_player1 = lbl_qual_team5
lbl_player2 = lbl_qual_team6
Call populatebracket(stage, lbl_player1, lbl_player2)
stage = " Qualifier4"
lbl_player1 = lbl_qual_team7
lbl_player2 = lbl_qual_team8
Call populatebracket(stage, lbl_player1, lbl_player2)
cont ...
stage = "Semi-final1"
lbl_player1 = lbl_semi_team1
lbl_player2 = lbl_semi_team2
Call populatebracket(stage, lbl_player1, lbl_player2)
stage = "Semi-final2"
lbl_player1 = lbl_semi_team3
lbl_player2 = lbl_semi_team4
Call populatebracket(stage, lbl_player1, lbl_player2)
stage = "Final"
lbl_player1 = lbl_final_team1
lbl_player2 = lbl_final_team2
Call populatebracket(stage, lbl_player1, lbl_player2)
stage = "Winner"
lbl_player1 = lbl_winner
Call populatebracket(stage, lbl_player1, lbl_player2)
End Sub
Once you have set up a pair of labels, the populatebracket procedure is called. This code can be written only once, but called(used) as many times as needed for different labels:
This procedure is called using the code
Call populatebracket(stage, lbl_player1, lbl_player2),
(shown above in the calling procedure).
The stage parameter is passed ByVal (by value), but the labels (which change with each call), are passed ByRef (by reference), so that different labels can be updated each time.
Basically, this procedure will find the players' names from the textfile MemberFiles.txt and update the labels.
Note that the lbl_player1 label is not a real label, it is the label that was named when the parameter lbl_player1 was passed as the point this procedure was called.
There are many different ways to handle data (records) stored within files. The code on these pages gives you the opportunity to save, search, display and update data from textfiles (set up in Notepad), using a called procedure and password parameters.
This code allows items to be added to a cart, as they are selected for purchase. The items are set up as buttons which can be clicked on, to put the item in the cart.
When the form loads, the basket is empty
The top three games are clicked on, which causes them to be added to the basket. However we can write the code to do this only once, then call it each time a button is pressed. We put this code in a procedure, so that we do not have to write it for every button (film) on the form.
When the checkout button is pressed, code is execute to add a line to the Purchases textfile, for each film in the basket.
Code for Example 3:
UpdateBasket is the called procedure
When each button is clicked the name of the game is saved to a variable called GameName.
The subprocedure UpdateBasket is then called using the variable GameName.
The procedure UpdateBasket then uses the variable GameName to find the record in the file and add the details found to the list box. UpdateBasket is used for every button which saves writing the code out for each button.