Multiline textbox printing last item only

Asked

Viewed 199 times

-1

I’m using TextBoxt Multiline to "issue" the report in the system itself. Ta printing, however, appears the following message: "System.Windows.Forms.Textbox" and then the data.

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}");
        }
    }
}

2 answers

0

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

textBox1.Text = leitor;

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

textBox1.Text += leitor;
  • Right, We solved a problem. Only it appears the result preceded by System.Windows.Forms.Textbox, Text: 123

  • Is appearing: System.Windows.Forms.TextBox, Text: 123 in the field?

  • Yes Yes, the correct data is 123, but these things appear before there.

  • somewhere in your code, you must be doing: textBox1.Text += textBox1;

  • No, because it’s a new screen, and the function is private.

  • Take a look if you really aren’t doing it by accident...

  • I even looked, but there’s nothing. Even, there’s 90% of the code. Previous to this has the standard parts of Visual ... The standard functions..

Show 2 more comments

-1


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

string leitor;

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

Placing the following code before the while:

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

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


In relation to the text System.Windows.Forms.TextBox, Text: 123, really, as @Thiagomagalhães said, it seems to originate in textBox1.Text += textBox1; or something like that. Unless it already comes from the file...

  • The whole code of the function was like this... And still give that message "System..." private void button1_Click(Object Sender, Eventargs and) { using (Streamreader ler = new Streamreader(@"typed.txt")) { string reader; while ((reader = read.Readline()) != null) { textBox1.Appendtext($"{reader}{Environment.Newline}"); } } }

  • Post the whole class code in your question, we might find something.

  • How the file was created "spelled.txt"? In another application? Manually?

  • It was created on the system. I use the notepad to insert output data on another screen. But I open and close the Streamwrite on the screen in question.

  • Can you put the contents of the file in your question? Then we could validate the solution on our side.

  • It’s data entered through the program, manually. I put random data. It’s random data, whole numbers.

  • How are you using StreamWriter? Actually, you’re not doing the following: sw.WriteLine(textBox1); instead of sw.WriteLine(textBox1.Text);?

  • string value = txtvalor.Tostring();

  • I’m wearing it like this.

  • That is the error :). You have to do string valor = txtValor.Text;, otherwise you are assigning a text representation of the control type.

  • Yet you make the same mistake. ;/

  • I think it worked now.

  • Good! Mark the answer as useful and/or correct if it helped you :)

  • That’s my fourth question here, I don’t know how you do it. THANK YOU VERY MUCH FOR THE HELP.. It was of great value.

  • If you can guide me, I’d appreciate it, and I’m done here .

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.