blah blah blah is here! blah blah » Close

up1down
link

Hi Everybody,
I am looking for ways to implements simple Qbasic features into C Sharp Console namely:

1. The use of blinking color in Console.
2. Delimit Clear Screen as from Row 5 Downward so as to leave the first five row of the Console Window for the Main Menu title and so on.
3. creating a key-driven main menu (up and down arrow) to move from menu no. 1 to menu no. 2 and to menu no 3 and vice versa. Like a background of white over a red/back text to show that this menu is highlighted and if I press ENTER, it is going to execute that option.

For the first solution through it was one of the simplest thing in Qbasic not to say even earlier basic, I am still looking.

For the second, I have created a method/function which I called clear_lower() in which I used for keyword to create 80 columns of " " multiply by 5 row. However, I will be delight to see an easiest method of doing so.

Thank you very much.
I really need answers to this as I am working on a very extensive marking system and this is but a pragmatic test to how it is going to work out.

Nicolas MARIE.

last answered one year ago

2 answers

up1down
link

Taking those questions in the same order:

1. Although the DOS console supports blinking colors, the Windows console does not. However, you can simulate them by using a timer to switch the color of a particular character 'on or off' at appropriate intervals.

2. The easiest way to do this is to create a method which writes blank lines to the console from the sixth row downwards. However, it sounds like your clear_lower() method is doing something like this already.

3. Here's a program I wrote a while back which shows how you can implement a simple listbox or menu for a console application :

using System; 

class ConsoleListBox
{

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

string[] levels = new string[10];

for (int i = 0; i < 10; i++)
{
levels[i] = "Level " + (i + 1).ToString();
}

WriteColorString("Choose Level using down and up arrow keys and press enter", 12, 20, ConsoleColor.Black, ConsoleColor.White);

int choice = ChooseListBoxItem(levels, 34, 5, ConsoleColor.Blue, ConsoleColor.White);
// do something with choice
WriteColorString("You chose " + levels[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();
}

}

Hi vulpes, I believe you sent me some powerful stuff. I will copy and run it on my computer back home. Of course, there are a number of items that I simply does not understand and I will need to get back over to you for some clarification and suggestion as to how to implement this to my program. I see you have a drawbox command that sound much like the @ ... to ... statement in dbase III and dbase IV. I have been looking for this for weeks in vain. Thank you and hope I can implement all this in my program. As to blinking color as being a continuous switch between two colors. How to implement this into the program when all my program seemed to be in a sequential mode. It would seemed that as soon as I move from the blinking method to another method or to the forthcoming program flow, it will just cease to blick ???? Still thousand thanks. Nicolas.

vulpes
17279

Basically, you need to call the timer on a background thread rather than your main thread. This is done automatically if you use the System.Threading.Timer which is suitable for console applications. I've added another answer to show how you can blink some text anywhere on the console using this approach.

up1down
link

Further to my above comment, here's the code. I've assumed a console with gray text on a black background:

using System;
using System.Threading;

class BlinkingColors
{
static void Main()
{
Console.Clear();

// print something at the top left hand corner
string text = "Hello World!";
Console.WriteLine(text);
Console.WriteLine("\nPress any key to end program");
Console.CursorVisible = false;

// Create a delegate to blink the color using the timer
TimerCallback timerDelegate = new TimerCallback(BlinkColor);

// create BlinkInfo structure
BlinkInfo bi = new BlinkInfo(0, 0, text);

// create a timer to blink the text every 500 milliseconds
Timer timer = new Timer(timerDelegate, bi, 500, 500);

// pause main thread until a key is pressed
Console.ReadKey(false);

// clean up
timer.Dispose(); // important to call this
Console.ForegroundColor = ConsoleColor.Gray;
Console.CursorVisible = true;
Console.Clear();
}

static void BlinkColor (object state)
{
BlinkInfo bi = (BlinkInfo)state;

if (Console.ForegroundColor == ConsoleColor.Gray)
{
Console.ForegroundColor = ConsoleColor.Black;
}
else
{
Console.ForegroundColor = ConsoleColor.Gray;
}
Console.SetCursorPosition(bi.Left, bi.Top);
Console.WriteLine(bi.Text);
}
}

struct BlinkInfo
{
public int Left;
public int Top;
public string Text;

public BlinkInfo(int left, int top, string text)
{
Left = left;
Top = top;
Text = text;
}
}

Feedback