Why is the Focus Rectangle not showing until the Tab key is pressed?

I have a weird problem (perhaps just for understanding) why in the test application, the rect focus is not displayed until I press the Tab key.

I want to show a dialog with two radio boxes and two buttons. When I show the dialogue, I would like to see the focus around my first radio drum. (So ​​that the user can see where the focus is.) I ordered the controls and set the tabindex property from 0 to 4 so that they are in the correct order. (radobox 1 has tabindex 0, ...)

When I show the dialogue, the first radio lens has focus, but it does not focus around it. (Until I press the tab key.)

I created a completely empty winforms project (Visual Studio 2010), added controls, and started it. So there is nothing special.

Can someone tell me what I'm doing wrong?

Sorry, here is the code for my example:

Public Class Form1

Private Sub Button1_Click(sender As System.Object, _
  e As System.EventArgs) _
    Handles Button1.Click

    Me.Close()
End Sub

Private Sub Button2_Click(sender As System.Object, _
  e As System.EventArgs) _
    Handles Button2.Click

    Me.Close()
End Sub

Private Sub Form1_Shown(sender As Object, _
   e As System.EventArgs) _
  Handles Me.Shown

    RadioButton1.Focus()
    RadioButton1.Select()
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("+{TAB}")

End Sub

End Class
+5
source share
4 answers

As I said, this is really a user preference setting.

But to show the rectangle, try to inherit your own RadioButton and override the function ShowFocusCues:

Public Class RadioWithFocus
  Inherits RadioButton

  Protected Overrides ReadOnly Property ShowFocusCues() As Boolean
    Get
      Return True
    End Get
  End Property

End Class
+5
source

Use the inherited Control.Focus()in the initialization method of your form or where applicable. Sort of:

public Form1 () {
    //Other stuff here
    radiobox1.Focus();// If this is the name of your control
}

Another way to view is Form.Activate. This is probably best used in this context.

Also may be helpful ActiveControl.

+1

As LarsTech said, this is a custom setting. For Windows 7, this option can be found here: Open the control panel / simplicity of the access center / simplify the use of the keyboard.

Find the checkbox with the text “Underline keyboard shortcuts and access keys” and check it.

Repeat your application and you will see that questions with focus will now be displayed when your form is loaded.

Enjoy

0
source

For Win32 / C ++, send the WM_CHANGEUISTATE message to the parent window:

    // Enable focus rect and accelerator underline in all controls.
    ::SendMessage(WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEACCEL | UISF_HIDEFOCUS), 0);
0
source

All Articles