Hi, I'm a rookie about C# and im starting developing some Basic Console
Aplications, but i was just wondering if there's a way to center a message
when using the "Console.WriteLine("");" command, lets say i want the message
to appear centered when running the program. Thank you so much! ;)
Marc Gravell - 02 Apr 2008 07:45 GMT
Not built in (AFAIK) - but you can just pad it manually:
static void Main()
{
WriteLineCentered("foo {0}", "bar");
}
static void WriteLineCentered(string format, params object[]
args)
{
string value = string.Format(format, args);
int padding = (Console.WindowWidth - value.Length) / 2;
if (padding > 0)
{
Console.Write(new string(' ',padding));
}
Console.WriteLine(value);
}
Ignacio Machin ( .NET/ C# MVP ) - 02 Apr 2008 16:11 GMT
On Apr 2, 2:04 am, Fernando Rios
<FernandoR...@discussions.microsoft.com> wrote:
> Hi, I'm a rookie about C# and im starting developing some Basic Console
> Aplications, but i was just wondering if there's a way to center a message
> when using the "Console.WriteLine("");" command, lets say i want the message
> to appear centered when running the program. Thank you so much! ;)
Hi,
Not really, you see int he console you can only isntruct to display a
text, but you have no control of how to do it. I'm not aware of any
library that allows you to control and format the console.