![](/img/trans.png)
[英]removing RichTextBox lines while keeping the colour of remaining lines in C#
[英]c# richtextbox display character remaining count
是的,我已經研究了這個問題。 我發現了這一點: 如何在C#中的標簽中顯示剩余的文本框字符? 和許多其他人一樣。 這就是我設法將以下代碼拼湊而成的方式:
protected void rtdDisclaimer(object sender, EventArgs e)
{
lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}
我以前從未使用c#進行編碼,但是正在從事小組項目,這就是小組負責人選擇的語言。 我是編程新手,對Java的經驗很少。 該程序在Visual Studio中完成。 我試圖使標簽顯示剩余的字符數,具體取決於在richtextbox中鍵入的內容。 沒有錯誤,但標簽根本沒有顯示任何內容。
您必須將方法與控件的textchanged事件關聯。
protected void rtbDisclaimerTextChanged(object sender, EventArgs e)
{
lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}
在構造函數上,在InitializeComponent()之后,必須添加以下行:
rtbDisclaimer.TextChanged += new EventHandler(rtbDisclaimerTextChanged);
您的代碼可以使用,但是您需要將該方法附加到RichTextBox
上的正確事件上,因此它將在事件觸發時調用該代碼。
將其添加到Form
的構造函數中:
rtbDisclaimer.TextChanged += rtdDisclaimer;
您在問題中提到的功能似乎在服務器端,您可以在諸如按鈕單擊之類的事件中調用它。 如果要在鍵入時顯示字符長度,請使用JavaScript / jQuery
喜歡
$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);
function updateCount() {
var cs = $(this).val().length;
$('#characters').text(cs);
}
也檢查一下。 JsFiddle示例 (由Dreami提供 )
希望這會有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.