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.
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:
or use the following:
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.