将文本附加到文本框顶部

时间:2017-04-12 21:20:30

标签: c# textbox appendtext

使用List填充文本框时。 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”),但这也不起作用。

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

我会使用String.Format,因为它非常灵活且易于读取,如果你想让你的字符串在将来更加花哨。

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