Live Chat Icon For mobile
Live Chat Icon

How can I make a ReadOnly TextBox ignore the mousedowns so that you cannot scroll the text or set the cursor

Platform: WinForms| Category: TextBox

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

[C#]
public class MyTextBox : TextBox
{
	protected override void WndProc(ref System.Windows.Forms.Message m)
	{	// WM_NCLBUTTONDOWN  WM_LBUTTONDOWN 
		if(this.ReadOnly && (m.Msg ==  0xa1 || m.Msg == 0x201))
		{
			return; //ignore it
		}
		base.WndProc(ref m);
	}
}
[VB.NET]
Public Class MyTextBox
	Inherits TextBox
    
	Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
		’ WM_NCLBUTTONDOWN  WM_LBUTTONDOWN 
		If Me.ReadOnly AndAlso(m.Msg = &HA1 OrElse m.Msg = &H201) Then 
			Return ’ignore it
		End If
		MyBase.WndProc(m)
	End Sub ’WndProc
End Class ’MyTextBox

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.