In this example, we’ll learn how to use progressbar control in C# Console Application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Program { static void Main(string[] args) { ProgressBarCiz(2, 1, 100, 0, ConsoleColor.White); ProgressBarCiz(2, 5, 50, 1, ConsoleColor.Red); ProgressBarCiz(2, 10, 40, 2, ConsoleColor.Green); ProgressBarCiz(2, 15, 100, 3, ConsoleColor.Cyan); ProgressBarCiz(2, 20, 65, 4, ConsoleColor.Yellow); Console.Read(); } public static void ProgressBarCiz(int sol, int ust, int deger, int isaret, ConsoleColor color) { char[] symbol = new char[5] { '\u25A0', '\u2592', '\u2588', '\u2551', '\u2502' }; // https://www.bilisimogretmeni.com/ int maxBarSize = Console.BufferWidth - 1; int barSize = deger; decimal f = 1; if (barSize + sol > maxBarSize) { barSize = maxBarSize - (sol + 5); // first 5 character "%100 " f = (decimal)deger / (decimal)barSize; } Console.CursorVisible = false; Console.ForegroundColor = color; Console.SetCursorPosition(sol + 5, ust); // https://www.bilisimogretmeni.com/ for (int i = 0; i < barSize + 1; i++) { System.Threading.Thread.Sleep(10); Console.Write(symbol[isaret]); Console.SetCursorPosition(sol, ust); Console.Write("%" + (i * f).ToString("0,0")); Console.SetCursorPosition(sol + 5 + i, ust); } Console.ResetColor(); } |
Output: