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

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 RichTextBoxcontrol 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 RichTextBox1control.
  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://msdn2.microsoft.com/en-us/library/system.windows.forms.richtextbox(vs.71).aspx
Propriedades

ID do Artigo: 814311 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários