Practical Learning Logo

Body Tag Formatter


In this application, we will create a dialog box equipped with various controls. The subject is to create a format for the HTML's body tag.

Practical Learning: Starting the Exercise

  1. Start a new Visual J# project as a Windows Application named BodyTag
  2. Design the form as follows:
    Control Name Text Other Properties
    GroupBox grpBodyAttributes Body Attributes  
    RadioButton rdoBackground Background Checked: True
    RadioButton rdoText Text  
    RadioButton rdoLink Link  
    RadioButton rdoActiveLink Active Link  
    RadioButton rdoVisitedLink Visited Link  
    TextBox txtBackground #000000  
    TextBox txtText #0000FF  
    TextBox txtLink #FF0000  
    TextBox txtActiveLink #008000  
    TextBox txtVisitedLink #800000  
    Panel pnlPreview   BackColor: White
    BorderStyle: Fixed3D
    VScrollBar scrRed   LargeChange: 1
    Maximum: 255
    Value: 255
    VScrollBar scrGreen   LargeChange: 1
    Maximum: 255
    Value: 255
    VScrollBar scrBlue   LargeChange: 1
    Maximum: 255
    Value: 255
    Panel pnlBody   BackColor: White
    BorderStyle: Fixed3D
    TextBox txtTextPreview Body tag formatter and colorizer ForeColor: Blue
    TextAlign: Center
    TextBox txtLinkPreview Sample text as link ForeColor: Red
    TextAlign: Center
    TextBox txtALinkPreview Current active link ForeColor: Green
    TextAlign: Center
    TextBox txtVLinkPreview This link has been visited ForeColor: Maroon
    TextAlign: Center
    GroupBox   Color Values  
    Label   Red:  
    TextBox txtRed 0  
    Label   Green:  
    TextBox txtGreen 0  
    Label   Blue:  
    TextBox txtBlue 0  
    Button btnClose Close  
    Button btnCopy Copy  
    TextBox txtResult <body bgcolor="#FFFFFF" text="#0000FF" link="#FF0000" alink="#008000" vlink="#800000">  
  3. In the class, declare the following private variables:
    	private System.ComponentModel.Container components = null;
    	private String HexBG, HexText, HexLink, HexALink, HexVLink;
    	public Form1()
    		// Required for Windows Form Designer support
    		// TODO: Add any constructor code after InitializeComponent call
  4. Right-click the form and click View Code.
  5. Just under the + Windows Form Designer generated code line, create the following method:
    private void ApplyColor()
    		// Retrieve the current hexadecimal colors from their Edit controls
    		HexBG    = this.txtBackground.get_Text();
    		HexText  = this.txtText.get_Text();
    		HexLink  = this.txtLink.get_Text();
    		HexALink = this.txtActiveLink.get_Text();
    		HexVLink = this.txtVisitedLink.get_Text();
    		// Get the integral position of each ScrollBar control
    		int iRed   = 255 - this.scrRed.get_Value();
    		int iGreen = 255 - this.scrGreen.get_Value();
    		int iBlue  = 255 - this.scrBlue.get_Value();
    		// Display the position of each ScrollBar
    		// in its corresponding Edit control
    		this.txtRed.set_Text(String.Format("{0}", (System.Int32)iRed));
    		this.txtGreen.set_Text(String.Format("{0}", (System.Int32)iGreen));
    		this.txtBlue.set_Text(String.Format("{0}", (System.Int32)iBlue));
    		// Change the color of the Preview panel
    		// according to the values of the ScrollBar positions
    		pnlPreview.set_BackColor(System.Drawing.Color.FromArgb(this.scrRed.get_Value(), this.scrGreen.get_Value(), this.scrBlue.get_Value()));
    		String FmtRed = String.Format("{0:X}", (System.Int32)(255 - scrRed.get_Value()));
    		if( FmtRed.get_Length() == 1 )
    			FmtRed = String.Concat("0", FmtRed);
    		String FmtGreen = String.Format("{0:X}", (System.Int32)(255 - scrGreen.get_Value()));
    		if( FmtGreen.get_Length() == 1 )
    			FmtGreen = String.Concat("0", FmtGreen);
    		String FmtBlue = String.Format("{0:X}", (System.Int32)(255 - scrBlue.get_Value()));
    		if( FmtBlue.get_Length() == 1 )
    			FmtBlue = String.Concat("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.get_Checked() == true )
    			String BG = "#";
    			BG = String.Concat(BG, FmtRed);
    			BG = String.Concat(BG, FmtGreen);
    			BG = String.Concat(BG, FmtBlue);
    			HexBG = txtBackground.get_Text();
    		else if( rdoText.get_Checked() == true )
    			String Txt = "#";
    			Txt = String.Concat(Txt, FmtRed);
    			Txt = String.Concat(Txt, FmtGreen);
    			Txt = String.Concat(Txt, FmtBlue);
    			txtTextPreview.set_ForeColor(System.Drawing.Color.FromArgb(scrRed.get_Value(), scrGreen.get_Value(), scrBlue.get_Value()));
    			HexText = txtText.get_Text();
    		else if( rdoLink.get_Checked() == true )
    			String TL = "#";
    			TL = String.Concat(TL, FmtRed);
    			TL = String.Concat(TL, FmtGreen);
    			TL = String.Concat(TL, FmtBlue);
    			txtLinkPreview.set_ForeColor(System.Drawing.Color.FromArgb(scrRed.get_Value(), scrGreen.get_Value(), scrBlue.get_Value()));
    			HexLink = txtLink.get_Text();
    		else if( rdoActiveLink.get_Checked() == true )
    			String AL = "#";
    			AL = String.Concat(AL, FmtRed);
    			AL = String.Concat(AL, FmtGreen);
    			AL = String.Concat(AL, FmtBlue);
    			txtALinkPreview.set_ForeColor(System.Drawing.Color.FromArgb(scrRed.get_Value(), scrGreen.get_Value(), scrBlue.get_Value()));
    			HexALink = txtActiveLink.get_Text();
    		else if( rdoVisitedLink.get_Checked() == true )
    			String VL = "#";
    			VL = String.Concat(VL, FmtRed);
    			VL = String.Concat(VL, FmtGreen);
    			VL = String.Concat(VL, FmtBlue);
    			txtVLinkPreview.set_ForeColor(System.Drawing.Color.FromArgb(scrRed.get_Value(), scrGreen.get_Value(), scrBlue.get_Value()));
    			HexVLink = txtVisitedLink.get_Text();
    		// Update the contents of the bottom Edit control
    		String BD = "<body bgcolor=\"";
    		BD = String.Concat(BD, HexBG);
    		BD = String.Concat(BD, "\" text=\"");
    		BD = String.Concat(BD, HexText);
    		BD = String.Concat(BD, "\" link=\"");
    		BD = String.Concat(BD, HexLink);
    		BD = String.Concat(BD, "\" alink=\"");
    		BD = String.Concat(BD, HexALink);
    		BD = String.Concat(BD, "\" vlink=\"");
    		BD = String.Concat(BD, HexVLink);
    		BD = String.Concat(BD, "\">");
  6. Double-click each scroll bar and implement them as follows:
    private void scrRed_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
    private void scrGreen_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
    private void scrBlue_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
  7. Test the application and return to Visual J#
  8. 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.
    Just after the closing of the ApplyColor() function, create the following function:
    private void ClickOption(System.Drawing.Color Clr, String Result)
    		// These variables will hold the red, green, and blue
    		// values of the passed color
    		int Red, Green, Blue;
    		// Colorize the Preview panel with the passed color
    		// Get the red value of the color of the Preview panel
    		Red = pnlPreview.get_BackColor().get_R();
    		// Get the green value of the color of the Preview panel
    		Green = pnlPreview.get_BackColor().get_G();
    		// Get the blue value of the color of the Preview panel
    		Blue = pnlPreview.get_BackColor().get_B();
    		// Now that we have the red, green, and blue values of the color,
    		// Update the scroll bars with the new values
    		// Update the red, green, and blue values
    		// of the Numeric Values group box
    		// Update the string that was passed using
    		// the retrieved red, green, and blue values
    		Result = String.Concat(Result, "#");
    		Result = String.Concat(Result, String.Format("{0:X}",(System.Int32)Red));
    		Result = String.Concat(Result, String.Format("{0:X}",(System.Int32)Green));
    		Result = String.Concat(Result, String.Format("{0:X}",(System.Int32)Blue));
  9. Double-click each radio button and implement it as follows:
    private void rdoBackground_CheckedChanged (Object sender, System.EventArgs e)
    		// Call the ClickOption() method to calculate
    		// the hexadecimal value of the color
    		ClickOption(pnlBody.get_BackColor(), txtBackground.get_Text());
    		HexBG = txtBackground.get_Text();
    	private void rdoText_CheckedChanged (Object sender, System.EventArgs e)
    		System.Drawing.Color BGColor = pnlBody.get_BackColor();
    		ClickOption(txtTextPreview.get_ForeColor(), txtText.get_Text());
    		HexText = txtText.get_Text();   
    	private void rdoLink_CheckedChanged (Object sender, System.EventArgs e)
    		System.Drawing.Color BGColor = pnlBody.get_BackColor();
    		ClickOption(txtLinkPreview.get_ForeColor(), txtLink.get_Text());
    		HexLink = txtLink.get_Text(); 
    	private void rdoActiveLink_CheckedChanged (Object sender, System.EventArgs e)
    		System.Drawing.Color BGColor = pnlBody.get_BackColor();
    		ClickOption(txtALinkPreview.get_ForeColor(), txtActiveLink.get_Text());
    		HexALink = txtActiveLink.get_Text();
    	private void rdoVisitedLink_CheckedChanged (Object sender, System.EventArgs e)
    		System.Drawing.Color BGColor = pnlBody.get_BackColor();
    		ClickOption(txtVLinkPreview.get_ForeColor(), txtVisitedLink.get_Text());
    		HexVLink = txtVisitedLink.get_Text();  
  10. Save and test the application then return to Visual J#
  11. Double-click the Copy button and implement it as follows:
    private void btnCopy_Click (Object sender, System.EventArgs e)
  12. Double-click the Close button and implement it as follows:
    private void btnClose_Click (Object sender, System.EventArgs e)
  13. Test the application



Home Copyright 2004-2014 FunctionX, Inc.