Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在所有控件中循环[MaterialSkin](C#)_C#_Foreach_Material Design - Fatal编程技术网

在所有控件中循环[MaterialSkin](C#)

在所有控件中循环[MaterialSkin](C#),c#,foreach,material-design,C#,Foreach,Material Design,我正在制作一个通用软件,并在其中添加了一些标签(我不能告诉更多..xD) 所以,我想遍历表单中的每个控件或至少所有标签。。。我试过了 foreach (Control c in this.Controls) { //item.Font.Size = 11f; c.ForeColor = Color.White; } 但我认为它只循环到MaterialSkin.Controls.MaterialTabControl 因为当我试着用MaterialLabel.等运行任何代码时

我正在制作一个通用软件,并在其中添加了一些标签(我不能告诉更多..xD) 所以,我想遍历表单中的每个控件或至少所有标签。。。我试过了

foreach (Control c in this.Controls)
{
    //item.Font.Size = 11f;
    c.ForeColor = Color.White;
}
但我认为它只循环到
MaterialSkin.Controls.MaterialTabControl

因为当我试着用MaterialLabel.等运行任何代码时

    foreach (MaterialLabel c in this.Controls)
    {
        //item.Font.Size = 11f;
        c.ForeColor = Color.White;
    }
它告诉我

Additional information: Unable to cast object of type 'MaterialSkin.Controls.MaterialTabControl' to type 'MaterialSkin.Controls.MaterialLabel'.
我需要帮助请帮帮我:) 我不是一个很好的程序员。。。所以请帮忙


现在,在更改任何属性之前,先播放Left 4 Dead xD

,检查它是否为
MaterialLabel
类型

foreach (Control c in this.Controls)
{
    if(c.GetType()==typeof(MaterialLabel))
    {
         c.Font = new Font(c.Font, FontStyle.Bold);
         c.ForeColor = Color.White;
    }
}
或者正如拉尔斯泰克所说:

foreach (MaterialLabel c in this.Controls.OfType<MaterialLabel>())
{
     c.Font = new Font(c.Font, FontStyle.Bold);
    c.ForeColor = Color.White;
}
foreach(this.Controls.OfType()中的MaterialLabel c)
{
c、 Font=新字体(c.Font,FontStyle.Bold);
c、 前景色=颜色。白色;
}
选项很少

foreach (var c in this.Controls)
{
    if(c is MaterialLabel)
    {
         var i = (MaterialLabel)c;
         i.Font = new Font(c.Font, FontStyle.Bold);
         i.ForeColor = Color.White;
    }
}

//最干净
foreach(this.Controls.OfType()中的var c)
{
c、 Font=新字体(c.Font,FontStyle.Bold);
c、 前景色=颜色。白色;
}

foreach(MaterialLabel c在这个.Controls.OfType()中)
我想它甚至没有到达其他控件。。。它在Tabcontrol上结束,就这样。。。它不适用于循环,但如果我正常使用它,它也可以工作。您能否告诉我如何将其加粗,因为当我使用
c.Font.bold=true它表示无法分配,因为它是readonly@RajaBilal我编辑了我的答案。请看一下我告诉过你它不起作用了。。。代码不起作用。。。请再读一遍我的评论,我告诉过你我已经改变了答案<代码>item.Font=新字体(item.Font,FontStyle.Bold)
c.GetType()==typeof(MaterialLabel)
不是进行类型检查的好方法,这些方法中没有一种有效。。。是因为我使用的是自定义字体,当我默认设置颜色时,它会自动变为黑色。。。即使是文件+已编译的exe,但当我在文件中执行时,比如
conditionlbl.ForeColor=Color.White它可以工作。。。但它不是通过循环工作,也不是默认设置。我使用此处提供的方法使用自定义字体…这也可能是因为我使用的是材质标签,而它不存在于系统的控制库中。。。它们位于选项卡控件内和2到3个面板内。。。唯一能得到的是tab控件。。。不在里面,我是说在那些面板里面。。。
foreach (var c in this.Controls)
{
    //slightly faster than the first version but won't work with struct
    var i = c as MaterialLabel; 
    if(i != null)
    {
         i.Font = new Font(c.Font, FontStyle.Bold);
         i.ForeColor = Color.White;
    }
}
//cleanest
foreach (var c in this.Controls.OfType<MaterialLabel>())
{
     c.Font = new Font(c.Font, FontStyle.Bold);
     c.ForeColor = Color.White;
}