Friday 30 March, 2012

Many ways to skin a cat!

Skinned-Cat

Last lesson we looked at using If Statements and Checkboxes to control styles applied to fonts.  Once you successfully ran your program you would had found that there were issues with using the “CheckState.Checked” and “CheckState.Unchecked”.  This lesson we will look at two methods to solve those issues.

Remember that with programming there is more than one way to skin a cat… meaning that this isn’t the only solution to our problem.

1. Analyzing the Issue


With our previous solution we were using the “CheckedChanged” event to see if the “FontStyle” had already been set to Bold.  If it hadn’t we were then making it Bold.

Private Sub chkBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBold.CheckedChanged
        If txtDisplay.Font.Bold Then
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Regular)
        Else
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold)
        End If
    End Sub

The issue here is that multiple checkboxes could be selected at any one time which would which would result in confusion of what style is being added.

2. One at a Time


The first thing was want to do is isolate the selection.  This means if we have ‘chkBold’ selected we want to ensure that all of the other checkboxes are NOT selected.  We can achieve this with a very simple if statement:

 If chkBold.CheckState = CheckState.Checked Then
            chkItalic.CheckState = CheckState.Unchecked
            chkUnderline.CheckState = CheckState.Unchecked
 End If

Above you can see that “if” the chkBold.CheckState = CheckState.Checked “then” we want to sent all of the other checkboxes to unchecked.  In the final step in this solution we will apply the fontstyle.  The commands will be executed sequential, this means that we will set all of the other checkboxes to unchecked and then we will apply the fontstyle.

 If chkBold.CheckState = CheckState.Checked Then
            chkItalic.CheckState = CheckState.Unchecked
            chkUnderline.CheckState = CheckState.Unchecked
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold Or FontStyle.Italic)
 End If

3. Nested If Statements


An alternative solution is to add an additional control in the form of a button.  This button will check which checkbox is selected and apply the fontstyle as per the results.  For many of you this will be there first time you have ever used a Nested If Statement, basically all it does it allow you to check against multiple conditions i.e. If this then this… else… this… else… this… etc.

If chkBold.CheckState = CheckState.Checked Then
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold And FontStyle.Italic)
        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

As mentioned above we are checking which box is selected and apply the font style as is required.  If none of them are selected it will go back to the regular fontstyle.  I prefer this method because it will allow you to expand the program further which we will do in the next lesson.

The complete solution is below, and you can also download the complete project from here.

4. The Complete Solution


Public Class Form1

    Private Sub chkBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBold.CheckedChanged
        If chkBold.CheckState = CheckState.Checked Then
            chkItalic.CheckState = CheckState.Unchecked
            chkUnderline.CheckState = CheckState.Unchecked
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Bold Or FontStyle.Italic)
        End If
    End Sub

    Private Sub chkItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkItalic.CheckedChanged
        If chkItalic.CheckState = CheckState.Checked Then
            chkBold.CheckState = CheckState.Unchecked
            chkUnderline.CheckState = CheckState.Unchecked
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Italic)
        End If
    End Sub

    Private Sub chkUnderline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUnderline.CheckedChanged
        If chkUnderline.CheckState = CheckState.Checked Then
            chkBold.CheckState = CheckState.Unchecked
            chkItalic.CheckState = CheckState.Unchecked
            txtDisplay.Font = New System.Drawing.Font(txtDisplay.Font, FontStyle.Underline)
        End If
    End Sub

    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
End Class

 


Related Posts


One ‘Sub’ to rule them all!
One ‘Sub’ to rule them all!
Computer Science, Programming
Programming: Classic Controls
Programming: Classic Controls
Computer Science, Programming