检测动态创建的控件是否具有手动设置的任何属性

时间:2018-10-28 22:52:12

标签: c# properties controls

我有一个正在动态创建控件/对象的应用程序。其中一些控件是我手动设置的某些属性。如您所见,我手动将CheckBox1,CheckBox3和CheckBox5复选框的Checked属性设置为false。在我的ForEach循环中,我会基于默认设置来设置某些属性,前提是该属性先前未明确定义。那么,有没有办法让我确定自己是否明确拥有?

 List<Object> controlList = new List<Object>
{
    new CheckBox() { Name = "Checkbox 1", Text = "Checkbox 1", Checked = false },
    new CheckBox() { Name = "Checkbox 2", Text = "Checkbox 2" },
    new CheckBox() { Name = "Checkbox 3", Text = "Checkbox 3" , Checked = false},
    new CheckBox() { Name = "Checkbox 4", Text = "Checkbox 4" },
    new CheckBox() { Name = "Checkbox 5", Text = "Checkbox 5", Checked = false}
};

foreach (var control in controlList)
{
    CheckBox checkBox = ((CheckBox)control);

    // Set Default Checked Property to true unless it was explicity set to false

    gbSource.Controls.Add(checkBox);
}

0 个答案:

没有答案