Wed 21 Jan 2009

In windows form, if you have richtextbox or multiline textbox then usually enter key use to enter a new line.

But if AcceptButton property of Form is set for some action/button press, then pressing enter key in richtextbox or multiline textbox doesn't enter a new line rather AcceptButton calls.

To supress this set the property AcceptsTab to true in richtexbox. In case of multiline textbox / control set AcceptsReturn and AcceptTab property to true.

 

Tags: ,
E-mail this post to someone or Comments (3)

Comments

4/2/2009 | zahid  Singapore
Thanks faisal for sharing this
4/14/2009 | James Portelli  Malta
I'm stuck with the 'bug'. I cannot have RichTextBox behave the same like a TextBox simply for the fact that AcceptsReturn property is missing in RichTextBox. I cannot understand why. I want to disable the AcceptTab in RichTextBox, but I want the Enter key to enter a new line, not exit the form. Setting AcceptsTab to True suppresses the Enter key, but I don't want Tabbing in my RichTextBox. Any workaround would be appreciated.
4/14/2009 | FSL  Islamic Republic of Pakistan

Following will be one workaround

First set AcceptsTab property to True, then On that richtextbox's key press event, capture the tab key, and on that move focus to next control.

Here is the code snippet, hope this will help u



        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\t')
            {
                SetFocusOnNextControl(this, ActiveControl, e);
            }
        }



        private void SetFocusOnNextControl(Control container, Control activeControl, KeyPressEventArgs e)
        {
            try
            {
                Control Ctrl;
                Ctrl = container.GetNextControl(activeControl, true);
                if (Ctrl != null)
                {
                    if (Ctrl.Visible && Ctrl.Enabled && Ctrl.TabStop)
                    {
                        Ctrl.Focus();
                        e.Handled = true;
                        return;
                    }
                    else
                    {
                        SetFocusOnNextControl(container, Ctrl, e);
                    }
                }
                else
                {
                    SetFocusOnNextControl(container, null, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


Add Comment Post comment

 
 
 
   Country flag

Loading

For mobile, tablets or touch devices, if verify button is disabed, please press Enter after entering captcha verfication text than click Verify button.