progress bar in winforms c# code example
Example: c# console progress bar
private static string Repeat(string str, int times) {
return string.Concat(Enumerable.Repeat(str, times));
}
private static void ProgressBar(int progress, int total, int chunks = 30, ConsoleColor completeColour = ConsoleColor.Green, ConsoleColor remainingColour = ConsoleColor.Gray, string symbol = "■", bool showPercent = true) {
Console.CursorLeft = 0;
Console.Write(" [");
Console.CursorLeft = chunks + 3;
Console.Write("]");
Console.CursorLeft = 3;
float chunk = (float) chunks / total;
double completeRaw = Math.Ceiling((double) chunk * progress);
int complete = (int) Math.Ceiling((double) chunk * progress);
int remaining = chunks - complete;
Console.ForegroundColor = completeColour;
Console.Write(Repeat(symbol, complete));
Console.ForegroundColor = remainingColour;
Console.Write(Repeat(symbol, remaining));
Console.CursorLeft = chunks + 4;
Console.ResetColor();
if (showPercent) {
int percent = (int) ((float) progress / (float) total * 100);
Console.Write($" {Repeat(" ", 3 - percent.ToString().Length)}{percent} %");
}
}