Ultimate Collection - { fslBlog & faisalmb.com } Ultimate Collection - { fslBlog & faisalmb.com }   
Blog   |   Site   |   Posts (347)   |   Tags Xplorer   |   Feed Subscribe Free! Aha! you surfing post Richtextbox or multiline ... Sign in    Partner Site - Real Home Contact Search   

Wed

21

Jan

2009

Wed-21-01-2009
   

Richtextbox or multiline textbox and AcceptButton to handle Enter or Tab key press



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.

 


Comments

zahid  Singapore | Reply 4/1/2009 11:51:30 PM
Visitor Comment   Thanks faisal for sharing this
James Portelli  Malta | Reply 4/13/2009 11:47:31 PM
Visitor Comment   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.
FSL  Islamic Republic of Pakistan | Reply 4/14/2009 4:00:29 AM
Visitor Comment  
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 Add comment

 
 
 
   Country flag

Click to change image  --> 

biuquote
  • Comment
  • Preview
Loading





Intro

Faisal Bashir
Consultant / Software Architect
KalSoft Limited
Microsoft Certified Technology Specialist.
Currently in Dubai. [more]

Right Now

How could u reach the pearl by only looking at the sea? if u seek the pearl, be a diver: the diver needs several qualities, he must trust his rope and his life to the Friend's hand, he must stop breating and he must jump - Jalaluddin Rumi.

Recent Comments

Comment RSS

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

By the Fig and the Olive, And Mount Sinai, And this city of Security. Undoubtedly, We created man in the fairest stature. (Holy Quraan - At Teen 95-1to4)
383396 hits. (Best viewed @ 1024x768 resolution min.) Comments here...
© 2001-2011 Muhammad Faisal | Disclaimer | Contact | Partner Site