我有一個按鈕,單擊該按鈕可將文本推送(添加)到 C# win 表單中的標簽中。現在我需要確定標簽中文本中某個字符重復的特定次數,例如字符“a”重復了多少次。在確定這一點后,我計劃在按下另一個按鈕時執行不同的操作。
下面我將包含我撰寫的一些代碼,以了解我希望我的應用程式如何作業。
//button that adds text to the label
private void btnLeave_Click(object sender, EventArgs e)
{
if (txtMembertype.Text == "Level A" && borrow_count > 3)
{
MessageBox.Show("You can only borrow three books at a time", "SyscoTech", MessageBoxButtons.OK, MessageBoxIcon.Warning);
lblBorrowings.Text = "Books borrowed - ";
}
else if (txtMembertype.Text == "Level B" && borrow_count > 2)
{
MessageBox.Show("You can only borrow two books at a time", "SyscoTech", MessageBoxButtons.OK, MessageBoxIcon.Warning);
lblBorrowings.Text = "Books borrowed - ";
}
//another button which does the same thing as above
private void btnBorrowedBk_Click(object sender, EventArgs e)
{
lblBorrowings.Text = lblBorrowings.Text "\n\t " cmbBooks.Text "";
cmbBooks.Text = "";
borrow_count = borrow_count 1;
}
//I plan on having an if-else like below
if (txtMembertype.Text == "Level A" && label.text == "a" > 3 [if a is repeated 3 times])
{
// do this
}
else if (txtMembertype.Text == "Level B" && label.text == "a" > 2 [if a is repeated two times])
{
// do this
}
任何幫助表示贊賞。提前致謝。
uj5u.com熱心網友回復:
使用 linq,您可以使用 Count 擴展方法:
label.text.Count(c=> c == 'a') > 3
不要忘記添加using System.Linq;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512675.html
