开发者

How to Draw Box,Rectangle in a C# Console application

开发者 https://www.devze.com 2023-01-18 21:47 出处:网络
I ask for 2 related questions. 1-How we can Put outputs(such as Results and Messages) inside a box in a c# console application.

I ask for 2 related questions.

1-How we can Put outputs(such as Results and Messages) inside a box in a c# console application.

2-How we can draw rectangle in a c# console application.thank u for any samp开发者_高级运维le tutorial or advice


Assuming you just meant a character box this will do it.

 private static void DrawABox( int x, int y, int width, int height,char Edge,string Message )
    {
        int LastIndex =0 ;
        Console.SetCursorPosition(x, y);
        for ( int h_i = 0; h_i <= height ; h_i++ )
        {
            if ( LastIndex != -1 )
            {
                int seaindex = (LastIndex + ( width - 1) );
                if(seaindex >= Message.Length -1 )
                    seaindex = Message.Length - 1;
                int newIndex = Message.LastIndexOf(' ',seaindex);
                if(newIndex == -1 )
                    newIndex = Message.Length - 1;
                string substr = Message.Substring(LastIndex, newIndex - LastIndex);
                LastIndex = newIndex;
                Console.SetCursorPosition(x + 1, y + h_i);
                Console.Write(substr);
            }
            for ( int w_i = 0; w_i <= width; w_i++ )
            {

                if ( h_i % height == 0 || w_i % width == 0 )
                {
                    Console.SetCursorPosition(x + w_i, y + h_i);
                    Console.Write(Edge);
                }


            }

        }

I edited the code to put a message in their. You will need to do more work on the boundary conditions. Ex no space in the message a word that is longer then the box but this should be enough to get you started.


There are curses bindings for C# (that might be a good start): http://curses-sharp.sourceforge.net/


If you want to write it by yourself, You can use extended ascii code to draw simple shapes in a console. Extended AScii Table

0

精彩评论

暂无评论...
验证码 换一张
取 消