Console Color: more than 1 in same line?

Dan

Using:

public static void ColoredConsoleWrite(ConsoleColor color, string text)
    {
    ConsoleColor originalColor = Console.ForegroundColor;
    Console.ForegroundColor = color;
    Console.Write(text);
    Console.ForegroundColor = originalColor;
}

and:

ColoredConsoleWrite(ConsoleColor.Blue, $"My favorite fruit: Apple");

Is there a way to display the Apple in Red while keeping My favorite fruit: Blue?

Jim Mischel

Yes, it's possible to write text in different colors on the same line. But you have to change the foreground color for each different color. That is, if you want to write "My favorite fruit: " in blue, and "Apple" in red, then you have to do two Write operations:

var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("My farorite fruit: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Apple");
Console.ForegroundColor = originalColor;

If you want to do that with a single call, then you need some way to define that in a string. The .NET Framework does not provide such a facility. It's possible to build such a thing. It would involve writing a string parser similar to what's used by String.Format, where you define placeholders for which you supply values as parameters.

Perhaps a simpler way to do it is to write a method that takes a list of color and string pairs, something like:

public class ColoredString
{
    public ConsoleColor Color;
    public String Text;

    public ColoredString(ConsoleColor color, string text)
    {
        Color = color;
        Text = text;
    }
}

public static void WriteConsoleColor(params ColoredString[] strings)
{
    var originalColor = Console.ForegroundColor;
    foreach (var str in strings)
    {
        Console.ForegroundColor = str.Color;
        Console.Write(str.Text);
    }
    Console.ForegroundColor = originalColor;
}

public void DoIt()
{
    WriteConsoleColor(
        new ColoredString(ConsoleColor.Blue, "My favorite fruit: "),
        new ColoredString(ConsoleColor.Red, "Apple")
    );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ValueError: need more than 1 value to unpack, split a line

From Dev

Django admin. list more than 1 model together in same listing

From Dev

Can I avoid specifying the same color value more than once in colors.xml?

From Dev

Specifying "AND" Condition with more than 1 time for same column in MYSQL

From Dev

Android: text button disordered and cut when more than 1 line

From Dev

More than 1 directory Redirect

From Dev

Using more than 1 function in-line from class

From Dev

Sending more than 1 message to TCP server in same connection

From Dev

dc.js stacked line chart with more than 1 dimension

From Dev

Validating more than 1 input

From Dev

Shared Preferences save more than 1 value at the same time

From Dev

Setting color of the defaultMarker by using more than "hue"

From Dev

How should I detect more than one occurrence of a pixel with the same color

From Dev

Specifying "AND" Condition with more than 1 time for same column in MYSQL

From Dev

localstorage more than 1 button

From Dev

Cant execute more than 1 line in my C++ code

From Dev

How do I write code of more than 1 line in the Python interpreter?

From Dev

More than 1 UIPickerView Swift

From Dev

How to animate more than 1 item from the same div in jQuery?

From Dev

using more than one linestyle in the same trend line with matplotlib

From Dev

End pipe when program returns more than 1 line

From Dev

Order by with more than 1 of lenght

From Dev

dc.js stacked line chart with more than 1 dimension

From Dev

Is it a palindrome in more than 1 base?

From Dev

Is it possible to change text color and background color in the console to different colors while still on the same line? C++

From Dev

Textview2 is not showm when TextView1 have more than one line

From Dev

SearchView for more than 1 recyclerview

From Dev

more than 1 HttpPost is activating

From Dev

extract specific string if another string IN SAME LINE appears more than once

Related Related

  1. 1

    ValueError: need more than 1 value to unpack, split a line

  2. 2

    Django admin. list more than 1 model together in same listing

  3. 3

    Can I avoid specifying the same color value more than once in colors.xml?

  4. 4

    Specifying "AND" Condition with more than 1 time for same column in MYSQL

  5. 5

    Android: text button disordered and cut when more than 1 line

  6. 6

    More than 1 directory Redirect

  7. 7

    Using more than 1 function in-line from class

  8. 8

    Sending more than 1 message to TCP server in same connection

  9. 9

    dc.js stacked line chart with more than 1 dimension

  10. 10

    Validating more than 1 input

  11. 11

    Shared Preferences save more than 1 value at the same time

  12. 12

    Setting color of the defaultMarker by using more than "hue"

  13. 13

    How should I detect more than one occurrence of a pixel with the same color

  14. 14

    Specifying "AND" Condition with more than 1 time for same column in MYSQL

  15. 15

    localstorage more than 1 button

  16. 16

    Cant execute more than 1 line in my C++ code

  17. 17

    How do I write code of more than 1 line in the Python interpreter?

  18. 18

    More than 1 UIPickerView Swift

  19. 19

    How to animate more than 1 item from the same div in jQuery?

  20. 20

    using more than one linestyle in the same trend line with matplotlib

  21. 21

    End pipe when program returns more than 1 line

  22. 22

    Order by with more than 1 of lenght

  23. 23

    dc.js stacked line chart with more than 1 dimension

  24. 24

    Is it a palindrome in more than 1 base?

  25. 25

    Is it possible to change text color and background color in the console to different colors while still on the same line? C++

  26. 26

    Textview2 is not showm when TextView1 have more than one line

  27. 27

    SearchView for more than 1 recyclerview

  28. 28

    more than 1 HttpPost is activating

  29. 29

    extract specific string if another string IN SAME LINE appears more than once

HotTag

Archive