Textbox multiline printing only last item [closed]

-1

I'm using TextBoxt Multiline to "cast" the report on the system itself. When printing, however, the following message appears: "System.Windows.Forms.TextBox" and then the given.

public Formulário()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    using (StreamReader ler = new StreamReader(@"escrevesaida.txt"))
    {
        string leitor;

        while ((leitor = ler.ReadLine()) != null)
        {
            textBox1.AppendText($"{leitor}{Environment.NewLine}");
        }
    }
}
    
asked by anonymous 03.08.2018 / 13:35

2 answers

-1

If the goal is to separate line by line, the best way is:

string leitor;

while ((leitor = ler.ReadLine()) != null)
{
    textBox1.AppendText($"{leitor}{Environment.NewLine}");
}

Placing the following code before while :

leitor = ler.ReadLine();
textBox1.Text = leitor;

I was picking up the first line and then the rest. You can put everything into the loop.

Regarding the text System.Windows.Forms.TextBox, Text: 123 , actually, as ThiagoMagalhães mentioned, it seems to be from textBox1.Text += textBox1; or something like this. Unless this already comes from the file ...

    
03.08.2018 / 14:56
0

The problem is that in Text of textBox , for each line it reads you are assigning the value, replacing the value you had previously.

textBox1.Text = leitor;

The correct would be to concatenate, that is, to join the results of each line. As follows:

textBox1.Text += leitor;
    
03.08.2018 / 13:39