How to draw a box, a rectangle in a C # application

I ask two related questions.

1-How can we put output (e.g., results and messages) inside a field in a C # console application.

2-How can we draw a rectangle in a C # console.thank u application for any sample tutorial or tip

+3
source share
3 answers

Assuming you just labeled a box of symbols, 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 them. You will need to work more on boundary conditions. If there is no space in the message, a word that is larger than the field, but this should be enough to get you started.

+4
source

If you want to write it yourself, you can use the extended ascii code to draw simple shapes in the console. Extended AScii table

0
source

Source: https://habr.com/ru/post/1767954/


All Articles