将文本追加到文本框顶部

Append text to Top of Textbox

使用列表填充文本框时。 Display方法如下

 private void Display()
    {
        StringBuilder sb = new StringBuilder();
        foreach (Player dude in _FootballRoster)
        {
            if (btnUSA.Checked == true)
            {
                sb.AppendLine("\r\nName: " + dude.getName() + " \r\n Team: " + dude.getTeam() + "\r\n Birthday: " + dude.getBirthday() + "\r\n Height(in):" + dude.getHeight() + "\r\n Weight(lbs): " + dude.getWeight() + "\r\n Salary(USD): " + dude.getSalary());
            }
            if (btnUSA.Checked == false)
            {
                sb.AppendLine("\r\nName: " + dude.getName() + " \r\n Team: " + dude.getTeam() + "\r\n Birthday: " + dude.getBirthday() + "\r\n Height(meters):" + (dude.getHeight()) / 39.3701 + "\r\n Weight(kg): " + (dude.getWeight()) / 2.20462 + "\r\n Salary(CD): " + (dude.getSalary()) / 1.31);
            }
        }
        txtRosterLog.Text = sb.ToString();
    }

当您尝试在单击 btnName 时实现 Sort 方法时,我希望 "SORT BY: NAME" 出现在文本框的顶部,但我当前的代码将其放在所有播放器的底部。

当前名称排序代码:

private void btnName_Click(object sender, EventArgs e)
    {

        _FootballRoster = _FootballRoster.OrderBy(dude => dude.Name).ToList();
        Display();
        txtRosterLog.AppendText("SORT BY: NAME ");

    }

有什么想法吗?我试过使用 txtRosterLog.Text.Insert(0, "SORT BY NAME)" 但这也没有用。

txtRosterLog.Text = "SORT BY: NAME \r\n" + txtRosterLog.Text;

txtRosterLog.Text.Insert(0, "SORT BY NAME)" 如果您将其分配回去也可以工作:

txtRosterLog.Text = txtRosterLog.Text.Insert(0, "SORT BY NAME");

我会选择 String.Format,因为它非常灵活且易于阅读,如果你想让你的字符串在未来更花哨的话。

String s = String.Format("SORT BY: NAME \r\n {0}", txtRosterLog.Text);