See

Thursday, November 1, 2007

Progressbar in console application

This is a program for progess bar in Console application.
We specially constrolling the postion of cursor .
plz don use Console.Writeline();
because it will go to next line.
using System;
namespace testcsharp
{
class Program
{
static void Main(string[] args)
{
ConsoleLineProgressBar (10, 100);

ConsoleLineProgressBar (20, 100);
ConsoleLineProgressBar (30, 100);
ConsoleLineProgressBar (40, 100);
ConsoleLineProgressBar (70, 100);
ConsoleLineProgressBar (100, 100);
}
private static void ConsoleLineProgressBar(int progress, int total)
{

Console.CursorLeft = 0;
Console.Write("[");
Console.CursorLeft = 42;
Console.Write("]");
Console.CursorLeft = 1;
float onechunk = 40.0f / total;


int position = 1;
for (int i = 0; i < onechunk * progress; i++)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.CursorLeft = position++;
Console.Write(" ");
}


for (int i = position; i < 41; i++)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.CursorLeft = position++;
Console.Write(" ");
}


Console.CursorLeft = 45;
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;

Console.Write(progress.ToString() + " Record has been Processed ");
}
}
}

1 comment:

Anonymous said...

hi ......
its working fine