.NET Console applications, can you create labels and regions?

Is it possible to output the text of a C # console application onto already applied labels? I've seen some native console apps that can do this.

Thus, on the screen, the user sees:

Progress: 1% or Progress: 50% depending on when the tag is updated (and the tag's progress stays in the same place, while only the percentage of progress is updated.

Rather than I know how to do it currently, this is console.writeLine, which will create a separate line for each Progress update.

EG:

Progress: 1%

Progress: 2%

+2


source to share


4 answers


Yes, you can do it.

You can use Console.SetCursorPosition to move the cursor after writing.



For example:

Console.WriteLine("Starting algorithm...");

int line = Console.CursorTop;
for (int i=0;i<100;++i)
{
    Console.SetCursorPosition(0,line);
    Console.Write("Progress is {0}%        ",i);  // Pad with spaces to make sure we cover old text
    Thread.Sleep(100);
}
Console.SetCursorPosition(0,line);    
Console.WriteLine("Algorithm Complete.       "); // Pad with spaces to make sure we cover old text

      

+9


source


Have a look at Console.SetCursorPosition



+1


source


Although I have already accepted the answer: here is a dynamic example for the following guy:

private static List<screenLocation> screenLocationsBasic = new List<screenLocation>();

public class screenLocation
{
    public int Left { get; set; }
    public int Top { get; set; }

    public screenLocation(int left, int top)
    {
        this.Left = left;
        this.Top = top; 
    }


}

      

Then, during the drawing phase of the template, you can add dynamic elements depending on the number of elements in your loop:

screenLocationsBasic.Add(new screenLocation(Console.CursorLeft , Console.CursorTop ));

      

Then, during data processing, you can only update that location depending on what subject you are dealing with:

Console.SetCursorPosition(screenLocationsBoth[pos].Left, screenLocationsBoth[pos].Top);

      

Then you only need to pass pos (the position of the element in your loop).

0


source


You can clear the window and redraw the screen each time, which should be fast enough to look like everything has changed.

-1


source







All Articles