将GroupBox中的所有控件设为只读? [英] Make all controls inside a GroupBox read-only?
问题描述
我知道许多单独的控件都有 ReadOnly
属性。但是假设我有一个 GroupBox
,其中有许多不同的控件(文本框,组合框,单选按钮等..),是否可以设置<$ c所有这些控件的$ c> ReadOnly 属性?
I know that many individual controls have a ReadOnly
property. But suppose I have a GroupBox
in which there are many different controls (text boxes, combo boxes, radio buttons, etc ..), is it possible to set the ReadOnly
property for all these controls together?
不是我只想为特定 GroupBox
中的控件设置此属性(因为我有多个GroupBox)也是如此,所以我不希望对其他GroupBoxes中的控件进行设置。.
Not that I only want to set this property for the controls within a specific GroupBox
(because I have multiple GroupBoxes too, so I don't want the setting to be done for controls in other GroupBoxes) ..
手动设置 ReadOnly
属性似乎非常令人昏昏欲睡,因为每个 Groupbox
中有多达20个控件(不要问为什么:p)。
Manually setting the ReadOnly
property seems very lethargic, as I have as many as 20 controls in each Groupbox
(don't ask why :p).
推荐答案
对于Winform中的标准控件,您可以使用类似这样的内容( TextBoxBase 是 ReadOnly 属性控件):
For standart controls in Winform you can use something like this (TextBoxBase is base class for ReadOnly properties controls) :
private void button1_Click(object sender, EventArgs e)
{
SetReadonlyControls(groupBox1.Controls);
}
private void SetReadonlyControls(Control.ControlCollection controlCollection)
{
if (controlCollection == null)
{
return;
}
foreach (TextBoxBase c in controlCollection.OfType<TextBoxBase>())
{
c.ReadOnly = true;
}
}
这篇关于将GroupBox中的所有控件设为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!