Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Focus Rectangle not shown until Tab key pressed?

I have a weird problem (probably only to understand) why in a test app the focus rect is not shown until i press the tab key.

I want to show a dialog with two radioboxes and two buttons. When i display the dialog, i'd like to see a focus rect around my first radiobutton. (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. (radiobox 1 has tabindex 0, ...)

When i show the dialog the first radiobox has the focus, but it has no focus rect around it. (Until i press tab key.)

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

Can someone give me a hint what i am doing wrong?

Sorry, here is the code of my sample:

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
like image 571
AlexS Avatar asked Feb 10 '12 10:02

AlexS


3 Answers

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

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

Public Class RadioWithFocus
  Inherits RadioButton

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

End Class
like image 186
LarsTech Avatar answered Nov 10 '22 21:11

LarsTech


Use the inherited Control.Focus() in the initialization method of your form, or wherever applicable. Something like:

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

Another method to look at is Form.Activate. This is probably better to use in this context

Also, ActiveControl might be helpful.

like image 1
annonymously Avatar answered Nov 10 '22 20:11

annonymously


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);
like image 1
Martin Connell Avatar answered Nov 10 '22 20:11

Martin Connell