Live Chat Icon For mobile
Live Chat Icon

How do I prevent a control from receiving focus when it receives a mouseclick

Platform: WinForms| Category: TextBox

You can set the control’s Enabled property to false. This will prevent clicking but also gives the disabled look.

There is a ControlStyles.Selectable flag that determines whether the control can get focus or not. But it does not appear to affect some controls such as TextBox. But you can prevent the TextBox from getting focus by overriding its WndProc method and ignoring the mouse down.

public class MyTextBox : TextBox
{
	const int WM_LBUTTONDOWN = 0x0201;
  
	protected override void WndProc(ref System.Windows.Forms.Message m)
	{
		if(m.Msg == WM_LBUTTONDOWN)
			return;
		base.WndProc(ref m);
	}
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

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

Please submit your question and answer.