0

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

 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 时尝试实现排序方法时,我希望“排序方式:名称”出现在文本框的顶部,但我当前的代码将它放在所有播放器的底部。

当前名称排序代码:

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)" 但这也没有用。

4

2 回答 2

1
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");
于 2017-04-12T21:23:52.310 回答
0

我会选择 String.Format ,因为如果您希望将来使您的字符串更加精美,它非常灵活且易于阅读。

String s = String.Format("SORT BY: NAME \r\n {0}", txtRosterLog.Text);
于 2017-04-12T21:29:48.907 回答