BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 3.5+

A Text-Based Box Generator

When working with console applications, or when adding to a log file, it is often useful to highlight elements of the text. One way in which to achieve this is to draw a box around the text, using standard symbols from the available character set.

BoxGenerator Class

When you are working with plain text, such as in a console application, you may want to draw attention to certain elements. One way in which to do so it to draw a box around the text. Of course, with plain text the options for drawing are limited. If you are using a typeface that has proportionally-spaced characters, it may be impossible to create a perfect box. With monospaced fonts, such as those usually used in console applications, you could use a single character to represent a border or you could use some of the characters of the selected font with different options for the horizontal, vertical and corner elements of the box.

In this article we'll create a simple BoxGenerator class, which will process an array of strings, each representing a single line of text. The strings will be combined into a single string that can be outputted or logged. This will contain the lines from the array surrounded by a box.

There are many ways in which you could draw a box using characters. We will permit three options, although you could add new box styles relatively easily. The three styles we will support are a single or double border, using eight common symbols, or a box made from a repeated, single character. The options are illustrated below:

┌──────┐
│Single│
└──────┘

╔══════╗
║Double║
╚══════╝

***********
*Character*
***********

NB: The first two styles above may appear to have broken borders when viewed in a web browser. The gaps in the border do not appear in a console application when using the default font.

Creating the Types

To begin, we need a class. We'll name it, "BoxGenerator". Create a new console application and add the class. Modify the code to make the class public, as shown below:

public class BoxGenerator
{
}

To make the code readable, it would be useful to include an enumeration that includes the border types. Add a new type named, "BoxStyle" and define the enumeration as follows:

public enum BoxStyle
{
    Single,
    Double,
    SingleCharacter
}

Border Style Definitions

There are many ways in which you could define the characters used for each border style. For example, you could set up character arrays or strings. For simplicity, we'll define three strings, each containing eight characters. The character position within the string will determine the position at which the characters will appear in the box. Let's match the order in which the characters will be used to their usage. This will give the following indexes:

  • 0. Top left.
  • 1. Top.
  • 2. Top right.
  • 3. Left.
  • 4. Right.
  • 5. Bottom left.
  • 6. Bottom
  • 7. Bottom right.

NB: The left and right borders, and the top and bottom borders, are likely to use the same character. However, by keeping them separate you will have the opportunity to create more interesting boxes with, for example, shadow effects.

We'll predefine three border strings. The first will include the border elements for a single border. The second will define a double-line border and the third a solid border, which will be the default when no style is specified. It will also be possible to provide any single character to use for the entire box. To define the strings, add the following three fields to the BoxGenerator class:

string _singleLine = "┌─┐││└─┘";
string _doubleLine = "╔═╗║║╚═╝";
string _defaultBorder = "████████";

When using the border styles we'll need somewhere to store the style that is in use. Add another field for this purpose:

string _border;

Magic Numbers

When we extract characters from the style strings, we'll need to reference the correct index for each position in the box. We could use integer literals, or magic numbers, in the code but this would lower the readability. A better idea is to define a set of constants. Add the following constants to the BoxGenerator class:

const int TL = 0;
const int T = 1;
const int TR = 2;
const int L = 3;
const int R = 4;
const int BL = 5;
const int B = 6;
const int BR = 7;
9 March 2014