1

I have a child form that I load from a parent form. The parent form is NOT a MDI parent. I would like to do the following:

I would like to disable the dotted/dashed rectangle around controls that have focus, particularly Buttons and RadioButtons.

Currently I am using the following code:

foreach (System.Windows.Forms.Control control in this.Controls)
{
    // Prevent button(s) and RadioButtons getting focus
    if (control is Button | control is RadioButton)
    {
        HelperFunctions.SetStyle(control, ControlStyles.Selectable, false);
    }
}

where my SetStyle method is

public static void SetStyle(System.Windows.Forms.Control control, ControlStyles styles,
    bool newValue)
{
    // .. set control styles for the form
    object[] args = { styles, newValue };

    typeof(System.Windows.Forms.Control).InvokeMember("SetStyle",
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
        null, control, args);
}

Needless to say, this does not seem to work. I am not sure what I am missing here. Any suggestions and/or advice would be greatly appreciated.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Zeos6
  • 273
  • 1
  • 7
  • 18

1 Answers1

2

Well, I think I have found sort of a solution. The solution resolves the focus rectangle issue but does not answer the question of what was wrong with the original code posted. This is my understanding of the rectangle issue....please feel free to correct me if I got any of this wrong.

It would appear that there are two types of dashed/dotted rectangles: rectangles that indicate that a control is focused, and rectangles that indicate that a control is the default control. Both of these situation require you to create a custom class for the control In my case the control of interest was a RadioButton. So here is the code:

public class NoFocusRadioButton: RadioButton
{
    // ---> If you DO NOT want to allow focus to the control, and want to get rid of
    // the default focus rectangle around the control, use the following ...

    // ... constructor
    public NoFocusRadioButton()
    {
        // ... removes the focus rectangle surrounding active/focused radio buttons
       this.SetStyle(ControlStyles.Selectable, false);
    }
}

or use the following:

public class NoFocusRadioButton: RadioButton
{
    // ---> If you DO want to allow focus to the control, and want to get rid of the
    // default focus rectangle around the control, use the following ...

    protected override bool ShowFocusCues
    {
        get
        {
            return false;
        }
    }
}

I am using the first (constructor code) approach to not allow input focus.

This all works great to solve the rectangle problem but I still don't understand why the initial code did not work.

Zeos6
  • 273
  • 1
  • 7
  • 18