PDA

View Full Version : آموزش: ساخت لیست باکس در Console Application ?



kingtak
دوشنبه 14 اسفند 1391, 21:51 عصر
سلام دوستان داشتم وبگردی می کردم که به یه پست جالب تو سایتc-sharpcorner.com برخوردم گفتم شما هم ازش استفاده کنید.همونطور که میبینید یه لیست باکس ساده توی محیط کنسول رو نشون داده.
درسته شاید بگید کی میاد با وجود Wpf و..با کنسول یوزر اینترفیس درست کنه ولی خودمونیم خیلی لذت بخشه.:چشمک:


http://www.c-sharpcorner.com/UploadFile/b942f9/creating-a-console-listbox-in-C-Sharp/Images/List1.gif

کافیه یه پروژه Console Application ایجاد کنید و کد زیر رو جایگزین کد قبلی بکنید و بعد برنامه رو اجرا کنید.



using System;

class ConsoleListBox
{

static void Main()
{
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += new ConsoleCancelEventHandler(BreakHandler);
Console.Clear();
Console.CursorVisible = false;

string[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
WriteColorString("Choose Level using down and up arrow keys and press enter", 12, 20, ConsoleColor.Black, ConsoleColor.White);
int choice = ChooseListBoxItem(months, 34, 3, ConsoleColor.Blue, ConsoleColor.White);
// do something with choice
WriteColorString("You chose " + months[choice - 1] + ". Press any key to exit", 21, 22, ConsoleColor.Black, ConsoleColor.White);
Console.ReadKey();
CleanUp();
}
public static int ChooseListBoxItem(string[] items, int ucol, int urow, ConsoleColor back, ConsoleColor fore)
{
int numItems = items.Length;
int maxLength = items[0].Length;
for (int i = 1; i < numItems; i++)
{
if (items[i].Length > maxLength)
{
maxLength = items[i].Length;
}
}
int[] rightSpaces = new int[numItems];
for (int i = 0; i < numItems; i++)
{
rightSpaces[i] = maxLength - items[i].Length + 1;
}
int lcol = ucol + maxLength + 3;
int lrow = urow + numItems + 1;
DrawBox(ucol, urow, lcol, lrow, back, fore, true);
WriteColorString(" " + items[0] + new string(' ', rightSpaces[0]), ucol + 1, urow + 1, fore, back);
for (int i = 2; i <= numItems; i++)
{
WriteColorString(items[i - 1], ucol + 2, urow + i, back, fore);
}
ConsoleKeyInfo cki;
char key;
int choice = 1;

while (true)
{
cki = Console.ReadKey(true);
key = cki.KeyChar;
if (key == '\r') // enter
{
return choice;
}
else if (cki.Key == ConsoleKey.DownArrow)
{
WriteColorString(" " + items[choice - 1] + new string(' ', rightSpaces[choice - 1]), ucol + 1, urow + choice, back, fore);
if (choice < numItems)
{
choice++;
}
else
{
choice = 1;
}
WriteColorString(" " + items[choice - 1] + new string(' ', rightSpaces[choice - 1]), ucol + 1, urow + choice, fore, back);
}
else if (cki.Key == ConsoleKey.UpArrow)
{
WriteColorString(" " + items[choice - 1] + new string(' ', rightSpaces[choice - 1]), ucol + 1, urow + choice, back, fore);
if (choice > 1)
{
choice--;
}
else
{
choice = numItems;
}
WriteColorString(" " + items[choice - 1] + new string(' ', rightSpaces[choice - 1]), ucol + 1, urow + choice, fore, back);
}
}
}
public static void DrawBox(int ucol, int urow, int lcol, int lrow, ConsoleColor back, ConsoleColor fore, bool fill)
{
const char Horizontal = '\u2500';
const char Vertical = '\u2502';
const char UpperLeftCorner = '\u250c';
const char UpperRightCorner = '\u2510';
const char LowerLeftCorner = '\u2514';
const char LowerRightCorner = '\u2518';
string fillLine = fill ? new string(' ', lcol - ucol - 1) : "";
SetColors(back, fore);
// draw top edge
Console.SetCursorPosition(ucol, urow);
Console.Write(UpperLeftCorner);
for (int i = ucol + 1; i < lcol; i++)
{
Console.Write(Horizontal);
}
Console.Write(UpperRightCorner);
// draw sides
for (int i = urow + 1; i < lrow; i++)
{
Console.SetCursorPosition(ucol, i);
Console.Write(Vertical);
if (fill) Console.Write(fillLine);
Console.SetCursorPosition(lcol, i);
Console.Write(Vertical);
}
// draw bottom edge
Console.SetCursorPosition(ucol, lrow);
Console.Write(LowerLeftCorner);
for (int i = ucol + 1; i < lcol; i++)
{
Console.Write(Horizontal);
}
Console.Write(LowerRightCorner);
}
public static void WriteColorString(string s, int col, int row, ConsoleColor back, ConsoleColor fore)
{
SetColors(back, fore);
// write string
Console.SetCursorPosition(col, row);
Console.Write(s);
}
public static void SetColors(ConsoleColor back, ConsoleColor fore)
{
Console.BackgroundColor = back;
Console.ForegroundColor = fore;
}
public static void CleanUp()
{
Console.ResetColor();
Console.CursorVisible = true;
Console.Clear();
}
private static void BreakHandler(object sender, ConsoleCancelEventArgs args)
{
// exit gracefully if Control-C or Control-Break pressed
CleanUp();
}

}


برای اطلاعات بیستر به این لینک ها سر بزنید:
http://en.wikipedia.org/wiki/Box-drawing_characters
http://www.c-sharpcorner.com/uploadfile/scottlysle/colorful-console-mode-applications-in-C-Sharp/