Removing focus from all objects in Visual Basic 6

2019-07-17 20:26发布

问题:

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!

回答1:

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).



回答2:

When a form is active, something generally HAS to have focus. It sounds like you're just wanting to not "show" that a particular control has focus.

If that's the case, it's going to depend on the controls. Some have properties that control whether or not the specific control "indicates" its focus in some way.

But the built in Windows controls will always show their focus state unless you subclass them

Given this problem. I'd probably put a button on the form , then move it offscreen when the form loads. Make sure it's not a tab stop, but then when you want to hide focus, set focus specifically to the button, make sure the button is STILL in the tab order, even though it's not a tab stop, so the user can press tab while on the button and end up somewhere logical.



回答3:

Don't have VB handy, but could you simply remove TabStop?

for x = 1 to me.Controls.count
    me.Controls(x).TabStop = 0
next