Advertisement
ToDiR0S

ProgressBars

Sep 21st, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class LoadingBar
  9. {
  10.     static void Main()
  11.     {
  12.         string stars = new string('*', short.MaxValue / 2);
  13.         string dash = new string('-', short.MaxValue / 2);
  14.  
  15.         int strSize = stars.Length;
  16.  
  17.         string[] product = new string[strSize];
  18.         for (int i = 0; i < strSize; i++)
  19.         {
  20.             product[i] = "" + stars[i] + dash[i];
  21.             //drawTextProgressBar(i, strSize);
  22.             DrawProgressBar(i, strSize, 30, '#');
  23.         }
  24.         Console.WriteLine();
  25.     }
  26.  
  27.     private static void DrawProgressBar(int complete, int maxVal, int barSize, char progressCharacter)
  28.     {
  29.         Console.CursorVisible = false;
  30.         int left = Console.CursorLeft;
  31.         decimal perc = (decimal)complete / (decimal)maxVal;
  32.         int chars = (int)Math.Floor(perc / ((decimal)1 / (decimal)barSize));
  33.         string p1 = String.Empty, p2 = String.Empty;
  34.  
  35.         for (int i = 0; i < chars; i++) p1 += progressCharacter;
  36.         for (int i = 0; i < barSize - chars; i++) p2 += progressCharacter;
  37.  
  38.         Console.ForegroundColor = ConsoleColor.Green;
  39.         Console.Write(p1);
  40.         Console.ForegroundColor = ConsoleColor.DarkGreen;
  41.         Console.Write(p2);
  42.  
  43.         Console.ResetColor();
  44.         Console.Write(" {0}%", (perc * 100).ToString("N2"));
  45.         Console.CursorLeft = left;
  46.     }
  47.  
  48.     static void drawTextProgressBar(int progress, int total)
  49.     {
  50.         //draw empty progress bar
  51.         Console.CursorLeft = 0;
  52.         Console.Write("["); //start
  53.         Console.CursorLeft = 31;
  54.         Console.Write("]"); //end
  55.         Console.CursorLeft = 1;
  56.         float onechunk = 30.0f; // total;
  57.  
  58.         //draw filled part
  59.         int position = 1;
  60.         for (int i = 0; i < onechunk * progress; i++)
  61.         {
  62.             Console.BackgroundColor = ConsoleColor.Green;
  63.             Console.CursorLeft = position++;
  64.             Console.Write(" ");
  65.         }
  66.  
  67.         //draw unfilled part
  68.         for (int i = position; i <= 31; i++)
  69.         {
  70.             Console.BackgroundColor = ConsoleColor.Black;
  71.             Console.CursorLeft = position++;
  72.             Console.Write(" ");
  73.         }
  74.  
  75.         //draw totals
  76.         Console.CursorLeft = 35;
  77.         Console.BackgroundColor = ConsoleColor.Black;
  78.         Console.Write(progress.ToString() + " of " + total.ToString() + "    "); //blanks at the end remove any excess
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement