Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I make a read-only TextBox ignore the MouseDown event so the user can't scroll the text or set the cursor?

You can do this by deriving the TextBox, overriding the WndProc method and ignoring these mouse down messages.

[C#]

public class CustomTextBox : TextBox
{
  protected override void WndProc( ref Message m )
  {     // WM_NCLBUTTONDOWN WM_LBUTTONDOWN
    if ( !( ReadOnly && (m.Msg == 0xa1 || m.Msg == 0x201) ) )
      base.WndProc( ref m );
  }
}

[Visual Basic]

Public Class CustomTextBox
  Inherits TextBox
  Protected Overrides Sub WndProc(ByRef m As Message)
    ' WM_NCLBUTTONDOWN WM_LBUTTONDOWN
    If Not (Me.ReadOnly AndAlso (m.Msg = &HA1 OrElse m.Msg = &H201)) Then
      MyBase.WndProc(m)
    End If
  End Sub
End Class

Contributed from George Shepherd's Windows Forms FAQ



Page view counter