Reputation: 189
I have sort of strange question.
I've created a form application, with menus, options, buttons etc. Also I've implemented possibility to turn on and off some options using arguments and launching application from Command Prompt. Now I would like to implement reaction to additional "help" argument, I want it to show information about all the possible arguments and some examples.
Is there way to show some output to console I am currently running from, without creating additional console? Or it would be just easier just to show new MessageBox with description of all the arguments?
Thank you!
Upvotes: 1
Views: 123
Reputation: 36
This is what I use to add the console to applications when I've needed it:
#region Console support
[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void AllocConsole();
[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void FreeConsole();
#endregion
When we need to turn it on, we can call AllocConsole()
, and likewise FreeConsole()
when we want to turn it back off.
As well, I created/use the following to write to the console with color:
/// <summary>
/// Writes to the Console. Does not terminate line, subsequent write is on right of same line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWrite(ConsoleColor color, string text)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(text);
Console.ForegroundColor = originalColor;
}
/// <summary>
/// Writes to the Console. Terminates line, subsequent write goes to new line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWriteLine(ConsoleColor color, string text)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ForegroundColor = originalColor;
}
Example usage:
#region User Check
Console.Write("User: {0} ... ", Environment.UserName);
if (validUser(Environment.UserName).Equals(false))
{
ColoredConsoleWrite(ConsoleColor.Red, "BAD!");
Console.WriteLine(" - Making like a tree, and getting out of here!");
Environment.Exit(0);
}
ColoredConsoleWrite(ConsoleColor.Green, "GOOD!"); Console.WriteLine(" - Continue on!");
#endregion
Valid user output with "GOOD!" being in Green text:
User: Chase.Ring ... GOOD! - Continue on!
Invalid user output with "BAD!" being in Red text:
User: Not.Chase.Ring ... BAD! - Making like a tree, and getting out of here!
Upvotes: 1
Reputation: 13931
If there is no important reason why you would use console - I would just use MessageBox.
Mixing console and windows forms is not good idea.
If you really have to do it - there is AttachConsole function in kernel32.dll
. You can use it like this:
Program.cs file:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Test
{
static class Program
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main()
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("This will show on console.");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Upvotes: 1