How do I make console output appear in a textArea in WindowsForms?

1

I would like to know a way in which the output of my project will appear in a textArea in WindowsForms. I need this because my system is a binary number converter, and I want you to print on the screen the calculation step by step. If there is another way, I accept suggestions!

Example method:

public int ConverterDeBinarioParaDecimal(String numeroBinario)
    {
        double resultado = 0;
        int qtdDigitos = numeroBinario.Length;
        Console.WriteLine("xxx = {0}", qtdDigitos);

        for (int cont = 0; cont < qtdDigitos; cont++)
        {
            if (numeroBinario[cont] == '1')
            {
                resultado = resultado + Math.Pow(2, qtdDigitos - (cont + 1));

            }
        }

        int resultadoConversao = Convert.ToInt32(resultado);

        return resultadoConversao;
    }
    
asked by anonymous 12.06.2018 / 05:23

1 answer

1

A console only displays the log of the executable, it would be complicated for you to redirect the output of the Console stream to a TextBox (this is not impossible, but it's annoying). There's a simple line of code that lets you do the same thing:

public static void WriteLn (string text, params string[] args)
    => TextBox1.Text += "\n" + String.Format(text, args);
public static void WriteLn (string text)
    => WriteLn (text, new string[] { });

To use, just replace

Console.WriteLine("xxx = {0}", qtdDigitos);

by:

WriteLn("xxx = {0}", qtdDigitos);
  

Note: Change the name of the control TextBox1 if necessary.

    
12.06.2018 / 05:44