【解決方法】C#コード二重カウント&char削除でカウント減らない


RichTextBox を使用して、このプロジェクトのさまざまな用途を試してきました
TextChanged KeyDown & now KeyPress でコードを試しました。
私が扱っている問題は次の2つです。
1.以下のコードは、文字を入力または削除すると二重にカウントされます。
2.大きな目標は、削除時にカウントを取得し、その現在のカウントを許可することです
文字の追加に戻るときにバックアップを取得します。
何が起こっているのかを説明するために、コードにコメントを追加しました。
データ入力の管理にTextChanged、キャプチャーにKeyDownを使ってみた
文字を削除しますが、変数の同期は機能しません。
これは WinForms アプリです。
二重カウントのヘルプをいただければ幸いです。
キャラクターの追加と削除、およびこの変数の同期を管理する方法。
この同期は多くのことを要求するものなので、何週間も試してきたことを理解してください!

私が試したこと:

using System;
using System.Windows.Forms;

namespace ATCTest
{
    public partial class frmDBC : Form
    {
        int cTLen = 0;
        int lnNT = 0;
        int lnN;
        static int res;
        static int count;
        static int gvX;
        static bool YN;
        int cLL1 = 0;
        int cLL2 = 0;
        int cLL3 = 0;
        int cLL4 = 0;
        public frmDBC()
        {
            InitializeComponent();
            rtbInfo.KeyPress += rtbInfo_KeyPress;
        }
        public class GV
        {
            public static int x;
        }
        public static int returnInt()
        {
            GV.x = gvX++;
            var b = count++;
            res = 33 - b;
            return res + GV.x;
        }

        private void rtbInfo_KeyPress(object sender, KeyPressEventArgs e)
        {
            cTLen = rtbInfo.TextLength;
            lnNT = rtbInfo.Lines.Length;
            lnN = (5 - lnNT);
            txtBoxMsg.Text = "Total of " + (135 - cTLen) + " can be entered on " + lnN + " Lines";
            
            // Press ESCAPE key counts two increments each keypress
           
            if (e.KeyChar == (char)Keys.Escape)
            {
                returnInt();
                e.Handled = true;
            }
            tbTest.Text = "Value res " + res + "  &  GV.x  " + GV.x + "";

            // Code below tried to count characters
            // being added and decrease count
            // when backspace key used

            /*foreach (char c in rtbInfo.Text)
            {
            if (!char.IsControl('\b') || char.IsPunctuation(c) || char.IsLetter(c) || char.IsNumber(c) || char.IsWhiteSpace(c))
            {
            YN = true; // Enter Char

            }
            else if (char.IsControl('\b') || !char.IsPunctuation(c) || !char.IsLetter(c) || !char.IsNumber(c) || !char.IsWhiteSpace(c))
            {
            YN = false;//Delete Char
            }
            else if (char.IsWhiteSpace(c))
            {
            tbTest.Text = "You pressed Space ";
            }

            if (YN == false)
            {
                res = 33 + cT;
                tbCount.Text = "Res F " + res + " YN " + YN + "";
            }
            else if (YN == true)
            {
                res = 33 - cT;
                tbCount.Text = "Res T " + res + " YN " + YN + "";
            }
            }*/

            // Code below was to manage count on each line
            // in the RichTextBox It has no way of capturing
            // backspace to decrease res variable
            
            if (lnN == 4)
        {
             cLL4++;
             tbMessage.Text = "Total of " + (33 - cLL4) + " more char can be entered on line " + (5 - lnN) + "";
             if (lnN == 4 && (33 - cLL4) == 0)
                {
                 tbTest.Text = "On lnN " + lnN + " and cLL4 =  " + res + " ";
                 SendKeys.Send("{ENTER}");
                 rtbInfo.ScrollToCaret();
                 rtbInfo.Focus();
             }
         }
         else if (lnN == 3)
         {
            cLL3 = cLL3 + 1;
             tbMessage.Text = "Total of " + (33 - cLL3) + " more char can be entered on line " + (5 - lnN) + "";
             if (lnN == 3 && (33 - cLL3) == 0)
             {
                 tbTest.Text = "On lnN " + lnN + " and cLL3 =  " + (33 - cLL3) + " ";
                 SendKeys.Send("{ENTER}");
                 rtbInfo.ScrollToCaret();
                 rtbInfo.Focus();
             }
         }
         else if (lnN == 2)
         {
             cLL2 = cLL2 + 1;
             tbMessage.Text = "Total of " + (33 - cLL2) + " more char can be entered on line " + (5 - lnN) + "";
             if (lnN == 2 && (33 - cLL2) == 0)
             {
                 tbTest.Text = "On lnN " + lnN + " and cLL2 =  " + (33 - cLL2) + " ";
                 SendKeys.Send("{ENTER}");
                 rtbInfo.ScrollToCaret();
                 rtbInfo.Focus();
             }
         }
         else if (lnN == 1)
         {
             cLL1 = cLL1 + 1;
             tbMessage.Text = "Total of " + (33 - cLL1) + " more char can be entered on line " + (5 - lnN) + "";
             if (lnN == 1 && (33 - cLL1) == 0)
             {
                 tbTest.Text = "On lnN " + lnN + " and cLL1 =  " + (33 - cLL1) + " ";
                 SendKeys.Send("{ENTER}");
                 rtbInfo.ScrollToCaret();
                 rtbInfo.Focus();
             }
         }
     }
        private void frmDBC_Load(object sender, EventArgs e)
        {
            ActiveControl = rtbInfo;
        }

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

解決策 1

元の質問とともに投稿された解決策: C#リッチテキストボックス編集 | 解決策 1[^]

コメント

タイトルとURLをコピーしました