You can change the text alignment in the RichTextBox control when you set the ReadOnly property to true (814311)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

Beta Information

This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.

SYMPTOMS

You set the ReadOnly property to True in the RichTextBox control. You can still change the text alignment in the RichTextBox to Right, Left, Justify, and Middle by using the RichEdit functional shortcut keys such as CTRL+R, CTRL+L, CTRL+J, and CTRL+E, respectively.

CAUSE

When you set the ReadOnly Property to True, you cannot change the RichTextBox text. However, you can copy the text. The shortcut keys for copying, alignment, and other procedures are derived from the RichEdit functionality. By default, these shortcuts are permitted.

WORKAROUND

To work around this problem, add the following code to the KeyDown event of the RichText Box. This code ignores the keys that you press (such as CTRL+E, CTRL+L, CTRL+R, and CTRL+J).

Visual Basic or Visual Basic 2005
   Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
      If RichTextBox1.ReadOnly = True Then
         If (e.KeyValue = Keys.Control Or e.KeyValue = Keys.E _
             Or e.KeyValue = Keys.J Or e.KeyValue = Keys.L _
             Or e.KeyValue = Keys.R) Then
            'Set handled = true
            e.Handled = True
         End If
      End If
   End Sub
Visual C# or Visual C# 2005
      private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
      {
         // If Readonly Property is Set to True then only disable the keys
         if (richTextBox1.ReadOnly == true)
         {
            if (e.KeyValue == (int)Keys.Control || 
               e.KeyValue == (int)Keys.E || 
               e.KeyValue == (int)Keys.J || 
               e.KeyValue == (int)Keys.L || 
               e.KeyValue == (int)Keys.R)
            {
               // Set handled = true
               e.Handled = true;
            }
         }
      }

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows application by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.
  2. By default, Form1 is created.
  3. From the toolbox, drag a RichTextBox control to Form1.
  4. Right-click RichTextBox, and then click Properties.
  5. In the Properties window, locate ReadOnly, and then set the property to True.
  6. On the Build menu, click Start.
  7. RichTextBox1 text is displayed in the RichTextBox1 control.
  8. On the RichTextBox1 control, press the CTRL+R keys to right-align the text.

REFERENCES

For more information, visit the following Microsoft Web site:

RichTextBox Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsrichtextboxclasstopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbWindowsForms kbprb kbEditCtrl kbRichEdit kbCtrl kbControl KB814311 kbAudDeveloper