Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a rectangle in console application?

I need to draw a rectangle, with a number inside, in a C# console app and using extended ASCII. How do I go about it?

This is for a demo.

like image 425
user712923 Avatar asked May 15 '11 05:05

user712923


2 Answers

public class ConsoleRectangle
{
    private int hWidth;
    private int hHeight;
    private Point hLocation;
    private ConsoleColor hBorderColor;

    public ConsoleRectangle(int width, int height, Point location, ConsoleColor borderColor)
    {
        hWidth = width;
        hHeight = height;
        hLocation = location;
        hBorderColor = borderColor;
    }

    public Point Location
    {
        get { return hLocation; }
        set { hLocation = value; }
    }

    public int Width
    {
        get { return hWidth; }
        set { hWidth = value; }
    }

    public int Height
    {
        get { return hHeight; }
        set { hHeight = value; }
    }

    public ConsoleColor BorderColor
    {
        get { return hBorderColor; }
        set { hBorderColor = value; }
    }

    public void Draw()
    {
        string s = "╔";
        string space = "";
        string temp = "";
        for (int i = 0; i < Width; i++)
        {
            space += " ";
            s += "═";
        }

        for (int j = 0; j < Location.X ; j++)
            temp += " ";

        s += "╗" + "\n";

        for (int i = 0; i < Height; i++)
            s += temp + "║" + space + "║" + "\n";

        s += temp + "╚";
        for (int i = 0; i < Width; i++)
            s += "═";

        s += "╝" + "\n";

        Console.ForegroundColor = BorderColor;
        Console.CursorTop = hLocation.Y;
        Console.CursorLeft = hLocation.X;
        Console.Write(s);
        Console.ResetColor();
    }
}
like image 81
hashi Avatar answered Oct 04 '22 20:10

hashi


Like this?

This worked for me:

Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("┌─┐");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");

[EDIT]

Answer to the sub-question in the comment:

Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("  ┌─┐");
Console.WriteLine("  │1│");
Console.WriteLine("┌─┼─┘");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
like image 22
Alex Aza Avatar answered Oct 04 '22 20:10

Alex Aza