.NET Console Applications, possible to create labels and regions?
Is it possible to have a C# console application output text to labels already drawn? I've seen some native win 32 console apps that can do this.
So onscreen the user sees:
Progress: 1% or Progress: 50% depending on when the label is updated (and the label progress stays in the same place, while only the value of the progress percentage gets updated.
Rather than the only way I know how to do it currently which is console.开发者_StackOverflow社区writeLine which would produce a seperate line for each Progress update.
EG:
Progress: 1%
Progress: 2%
Yes, you can do this.
You can use Console.SetCursorPosition to reposition 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
Have a look at Console.SetCursorPosition
Although I have already accepted the answer: here is a dynamic example for the next 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 template draw phase you can add dynamic elements in, depending on how many items are in your loop:
screenLocationsBasic.Add(new screenLocation(Console.CursorLeft , Console.CursorTop ));
Then during data rendering time, you can update just that location depending on which item you're dealing with :
Console.SetCursorPosition(screenLocationsBoth[pos].Left, screenLocationsBoth[pos].Top);
Then all you need to do is pass pos (position of item in your loop).
You can clear the window and redraw the screen each time, which should be fast enough to look like things have changed in place.
精彩评论