How to draw a rectangle in console application?
48,356
Solution 1
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();
}
}
Solution 2
This is an extension method to String, which will draw a console box around a given string. Multi-line support included.
i.e.
string tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());
public static string DrawInConsoleBox(this string s)
{
string ulCorner = "╔";
string llCorner = "╚";
string urCorner = "╗";
string lrCorner = "╝";
string vertical = "║";
string horizontal = "═";
string[] lines = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int longest = 0;
foreach(string line in lines)
{
if (line.Length > longest)
longest = line.Length;
}
int width = longest + 2; // 1 space on each side
string h = string.Empty;
for (int i = 0; i < width; i++)
h += horizontal;
// box top
StringBuilder sb = new StringBuilder();
sb.AppendLine(ulCorner + h + urCorner);
// box contents
foreach (string line in lines)
{
double dblSpaces = (((double)width - (double)line.Length) / (double)2);
int iSpaces = Convert.ToInt32(dblSpaces);
if (dblSpaces > iSpaces) // not an even amount of chars
{
iSpaces += 1; // round up to next whole number
}
string beginSpacing = "";
string endSpacing = "";
for (int i = 0; i < iSpaces; i++)
{
beginSpacing += " ";
if (! (iSpaces > dblSpaces && i == iSpaces - 1)) // if there is an extra space somewhere, it should be in the beginning
{
endSpacing += " ";
}
}
// add the text line to the box
sb.AppendLine(vertical + beginSpacing + line + endSpacing + vertical);
}
// box bottom
sb.AppendLine(llCorner + h + lrCorner);
// the finished box
return sb.ToString();
}
Solution 3
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("└─┘");
Author by
user712923
Updated on September 24, 2021Comments
-
user712923 over 2 years
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.
-
MaddTheSane almost 13 yearspossible duplicate of Console.Write() - display extended ascii chars?
-
David Heffernan almost 13 yearsC# doesn't use ASCII, extended or otherwise.
-
-
user712923 almost 13 years@Alex thanks for your example.That will definetely do the trick.Correct if I am wrong but what you did is not using ascii is it?Also what does "Encoding.GetEncoding(866) does?
-
user712923 almost 13 years@Alex how did you draw the top of the rectangle?
-
Alex Aza almost 13 years@user712923 - Hold Alt key and type 218 on numeric keyboard. This will show ┌. Than you do the same with 196 and 191. Important - you need to use numeric keyboard (NumLock is On).
-
user712923 almost 13 years@Alex you have been very very helful .Can i ask you one last thing.Sorry.I have done the rectangle ,I need to draw another rectangle at the top right hand corner with a numnber inside.Any chance of help?thanks
-
user712923 almost 13 yearsHow do you use it,if i need to draw number inside the rectangle
-
Agi Hammerthief about 9 yearsIf you're drawing a box around the entire console area, decrease your width and height by two (because the first and last character in each dimension are not usable/available, being taken up by the corner characters).
-
Mike about 8 yearsis there any way to modify this so you can draw two rectangles (one inside the other) ? Because of the spaced the second rectangle overwrite the first rectangle left horizontal line
-
Alex K. over 5 yearsIf you want to copy/paste:
╔ ═ ╗ ╚ ╝
-
IC_ about 5 years@Alex, you forgot
║