C# 窗口窗体控制样式,通过样式更改删除焦点
我有一个从父表单加载的子表单。父窗体不是 MDI 父窗体。 我想执行以下操作:
我想禁用具有焦点的控件周围的点线/虚线矩形,特别是按钮和单选按钮。
目前我正在使用以下代码:
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);
}
}
其中我的 SetStyle 方法
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);
}
不用说,这似乎不起作用。我不确定我在这里缺少什么。任何建议和/或意见将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我想我已经找到了某种解决方案。该解决方案解决了焦点矩形问题,但没有回答发布的原始代码有什么问题的问题。这是我对矩形问题的理解......如果我有任何错误,请随时纠正我。
看起来有两种类型的虚线/点线矩形:指示控件获得焦点的矩形,以及指示控件是默认控件的矩形。这两种情况都要求您为控件创建自定义类。在我的例子中,感兴趣的控件是 RadioButton。所以这里是代码:
或使用以下内容:
我使用第一种(构造函数代码)方法不允许输入焦点。
这一切都很好地解决了矩形问题,但我仍然不明白为什么初始代码不起作用。
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.