Saturday 31 March, 2012
One ‘Sub’ to rule them all!

So far we have be using Sub Procedures which have been generated when we double click on a button and an event method is called. This lesson we are going to create our own “Custom” Sub which will help us make our code more efficient. If you haven’t already you need to download and run the previous project from the last lesson… here.
1. Remove the Button
We no longer require the button which executes our events when clicked. So you can delete this in the “Design Mode” and you will also need to remove the Sub within the code, which should look something like this…
Private Sub btnStyles_Click(sender As System.Object, e As System.EventArgs) Handles btnStyles.Click
If chkBold.CheckState = CheckState.Checked Then
txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold)
ElseIf chkItalic.CheckState = CheckState.Checked Then
txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Italic)
ElseIf chkUnderline.CheckState = CheckState.Checked Then
txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Underline)
Else
txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Regular)
End If
End Sub
2. Optimising our code
I have named my sub “ruleall( )” to demonstrate that you can all your subs any unique name you like (just like variables), but they should be unique and descriptive. This sub we are creating is going to rule all of our checkboxes hence ruleall(). In this section you need to delete all of the If Statements and just call the new sub-procedure which we will create next.
To call a sub procedure you simply write Call sub-procedureName()
Private Sub chkBold_CheckedChanged(ByVal sender As System.Object , ByVal e As System.EventArgs) Handles chkBold.CheckedChanged
Call ruleall()
End Sub
Private Sub chkItalic_CheckedChanged(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles chkItalic.CheckedChanged
Call ruleall()
End Sub
Private Sub chkUnderline_CheckedChanged(ByVal sender As System. Object, ByVal e As System.EventArgs ) Handles chkUnderline.CheckedChanged
Call ruleall()
End Sub
3. Creating one sub to ruleall()
Creating your own sub follows the same format as the subs used for the checkboxes, accept ours will be nice and simple with none of the stuff that goes along with event handling. Your sub uses the exact same code as before but as aforementioned we have amalgamated all of the if statements into a nested if statement.
Private Sub ruleall()
If chkBold.CheckState = CheckState .Checked Then
chkUnderline.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Bold)
ElseIf chkItalic.CheckState = CheckState .Checked Then
chkBold.CheckState = CheckState.Unchecked
chkUnderline.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Italic)
ElseIf chkUnderline.CheckState = CheckState .Checked Then
chkBold.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Underline)
Else
chkBold.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
chkUnderline.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Regular)
End If
End Sub
This code will also ensure that ONLY ONE checkbox and style can be applied at any one time.
4. Complete Project
Once again you can download the complete project from here and below is our nicely optimised and completed code
Public Class Form1
Private Sub chkBold_CheckedChanged(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles chkBold.CheckedChanged
Call ruleall()
End Sub
Private Sub chkItalic_CheckedChanged(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles chkItalic.CheckedChanged
Call ruleall()
End Sub
Private Sub chkUnderline_CheckedChanged(ByVal sender As System. Object, ByVal e As System.EventArgs ) Handles chkUnderline.CheckedChanged
Call ruleall()
End Sub
Private Sub ruleall()
If chkBold.CheckState = CheckState .Checked Then
chkUnderline.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Bold)
ElseIf chkItalic.CheckState = CheckState .Checked Then
chkBold.CheckState = CheckState.Unchecked
chkUnderline.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Italic)
ElseIf chkUnderline.CheckState = CheckState .Checked Then
chkBold.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Underline)
Else
chkBold.CheckState = CheckState.Unchecked
chkItalic.CheckState = CheckState.Unchecked
chkUnderline.CheckState = CheckState.Unchecked
txtDisplay.Font = New System.Drawing.Font (txtDisplay.Font, FontStyle.Regular)
End If
End Sub
End Class
4. Challenge Activity
You can apply multiple styles to the one checkbox by using the logical “Or” operator (I would had thought it would be more logical to use the AND operator… but go figure). With this in mind, use the code snippet below to modify your application to allow multiple combinations of checkboxes and thus multiple styles to be applied.
txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold Or FontStyle.Underline)
Remember that this is only one way to do it.. you may be able to find an easier or more efficient way to do it… if you do please feel free to post suggestions in the comments along with any other questions you may have relating to this task.







