c#文本框不显示所有内容

时间:2013-11-13 16:53:10

标签: c# wpf textbox

我想要一个显示单词Seq (这是一个列名)的文本框,然后列出其下面的mylist中的值。到目前为止,列表中的值显示为,但单词Seq不显示

  private void button7_Click(object sender, EventArgs e)
          {
              if (seq1) 
              {
                  textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
                  foreach (object o in SeqIrregularities)
                  {
                      textBox1.Text = String.Join(Environment.NewLine, SeqIrregularities);
                  }
              }

          }

2 个答案:

答案 0 :(得分:5)

您将textBox1.Text的值重新分配给值列表,而不是将值列表附加到文本框内容中。

试试这个:

 textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
 textBox1.Text += Environment.NewLine + String.Join(Environment.NewLine, SeqIrregularities);

如果你正在做的是创建一个串联的字符串,你也不需要遍历你的违规行为。

另一种方法(可能更清楚):

string irregularities = String.Join(Environment.NewLine, SeqIrregularities);
string displayString = " Seq" + Environment.NewLine + irregularities;
textBox1.Text = displayString;

答案 1 :(得分:2)

将您的代码更改为:

  private void button7_Click(object sender, EventArgs e)
          {
              if (seq1) 
              {
                  textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
                  foreach (object o in SeqIrregularities)
                  {
                      textBox1.Text += String.Join(Environment.NewLine, SeqIrregularities);
                  }
              }

          }

您在foreach语句的每次迭代中都覆盖了您的文本。您必须在foreach语句中使用+=而不是=

相关问题