C# RichTextBox edit count character deleted

54 Views Asked by At

I have a RichTextBox in a C# project. Adding characters and counting the number of characters per line works.
The issue comes when I try to delete characters with the Backspace Key
I have commented out the code I have tried while pressing the Backspace Key
I also tried to use a KeyPress event to manage counting characters deleted complete fail.
If anyone would like to test the code here are the properties of the RichTextBox Name rtbInfo.
Size 347 by 120 Font 10.8 Bold WordWrap False NO Scroll Bars Max Length 135
GOAL Increase the variable res by 1 for each character or space deleted with the press of the Backspace Key.
I would prefer to not use KeyPress KeyUp or KeyDown Events if at all possible.
Happy to consider code suggestions with those Event designs.

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;
            }

   
1

There are 1 best solutions below

0
Vector On

I solved this using both TextChanged & KeyPress events
While that solution worked I decided to put all the code in a KeyPress event ONLY.
One HUGE mistake in my original question was in InitializeComponent
Look up and read when & how to use this method. Enjoy the Code

   public partial class frmKPO : Form
{
    int rtbTotLen;
    int linesTot;
    int lineNum;
    int res1;
    int count1;
    int res2;
    int count2;
    int res3;
    int count3;
    int res4;
    int count4;
  
    public frmKPO()
    {
        InitializeComponent();
    }

    private void rtbInfo_KeyPress(object sender, KeyPressEventArgs e)
    {
        rtbTotLen = rtbInfo.TextLength;
        linesTot = rtbInfo.Lines.Length;
        lineNum = (5 - linesTot);

        txtBoxMsg.Text = "Total of " + (135 - rtbTotLen) + " can be entered on " + (5 - lineNum) + " Lines";

        if (e.KeyChar == (char)Keys.Back)
        {
            if (lineNum == 1)
            {
                --res1;
            }
            if (lineNum == 2)
            {
                --res2;
            }
            if (lineNum == 3)
            {
                --res3;
            }
            if (lineNum == 4)
            {
                --res4;
            }
        }
        else
        {
            if (lineNum == 1)
            {
                ++res1;
            }
            if (lineNum == 2)
            {
                ++res2;
            }
            if (lineNum == 3)
            {
                ++res3;
            }
            if (lineNum == 4)
            {
                ++res4;
            }
        }


        if (lineNum == 4)
        {
            count4 = 33 - res4;
            tbMessage.Text = "Total of " + count4 + " more char can be entered on line " + (5 - lineNum) + "";
            if (count4 == 0)
            {
                SendKeys.Send("{ENTER}");
                rtbInfo.ScrollToCaret();
                rtbInfo.Focus();
            }
        }
        else if (lineNum == 3)
        {
            count3 = 33 - res3;
            tbMessage.Text = "Total of " + count3 + " more char can be entered on line " + (5 - lineNum) + "";
            if (count3 == 0)
            {
                SendKeys.Send("{ENTER}");
                rtbInfo.ScrollToCaret();
                rtbInfo.Focus();
            }
        }
        else if (lineNum == 2)
        {
            count2 = 33 - res2;
            tbMessage.Text = "Total of " + count2 + " more char can be entered on line " + (5 - lineNum) + "";
            if (count2 == 0)
            {
                SendKeys.Send("{ENTER}");
                rtbInfo.ScrollToCaret();
                rtbInfo.Focus();
            }
        }
        else if (lineNum == 1)
        {
            count1 = 33 - res1;
            tbMessage.Text = "Total of " + count1 + " more char can be entered on line " + (5 - lineNum) + "";
            if (count1 == 0)
            {
                SendKeys.Send("{ENTER}");
                rtbInfo.ScrollToCaret();
                rtbInfo.Focus();
            }
        }
        if (linesTot == 5 | rtbTotLen == 135)
        {
            rtbInfo.ReadOnly = true;
        }

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

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

            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();
            }
            else
            {
                rtbInfo.ReadOnly = false;
                rtbInfo.Clear();
                tbCount.Text = "All Notes Remove";
            }
        }
    }

    private void btnToStart_Click(object sender, EventArgs e)
    {
        Close();
        frmStart fS = new frmStart();
        fS.Show();
    }

    private void frmKPO_Load(object sender, EventArgs e)
    {
        ActiveControl = rtbInfo;
        tbMessage.Text = "Total of " + 33 + " more char can be entered on line " + 1 + "";
    }
}