You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# RichTextBox退格删除字符时res变量计数异常问题求助

C# RichTextBox退格键计数异常问题

在C#项目中使用RichTextBox实现字符及每行字符计数功能时,输入字符的计数逻辑正常,但按下退格键删除字符时,无法正确更新计数(目标是每删除一个字符/空格就将变量res加1)。尝试过的退格相关处理代码已注释,也试过用KeyPress事件处理删除计数,但完全失效。

RichTextBox属性

  • 控件名称:rtbInfo
  • 尺寸:347×120
  • 字体:10.8号加粗
  • WordWrap:False
  • 无滚动条
  • 最大长度:135

需求目标

每按下退格键删除一个字符或空格,将变量res的值加1。优先尝试不使用KeyPress、KeyUp或KeyDown事件的实现方案,也可接受基于这些事件的代码方案。

现有代码

public partial class frmRTB : Form
{
    int Count = 0;
    int Lines = 0;
    int cT = 0;
    int res = 0;
   
    public frmRTB()
    {
        InitializeComponent();
        //rtbInfo.KeyPress += rtbInfo_KeyPress;
      /*=== This Must be here to register rtbInfo ===*/
      /*=============================================*/
    }

    private void frmRTB_Load(Object sender, EventArgs e)
    {
        ActiveControl = rtbInfo;
    }

    /*private void rtbInfo_KeyPress(object sender, KeyPressEventArgs e)
    {
        
        if (rtbInfo.TextLength == 0)
        {
            tbMessage.Text = "Nothing to Delete";
            return;
        }
       
        if (e.KeyChar == ((char)Keys.Back) & (char.IsControl('\b')))
        {
         
            //rtbInfo.Text = rtbInfo.Text.Remove(rtbInfo.TextLength - 1, 0);
            //rtbInfo.Select(rtbInfo.TextLength - 0, 0);
            //rtbInfo.ScrollToCaret();
            //cT = cT - 1;
            //res = - 1;// Last edit was res - cT ?
            //tbCount.Text = "Back Fire";
            //e.Handled = true;
        }
    }*/
  
    public void rtbInfo_TextChanged(Object sender, EventArgs e)
    {
        Count = rtbInfo.TextLength;
        Lines = rtbInfo.Lines.Length;

        foreach (char chr in rtbInfo.Text)
        {
            if (char.IsLetterOrDigit(chr))
            {
                cT = cT + 1;
                res = 33 - cT;
            }

            /*
            Regex regex = new Regex(@"^[\b]+$");
            //Regex regex = new Regex(@"^[\b\S\t\D\W]+$");

            if (regex.IsMatch(rtbInfo.Text))
            {
              tbCount.Text = "Use BACKSPACE Key to Edit";
            }*/
   

        //if (chr == '\b')
        //if (chr == ((char)Keys.Back))
        // tried above if statments they do not fire

        // This fires when any key is entered NOT just the Backspace key
      /*if (char.IsControl('\b'))// & !(char.IsLetterOrDigit(chr)))
        {
        //rtbInfo.Text = rtbInfo.Text.Remove(rtbInfo.TextLength - 1, 0);
        //rtbInfo.Select(rtbInfo.TextLength - 0, 0);
        //rtbInfo.ScrollToCaret();
         res = +1;
         tbCount.Text = "Use BACKSPACE Key to Edit";
       }*/

            if (Lines == 5 | Count == 135)
             {
                 rtbInfo.ReadOnly = true;
             }

              if (rtbInfo.ReadOnly == true)
              {
                  txtBoxMsg.Text = "";
                  tbMessage.Text = "";
                  tbCount.Text = "Use BACKSPACE Key to Edit";
              }

              if (rtbInfo.Text.EndsWith("\n"))
              {
                  cT = 0;
                  res = 0;
              }

              if (cT == 33)
              {
                   SendKeys.Send("{ENTER}");
                   rtbInfo.ScrollToCaret();
                   rtbInfo.Focus();
                   cT = 0;
                   res = 0; 
              }

              if (rtbInfo.ReadOnly == true)
              { 
                  string message = "Press YES to edit Notes" + '\n' + "        NO Clear Notes";
                  string title = "Edit Notes";
                  MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                  DialogResult result = MessageBox.Show(message, title, buttons);
               if (result == DialogResult.Yes)
               {
                   rtbInfo.ReadOnly = false;
                   SendKeys.Send("{BACKSPACE}");
                   rtbInfo.ScrollToCaret();
                   rtbInfo.Focus();
                   cT = 0;
                   res = 0;
              }
              else
              {
                   rtbInfo.ReadOnly = false;
                   rtbInfo.Clear();
                   tbCount.Text = "All Notes Remove";
              }
              }

              if (res == 0)
              {
                  res = 33;
              }
                txtBoxMsg.Text = "Total of " + (135 - Count) + " can be entered & " + (5 - Lines) + " Lines";
                tbMessage.Text = "Total of " + res + " more char can be entered on line " + Lines;
                //tbCount.Text = "Count is " + Count + " cT " + cT + " res " + res;// For Testing
                return;
            }
    }
}

内容的提问来源于stack exchange,提问作者Vector

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 23:50:23