Removing focus from all objects in Visual Basic 6

erekalper picture erekalper · Feb 22, 2011 · Viewed 7.5k times · Source

Is there a method such that a user can click on the form itself, and in doing so remove focus from whatever object (textbox, combobox, etc) currently has it? Basically, can focus be uniformly removed from everything at once?

Setting the focus to the form itself does not work.

I thought about doing the old "hide a placeholder button behind another object" trick, but I'm really not a fan of that.

Thanks!

Answer

wqw picture wqw · Feb 23, 2011

In VB6 a PictureBox can get focus, even if it does not contain any control.

In your case you can put a PictureBox with TabStop false, BorderStyle set to 0, TabIndex set to 0 behind every other control but not containing any focusable control and stretch it to ScaleWidth by ScaleHeight at run-time.

You have to put the labels and any windowless control in this background PictureBox too.

This way when the user clicks "on the form" the focus will "go away". With "no focus" Tab key will focus first control (the one with TabIndex set to 1).