Manipulation of Textbox

Asked

Viewed 161 times

0

You simply fill in the fields Modelo, Placa, Km and it will be saved in the fields multiline down below:

Tela com o resultado

But when I click on the "Save" button the result is totally opposite to the expected one. I wanted each field to be one in front of the other and when it came to the end "novoCarro.mostraKm()" he skipped a line to then save the next vehicle on the next line.

txtCarros.Text += novoCarro.mostraModelo() + "/t";

txtCarros.Text += novoCarro.mostraPlaca() + "/t";

txtCarros.Text += novoCarro.mostraKm() + "/r/n";
  • Wouldn’t r n? or n

  • i tested with only /n and the result was really, but just in case I’ll edit the topic

  • test with the backslash? n?

  • another way would be txtCarros.Text += Environment.Newline + newCarro.shows()

  • My, my, my, that’s right :)

  • Blz. I will add as an answer only to help anyone with the same question.

Show 1 more comment

3 answers

4


The best way to do it is like this:

txtCarros.Text = string.Join("\t", novoCarro.mostraModelo(), novoCarro.mostraPlaca(), novoCarro.mostraKm(), "\n");

Concatenate string manually generates several allocations and this in addition to being slow creates pressure on the garbage collector. This case doesn’t do a lot of damage, but if the number of items increases, it gets exponentially worse.

I put the tabs that is the question asks, and the line break only at the end.

In a simplified form:

public class Program { public static void Main() => System.Console.WriteLine(string.Join("\t", "modelo", "placa", "km", "\n")); }

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Documentation of Join().

But if you are doing everything in the same expression it is possible to simplify a little the syntax that the compiler will transform into Concat() for you, and there fits even better:

txtCarros.Text = novoCarro.mostraModelo() + "\t" + novoCarro.mostraPlaca() + "\t" novoCarro.mostraKm() + "\n";

For a few items it is not worth using the StringBuilder, especially when you don’t know the final size.

Another alternative would be:

txtCarros.Text = $"{novoCarro.mostraModelo()}\t{novoCarro.mostraPlaca()}\t {novoCarro.mostraKm()}\n";
  • I did not know this detail, but thank you very much I will add the codes to my program, vlw!

1

To break the string’s line just replace the "/t" bar with the backslash " n"

txtCarros.Text += novoCarro.mostraModelo() + "\n";

txtCarros.Text += novoCarro.mostraPlaca() + "\n";

txtCarros.Text += novoCarro.mostraKm() + "\n";

Another way is to use String + Environment.Newline + String.

0

What’s wrong with your code, besides manual string concatenation, is that you’re using / instead of \, as an escape character.
Instead of "/t" must use "\t" and, for line breaking, "\n".

To avoid the problems (slowness and pressure on the garbage collector) of manual string concatenation and that shows what you are doing use the class Stringbuilder:

StringBuilder builder = new StringBuilder();
builder.Append(novoCarro.mostraModelo());
builder.Append("\t");
builder.Append(novoCarro.mostraPlaca());
builder.Append("\t");
builder.AppendLine(novoCarro.mostraKm());
txtCarros.Text = builder.ToString();

Browser other questions tagged

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