Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制台输出的自定义格式_C#_String_Console_Format_Alignment - Fatal编程技术网

C# 控制台输出的自定义格式

C# 控制台输出的自定义格式,c#,string,console,format,alignment,C#,String,Console,Format,Alignment,所以,我正在尝试在控制台中编写一个库,以便进行一些简单的格式设置,这样我就不必每次做项目时都这样做。在本文中,我想创建一个方法,将您输入的字符串放入一个框中 这是我的代码: public static void DrawBox(string message, char borderChar, ConsoleColor messageColor, ConsoleColor borderColor, int padTop, int padBottom) { for (i

所以,我正在尝试在控制台中编写一个库,以便进行一些简单的格式设置,这样我就不必每次做项目时都这样做。在本文中,我想创建一个方法,将您输入的字符串放入一个框中

这是我的代码:

 public static void DrawBox(string message, char borderChar, ConsoleColor messageColor, ConsoleColor borderColor, int padTop, int padBottom)
     {

        for (int i = 0; i < Console.WindowWidth; i++)
        {
            Console.ForegroundColor = borderColor;
            Console.Write(borderChar);
        }
        for (int i = 0; i < padTop; i++)
        {
            Console.Write(string.Format("{0,0}" + "{0," + (Console.WindowWidth - 1) + "}",borderChar));
        }

        Console.ForegroundColor = borderColor;
        Console.Write(string.Format("{0,0}", borderChar));

        Console.ForegroundColor = messageColor;
        Console.Write("{0," + ((Console.WindowWidth / 2) + message.Length / 2) + "}", message);

        Console.ForegroundColor = borderColor;
        Console.Write("{0," + (((Console.WindowWidth / 5) + message.Length / 5)) + "}", borderChar);

        for (int i = 0; i < padBottom; i++)
        {
            Console.WriteLine(string.Format("{0,0}" + "{0," + (Console.WindowWidth - 1) + "}", borderChar));
        }

        for (int i = 0; i < Console.WindowWidth; i++)
        {
            Console.ForegroundColor = borderColor;
            Console.Write(borderChar);
        }
    }

把中间的6条线换成这些应该做的把戏

Console.ForegroundColor = borderColor;
Console.Write("{0,-" + (Console.WindowWidth - message.Length) / 2 + "}", borderChar);

Console.ForegroundColor = messageColor;
Console.Write(message);

Console.ForegroundColor = borderColor;
Console.Write("{0," + ((Console.WindowWidth - message.Length) / 2 + message.Length % 2) + "}", borderChar);

我已经试过各种各样的东西了,但还没有修好。我们将不胜感激
Console.ForegroundColor = borderColor;
Console.Write("{0,-" + (Console.WindowWidth - message.Length) / 2 + "}", borderChar);

Console.ForegroundColor = messageColor;
Console.Write(message);

Console.ForegroundColor = borderColor;
Console.Write("{0," + ((Console.WindowWidth - message.Length) / 2 + message.Length % 2) + "}", borderChar);