textBox1.AppendText("Hello World!");
string[] colors = { "red", "green", "blue" }; foreach (string color in colors) { textBox1.AppendText(color + " "); }
string message = "This is a long message that needs to be split into multiple lines in the TextBox."; int lineLength = 30; int startIndex = 0; while (startIndex + lineLength < message.Length) { int lineBreak = message.LastIndexOf(' ', startIndex + lineLength); if (lineBreak == -1) { lineBreak = startIndex + lineLength; } int lineLengthWithBreak = lineBreak - startIndex + 1; textBox1.AppendText(message.Substring(startIndex, lineLengthWithBreak) + Environment.NewLine); startIndex = lineBreak + 1; } textBox1.AppendText(message.Substring(startIndex));This code will append the message to the end of the text in a TextBox named textBox1, but will also break the message into multiple lines if it is too long to fit on one line. It uses the LastIndexOf method to find the closest space character to the end of the line, then adds a newline character to start a new line. Overall, the System.Windows.Forms.TextBox class is a powerful tool for working with text in a graphical user interface in C#. The AppendText method is easy to use and allows you to add text quickly and efficiently.