首页 文章

C#多行文本框

提问于
浏览
-1

我对c#的多行文本框感到困惑 . 有没有办法让每一行的字符串中有1个项目?我正在使用foreach语句为文本框中的每一行执行命令 .

这是我到目前为止的代码...我正在尝试对输入的所有管理员帐户和输入的所有标准帐户执行某些操作 .

private void button1_Click(object sender, EventArgs e)
{
    string[] administrator = textBox2.Text.Split(Environment.NewLine);
    string[] standard = textBox1.Text.Split(Environment.NewLine);
    foreach(string admin in administrator)
    {
        //edit accounts here
    }
}

2 回答

  • 0

    您的问题没有显示任何代码示例,那么这个答案可能不合适 .

    无论如何,你应该使用TextBox.Lines属性:

    private void button1_Click(object sender, EventArgs e)
    {
        this.textBox1.Lines = new string[] { "Hello", "World" };
        string[] lines = this.textBox1.Lines;
    }
    
  • 0

    如果每个帐户都在TextBox的单独行中(应该使用类似ListBox的声音),那么您只需要遍历Lines属性:

    for (int i = 0; i < textBox1.Lines.Length; ++i) {
      string account = textBox1.Lines[i];
      // do something with account...
    }
    

相关问题