1 回答

TA貢獻1893條經驗 獲得超10個贊
有幾種方法可以實現這一目標,以下是一個建議。
由于您需要時間戳以及添加的字符串,因此可以將其作為 Notes 類的一部分。例如,修改notes類如下。
class Notes
{
? ? public string Note { get; set; }
? ? public DateTime TimeStamp { get; set; }
? ? public Notes(string note)
? ? {
? ? ? ? Note = note;
? ? ? ? TimeStamp = DateTime.Now;
? ? }
? ? public override string ToString()
? ? {
? ? ? ? return $"{Note}-{TimeStamp.ToString()}";
? ? }
}
現在,您可以在 Main 類中定義一個集合,該集合將保存每個添加的注釋。
private List<Notes> _notesCollection = new List<Notes>();
最后,btnAddNote 單擊事件如下所示
private List<Notes> _notesCollection = new List<Notes>();
private void btnAddNote_Click(object sender, EventArgs e)
{
? ? var note = new Notes(txtNoteWriter.Text);
? ? _notesCollection.Add(note);
? ? txtNoteReader.Text = string.Join(Environment.NewLine, _notesCollection.OrderByDescending(x => x.TimeStamp).Select(x => x.ToString()));
}
在按鈕 Click 事件中,您將向集合中添加新注釋。然后,您使用 LINQ 根據 TimeStamp 屬性對集合進行排序。為此,您使用OrderByDescending方法。Select方法使您能夠從集合中選擇需要顯示的內容。
最后,string.Join方法允許您連接不同的字符串以形成最終結果。
- 1 回答
- 0 關注
- 176 瀏覽
添加回答
舉報