Practical
Learning: Introducing Scroll Bar Controls
|
|
- Start a new Windows Application named HTMLBodyTag1
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type BodyTag.vb and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
GroupBox |
|
|
Body Attributes |
|
RadioButton |
|
rdoBackground |
Background |
Checked: True |
TextBox |
|
txtBackground |
#000000 |
|
RadioButton |
|
rdoText |
Text |
|
TextBox |
|
txtText |
#0000FF |
|
RadioButton |
|
rdoLink |
Link |
|
TextBox |
|
txtLink |
#FF0000 |
|
RadioButton |
|
rdoActiveLink |
Active Link |
|
TextBox |
|
txtActiveLink |
#008000 |
|
RadioButton |
|
rdoVisitedLink |
Visited Link |
|
TextBox |
|
txtVisitedLink |
#800000 |
|
Panel |
|
pnlPreview |
|
BackColor: White
BorderStyle: Fixed3D |
VScrollBar |
|
scrRed |
|
|
VScrollBar |
|
scrGreen |
|
|
VScrollBar |
|
scrBlue |
|
|
Label |
|
|
R |
|
Label |
|
|
G |
|
Label |
|
|
B |
|
Panel |
|
pnlBody |
|
BackColor: White
BorderStyle: Fixed3D |
TextBox |
|
txtTextPreview |
Body tag formatter and colorizer |
BorderStyle: None
ForeColor: Blue
TextAlign: Center |
TextBox |
|
txtLinkPreview |
Sample text as link |
BorderStyle: None
ForeColor: Red
TextAlign: Center |
TextBox |
|
txtALinkPreview |
Current active link |
BorderStyle: None
ForeColor: Green
TextAlign: Center |
TextBox |
|
txtVLinkPreview |
This link has been visited |
BorderStyle: None
ForeColor: Maroon
TextAlign: Center |
GroupBox |
|
|
Color's Values |
|
Label |
|
|
Red: |
|
TextBox |
|
txtRed |
0 |
|
Label |
|
|
Green: |
|
TextBox |
|
txtGreen |
0 |
|
Label |
|
|
Blue: |
|
TextBox |
|
txtBlue |
0 |
|
Button |
|
btnCopy |
Copy |
|
Button |
|
btnClose |
Close |
|
TextBox |
|
txtResult |
<body bgcolor="#FFFFFF"
text="#0000FF" link="#FF0000" alink="#008000"
vlink="#800000"> |
|
- On the form, click one of the scroll bars
- Press and hold Ctrl
- Click the other two scroll bars and release Ctrl
- In the Properties window, click Maximum, type 255, and press
Enter
- Click an unoccupied area of the form to make sure no control is selected
- On the form, click one of the scroll bars
- Press and hold Ctrl
- Click the other two scroll bars and release Ctrl
- In the Properties window, click LargeChange, type 1, and press
Enter
- Right-click the form and click View Code
- Change the file as follows:
Public Class BodyTag
Private HexBG As String
Private HexText As String
Private HexLink As String
Private HexALink As String
Private HexVLink As String
Private Sub ApplyColor()
' Retrieve the current hexadecimal colors from their Edit controls
HexBG = txtBackground.Text
HexText = txtText.Text
HexLink = txtLink.Text
HexALink = txtActiveLink.Text
HexVLink = txtVisitedLink.Text
' Get the integral position of each ScrollBar control
Dim strRed As String = CStr(255 - scrRed.Value)
Dim strGreen As String = CStr(255 - scrGreen.Value)
Dim strBlue As String = CStr(255 - scrBlue.Value)
' Display the position of each ScrollBar
' in its corresponding Edit control
txtRed.Text = strRed
txtGreen.Text = strGreen
txtBlue.Text = strBlue
' Change the color of the Preview panel
' according to the values of the ScrollBar positions
pnlPreview.BackColor = Color.FromArgb(255 - scrRed.Value,
255 - scrGreen.Value,
255 - scrBlue.Value)
Dim FmtRed As String = (255 - scrRed.Value).ToString("X")
If FmtRed.Length = 1 Then FmtRed = "0" & FmtRed
Dim FmtGreen As String = (255 - scrGreen.Value).ToString("X")
If FmtGreen.Length = 1 Then FmtGreen = "0" & FmtGreen
Dim FmtBlue As String = (255 - scrBlue.Value).ToString("X")
If FmtBlue.Length = 1 Then FmtBlue = "0" & FmtBlue
' Get the position of each ScrollBar control
' Create a hexadecimal color starting with #
' And display the color in the appropriate Edit control
If rdoBackground.Checked = True Then
Dim BG As String = "#"
BG = BG & FmtRed
BG = BG & FmtGreen
BG = BG & FmtBlue
txtBackground.Text = BG
pnlBody.BackColor = pnlPreview.BackColor
txtTextPreview.BackColor = pnlPreview.BackColor
txtLinkPreview.BackColor = pnlPreview.BackColor
txtALinkPreview.BackColor = pnlPreview.BackColor
txtVLinkPreview.BackColor = pnlPreview.BackColor
HexBG = txtBackground.Text
ElseIf rdoText.Checked = True Then
Dim Txt As String = "#"
Txt = Txt & FmtRed
Txt = Txt & FmtGreen
Txt = Txt & FmtBlue
txtText.Text = Txt
txtTextPreview.ForeColor = Color.FromArgb(255 - scrRed.Value,
255 - scrGreen.Value,
255 - scrBlue.Value)
HexText = txtText.Text
ElseIf rdoLink.Checked = True Then
Dim TL As String = "#"
TL = TL & FmtRed
TL = TL & FmtGreen
TL = TL & FmtBlue
txtLink.Text = TL
txtLinkPreview.ForeColor = Color.FromArgb(255 - scrRed.Value,
255 - scrGreen.Value,
255 - scrBlue.Value)
HexLink = txtLink.Text
ElseIf rdoActiveLink.Checked = True Then
Dim AL As String = "#"
AL = AL & FmtRed
AL = AL & FmtGreen
AL = AL & FmtBlue
txtActiveLink.Text = AL
txtALinkPreview.ForeColor = Color.FromArgb(255 - scrRed.Value,
255 - scrGreen.Value,
255 - scrBlue.Value)
HexALink = txtActiveLink.Text
ElseIf rdoVisitedLink.Checked = True Then
Dim VL As String = "#"
VL = VL & FmtRed
VL = VL & FmtGreen
VL = VL & FmtBlue
txtVisitedLink.Text = VL
txtVLinkPreview.ForeColor = Color.FromArgb(255 - scrRed.Value,
255 - scrGreen.Value,
255 - scrBlue.Value)
HexVLink = txtVisitedLink.Text
End If
' Update the contents of the bottom text box
Dim BD As String = "<body bgcolor="""
BD = BD & HexBG
BD = BD & """ text="""
BD = BD & HexText
BD = BD & """ link="""
BD = BD & HexLink
BD = BD & """ alink="""
BD = BD & HexALink
BD = BD & """ vlink="""
BD = BD & HexVLink
BD = BD & "">"
txtResult.Text = BD
End Sub
End Class
|
- In the Class Name combo box, select scrRed
- In the Method Name combo box, select Scroll and implement the event as
follows:
Private Sub scrRedScroll(ByVal sender As Object,
ByVal e As System.Windows.Forms.ScrollEventArgs)
Handles scrRed.Scroll
ApplyColor()
End Sub
|
- In the Class Name combo box, select scrGreen
- In the Method Name combo box, select Scroll and implement the event as
follows:
Private Sub scrGreenScroll(ByVal sender As Object,
ByVal e As System.Windows.Forms.ScrollEventArgs)
Handles scrGreen.Scroll
ApplyColor()
End Sub
|
- In the Class Name combo box, select scrBlue
- In the Method Name combo box, select Scroll and implement the event as
follows:
Private Sub scrBlueScroll(ByVal sender As Object,
ByVal e As System.Windows.Forms.ScrollEventArgs)
Handles scrBlue.Scroll
ApplyColor()
End Sub
|
- Execute the application to test the form
- Close the form and return to your programming environment
- When the user clicks a radio button from the Body Attributes group box,
we need to display its color on the Preview panel. When a particular button
is clicked, we will retrieve the color of its font from the Body text box,
translate that color into red, green, and blue values, and then use those
values to automatically update the scroll bars and the edit boxes. While we
are at it, we also need to update the corresponding text box in the Body
Attributes group box. Since this functionality will be used by all radio
buttons in the group, we will use a global function to which we can pass two
variables.
When the user clicks a particular radio button, that button is represented
by a text box in the lower-left Body section. We need to get the color of
that edit box and pass it to our function. Since the clicked radio button
has a corresponding text box in the Body Attributes group box, we need to
change/update that value with the hexadecimal value of the first argument.
Therefore, we will pass a string argument to our function.
In the Code Editor, just after the End Sub line of the scrBlueScroll event,
create the following procedure:
Private Sub ClickOption(ByVal Clr As Color,
ByVal Result As String)
' These variables will hold the red, green, and blue
' values of the passed color
Dim red As Integer
Dim green As Integer
Dim blue As Integer
' Colorize the Preview panel with the passed color
pnlPreview.BackColor = Clr
' Get the red value of the color of the Preview panel
red = 255 - pnlPreview.BackColor.R
' Get the green value of the color of the Preview panel
green = 255 - pnlPreview.BackColor.G
' Get the blue value of the color of the Preview panel
blue = 255 - pnlPreview.BackColor.B
' Now that we have the red, green, and blue values of the color,
' Update the scroll bars with the new values
scrRed.Value = red
scrGreen.Value = green
scrBlue.Value = blue
' Update the red, green, and blue values
' of the Numeric Values group box
txtRed.Text = red.ToString()
txtGreen.Text = green.ToString()
txtBlue.Text = blue.ToString()
' Update the string that was passed using
' the retrieved red, green, and blue values
Result = Result & "#"
Result = Result & red.ToString("X")
Result = Result & green.ToString("X")
Result = Result & blue.ToString("X")
End Sub
|
- In the Class Name combo box, select rdoBackground
- In the Method Name combo box, select CheckedChanged and implement the
event as follows:
Private Sub rdoBackgroundCheckedChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoBackground.CheckedChanged
' If the user clicks Background radio button
' set color of the panel to that of the radio button
Dim BGColor As Color = pnlBody.BackColor
pnlBody.BackColor = BGColor
' Call the ClickOption() method to calculate
' the hexadecimal value of the color
ClickOption(pnlBody.BackColor, txtBackground.Text)
HexBG = txtBackground.Text
End Sub
|
- In the Class Name combo box, select rdoText
- In the Method Name combo box, select CheckedChanged and implement the
event as follows:
Private Sub rdoTextCheckedChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoText.CheckedChanged
Dim BGColor As Color = pnlBody.BackColor
txtTextPreview.BackColor = BGColor
ClickOption(txtTextPreview.ForeColor, txtText.Text)
HexText = txtText.Text
End Sub
|
- In the Class Name combo box, select rdoLink
- In the Method Name combo box, select CheckedChanged and implement the
event as follows:
Private Sub rdoLinkCheckedChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoLink.CheckedChanged
Dim BGColor As Color = pnlBody.BackColor
txtLinkPreview.BackColor = BGColor
ClickOption(txtLinkPreview.ForeColor, txtLink.Text)
HexLink = txtLink.Text
End Sub
|
- In the Class Name combo box, select rdoActiveLink
- In the Method Name combo box, select CheckedChanged and implement the
event as follows:
Private Sub rdoActiveLinkCheckedChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoActiveLink.CheckedChanged
Dim BGColor As Color = pnlBody.BackColor
txtALinkPreview.BackColor = BGColor
ClickOption(txtALinkPreview.ForeColor, txtActiveLink.Text)
HexALink = txtActiveLink.Text
End Sub
|
- In the Class Name combo box, select rdoVisitedLink
- In the Method Name combo box, select CheckedChanged and implement the
event as follows:
Private Sub rdoVisitedLinkCheckedChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles rdoVisitedLink.CheckedChanged
Dim BGColor As Color = pnlBody.BackColor
txtVLinkPreview.BackColor = BGColor
ClickOption(txtVLinkPreview.ForeColor, txtVisitedLink.Text)
HexVLink = txtVisitedLink.Text
End Sub
|
- In the Class Name combo box, select btnCopy
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnCopyClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnCopy.Click
txtResult.SelectAll()
txtResult.Copy()
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub
|
- Execute the application
- Close the form
Download |
|