Edit style textbox

Ashu seh 60 Reputation points
2023-12-02T14:21:29.3166667+00:00

Hello

 
        static bool isEditModeEnabled = false; 
        private static void Txb1_KeyDown(object sender, KeyEventArgs e)
        {
            var txb = (TextBox)sender;         
            if (e.Control && e.KeyCode == Keys.R)
            {
                isEditModeEnabled = !isEditModeEnabled;
                e.Handled = true;
            }
}


  private static void Txb_KeyPress(object sender, KeyPressEventArgs e)
        {
            var txb = (TextBox)sender;
           
            if (isEditModeEnabled)
            {
                int currentPosition = txb.SelectionStart;
                txb.Text = txb.Text.Remove(currentPosition, 1).Insert(currentPosition, e.KeyChar.ToString());
                txb.SelectionStart = currentPosition + 1;
                e.Handled = true;
            }


In the above code , i tried to edit the text whenever the user presses ctrl + r inside a textbox . but when he presses ctrl+k , [] is been replacing as text for no reason and whenever he presses backspace the program crashes . Can some expert please check why is this happening .

Thanks in advance .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. KOZ6.0 6,735 Reputation points
    2023-12-04T06:54:08.43+00:00
    private static bool isEditModeEnabled = false;
    
    private void Txb_Enter(object sender, EventArgs e) {
        isEditModeEnabled = false; //clear Flag
    }
    
    private static void Txb_KeyDown(object sender, KeyEventArgs e) {
        var txb = (TextBox)sender;
        if (e.Control && e.KeyCode == Keys.R) {
            isEditModeEnabled = !isEditModeEnabled;
            e.Handled = true;
            return;
        }
    }
    
    private static void Txb_KeyPress(object sender, KeyPressEventArgs e) {
        var txb = (TextBox)sender;
        if (isEditModeEnabled && IsAllowChar(txb, e.KeyChar)) {
            int currentPosition = txb.SelectionStart;
            string txt = txb.Text;
            if (currentPosition < txt.Length) {
                txt = txt.Remove(currentPosition, 1);
            }
            if (txb.MaxLength == 0 || txt.Length < txb.MaxLength) {
                txb.Text = txt.Insert(currentPosition, e.KeyChar.ToString());
                txb.SelectionStart = currentPosition + 1;
                txb.SelectionLength = 0;
                e.Handled = true;
                return;
            }
        }
    }
    
    private static bool IsAllowChar(TextBox txb, char keyChar) {
        if (txb.Multiline) {
            switch (keyChar) {
                case '\r':
                case '\n':
                case '\t':
                    return true;
                default:
                    if (keyChar  < ' ') {
                        return false;
                    }
                    break;
            }
        } else {
            if (keyChar < ' ') {
                return false;
            }
        }
        return true;
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. gekka 12,206 Reputation points MVP Volunteer Moderator
    2023-12-03T00:25:06.57+00:00

    Can't remove when there is no character after the caret position.

    if (currentPosition < txb.Text.Length - 1)
    {
        txb.Text = txb.Text.Remove(currentPosition, 1).Insert(currentPosition, e.KeyChar.ToString());
    }
    else
    {
        txb.Text = txb.Text.Insert(currentPosition, e.KeyChar.ToString());
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.