programing

여러 줄 TextBox에 줄을 추가하는 방법은 무엇입니까?

itsource 2021. 1. 17. 10:52
반응형

여러 줄 TextBox에 줄을 추가하는 방법은 무엇입니까?


여러 줄에 텍스트 줄을 추가하려면 어떻게 TextBox합니까?

예 : 의사 코드;

textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");

또는

textBox1.Lines.Append("brown");
textBox1.Lines.Append("brwn");
textBox1.Lines.Append("brn");
textBox1.Lines.Append("brow");
textBox1.Lines.Append("br");
textBox1.Lines.Append("brw");
textBox1.Lines.Append("brwm");
textBox1.Lines.Append("bron");
textBox1.Lines.Append("bwn");
textBox1.Lines.Append("brnw");
textBox1.Lines.Append("bren");
textBox1.Lines.Append("broe");
textBox1.Lines.Append("bewn");

TextBox.Lines가 구현 하는 유일한 메서드 는 다음과 같습니다.

  • 복제
  • 에게 복사
  • 같음
  • GetType
  • GetHashCode
  • GetEnumerator
  • 초기화
  • GetLowerBound
  • GetUpperBound
  • GetLength
  • GetLongLength
  • GetValue
  • SetValue
  • ToString

여기에 이미지 설명 입력


@Casperah는 내가 잘못 생각하고 있다고 지적했습니다. A 에는TextBox이없고 텍스트가 있습니다. 요청 된 경우 해당 텍스트는 CRLF에서 행으로 분할 될 수 있지만 행 개념은 없습니다.

문제는 WinForms가 나에게 허용하는 것보다 내가 원하는 것을 달성하는 방법입니다.

다른 주어진 변형에는 미묘한 버그가 있습니다.

  • textBox1.AppendText("Hello" + Environment.NewLine);
  • textBox1.AppendText("Hello" + "\r\n");
  • textBox1.Text += "Hello\r\n"
  • textbox1.Text += System.Environment.NewLine + "brown";

필요하지 않을 때 개행을 추가하거나 앞에 추가합니다.

따라서 확장 도우미 :

public static class WinFormsExtensions
{
   public static void AppendLine(this TextBox source, string value)
   {
      if (source.Text.Length==0)
         source.Text = value;
      else
         source.AppendText("\r\n"+value);
   }
}

그래서 지금:

textBox1.Clear();
textBox1.AppendLine("red");
textBox1.AppendLine("green");
textBox1.AppendLine("blue");

textBox1.AppendLine(String.Format("Processing file {0}", filename));

Note: Any code is released into the public domain. No attribution required.


I would go with the System.Environment.NewLine or a StringBuilder

Then you could add lines with a string builder like this:

StringBuilder sb = new StringBuilder();
sb.AppendLine("brown");
sb.AppendLine("brwn");

textbox1.Text += sb.ToString();

or NewLine like this:

textbox1.Text += System.Environment.NewLine + "brown";

Better:

StringBuilder sb = new StringBuilder(textbox1.Text);
sb.AppendLine("brown");
sb.AppendLine("brwn");

textbox1.Text = sb.ToString();

Append a \r\n to the string to put the text on a new line.

textBox1.Text += ("brown\r\n");
textBox1.Text += ("brwn");

This will produce the two entries on separate lines.


Try this

textBox1.Text += "SomeText\r\n" 

you can also try

textBox1.Text += "SomeText" + Environment.NewLine;

Where \r is carriage return and \n is new line


You have to use the AppendText method of the textbox directly. If you try to use the Text property, the textbox will not scroll down as new line are appended.

textBox1.AppendText("Hello" + Environment.NewLine);

The "Lines" property of a TextBox is an array of strings. By definition, you cannot add elements to an existing string[], like you can to a List<string>. There is simply no method available for the purpose. You must instead create a new string[] based on the current Lines reference, and assign it to Lines.

Using a little Linq (.NET 3.5 or later):

textBox1.Lines = textBox.Lines.Concat(new[]{"Some Text"}).ToArray();

This code is fine for adding one new line at a time based on user interaction, but for initializing a textbox with a few dozen new lines, it will perform very poorly. If you're setting the initial value of a TextBox, I would either set the Text property directly using a StringBuilder (as other answers have mentioned), or if you're set on manipulating the Lines property, use a List to compile the collection of values and then convert it to an array to assign to Lines:

var myLines = new List<string>();

myLines.Add("brown");
myLines.Add("brwn");
myLines.Add("brn");
myLines.Add("brow");
myLines.Add("br");
myLines.Add("brw");
...

textBox1.Lines = myLines.ToArray();

Even then, because the Lines array is a calculated property, this involves a lot of unnecessary conversion behind the scenes.


The adding of Environment.NewLine or \r\n was not working for me, initially, with my textbox. I found I had forgotten to go into the textbox's Behavior properties and set the "Multiline" property to "True" for it to add the lines! I just thought I'd add this caveat since no one else did in the answers, above, and I had thought the box was just going to auto-expand and forgot I needed to actually set the Mulitline property for it to work. I know it's sort of a bonehead thing (which is the kind of thing that happens to us late on a Friday afternoon), but it might help someone remember to check that. Also, in the Appearance section is the "ScrollBars" property that I needed to set to "Both", to get both horizontal and vertical bars so that text could actually be scrolled and seen in its entirety. So the answer here isn't just a code one by appending Environment.NewLine or \r\n to the .Text, but also make sure your box is set up properly with the right properties.


Just put a line break into your text.

방법으로 줄을 추가하지 않습니다. 여러 줄은 줄 바꿈 사용을 지원합니다.


원하는 줄 수를 알고 있다면 그 수의 멤버 (예 : myStringArray)로 문자열 배열을 만듭니다. 그런 다음 myListBox.Lines = myStringArray;


위의 방법은 저에게 효과적이지 않았습니다. 다음 예외가 있습니다.

Exception : 'System.InvalidOperationException' in System.Windows.Forms.dll

Invoke먼저 컨트롤 을 호출해야했습니다 . 여기에서 답변을 참조 하십시오 .

참조 URL : https://stackoverflow.com/questions/8536958/how-to-add-a-line-to-a-multiline-textbox

반응형