简体   繁体   中英

C# - How to prevent cursor from jumping to next row when the current row fills up

I'm trying to create a custom sized border depending on the size of the console window. The problem is when it draws the last line, the cursor jumps down to the next row, which in turn moves the topmost row offscreen.

The buffersize of the console is the same as the consolesize to remove the scrollbars, which makes this possible.

边界到目前为止没有最后一排 完成与最后一行的边框,创建新行

The right one is the complete border, which creates an extra space because it fills the whole row. The left one is just before doing the last row, which does not have any problems (because it has not drawn the last one).

My current code is that of:

static void DrawFrame(string[,] frame)
    {
        string[] array = new string[frame.GetLength(1)];
        string output;
        for (int y = 0; y < frame.GetLength(1); y++)
        {
            output = "";
            Console.SetCursorPosition(0, y);
            for (int x = 0; x < frame.GetLength(0); x++)
            {
                output += frame[x, y];

            }
            array[y] = output;
            Console.Write(output);
        }
    }

The string variable frame has the value of:

╔══════════════════════╗
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
║                      ║
╚══════════════════════╝

The intended behaviour is to not create an extra row when doing the last row, which so far i haven't found any solution for.

Any help is appreciated!

PS: Console.CursorVisible = false; is not a solution.

PS2: The code for creating the border is as follows:

static string[,] SetBorder(string[,] frame)
    {
        for (int y = 0; y < frame.GetLength(1); y++)
        {
            for (int x = 0; x < frame.GetLength(0); x++)
            {
                if (y == 0)
                {
                    if (x == 0)
                    {
                        frame[x, y] = "╔";
                    }
                    else if(x == frame.GetLength(0) - 1)
                    {
                        frame[x, y] = "╗";
                    }
                    else
                    {
                        frame[x, y] = "═";
                    }
                }
                else if (y == frame.GetLength(1) - 1)
                {
                    if (x == 0)
                    {
                        frame[x, y] = "╚";
                    }
                    else if (x == frame.GetLength(0) - 1)
                    {
                        frame[x, y] = "╝";
                    }
                    else
                    {
                        frame[x, y] = "═";
                    }
                }
                else
                {
                    if (x == 0 || x == frame.GetLength(0) - 1)
                    {
                        frame[x, y] = "║";
                    }
                    else
                    {
                        frame[x, y] = " ";
                    }
                }
            }
        }
        return frame;
    }

The console size is also defined in the beginning as the size of frames dimensions.

Edit1: For those trying to debug my code, I'll link it here .

You can use Console.SetCursorPosition at the end of your code:

Console.SetCursorPosition(0, 0);

Keep in mind, since you said that the buffer size is equal to the console size. This can still be done if you used SetCursorPosition before setting the buffer size.

I used it this way and it worked:

DrawFrame(SetBorder(new string[Console.WindowWidth, Console.WindowHeight]));
Console.SetCursorPosition(0, 0);
Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight);

Console.ReadKey();

Output:

控制台Windows输出

UPDATE:

I will assume you're running the code on Windows 10. Why? When I first posted my answer here, I was at work, running the code on a Windows 7 machine, it gave the output that I have posted above a few hours ago.

Now that I am home, on a Windows 10 machine, and checking the weird scenario you're telling me about, I tested the same code I had in work here, and guess what, I had the same malfunction that you talked about.

To fix that, I changed the SetBufferSize function to this:

Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight+1);

I added the +1 to the height. Yes, it did work. However, it kept the scroll bar showing. I'll take a deeper look to see why the difference between the Win7 and Win10, and how to fix it without the scroll bar being seen.

According to this long explanation , a lot has changed in the console between Windows 7 and Windows 10. I am not that expert, but I can safely conclude that all those changes have a direct effect on the difference we had between the output of the code running on Win7 and running on Win10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM