Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
C# 按名称查找特定控件并修改其.Value属性_C#_Winforms_Controls - Fatal编程技术网

C# 按名称查找特定控件并修改其.Value属性

C# 按名称查找特定控件并修改其.Value属性,c#,winforms,controls,C#,Winforms,Controls,我发现了一些关于修改.Text、.Checked值等的答案,但在我尝试更改.Value属性时,这些答案都不起作用。我不能让它在进度条上工作。 上次我试过: foreach (Control c in this.Controls) { if (c.Name == "test" && c is ProgressBar) { ((ProgressBar)c).Value = 23; } } 我是否缺少using语句或其他内容?假设您的progr

我发现了一些关于修改.Text、.Checked值等的答案,但在我尝试更改.Value属性时,这些答案都不起作用。我不能让它在进度条上工作。 上次我试过:

foreach (Control c in this.Controls)
{
    if (c.Name == "test" && c is ProgressBar)
    {
        ((ProgressBar)c).Value = 23;
    }
}

我是否缺少using语句或其他内容?

假设您的progressbar控件名为“test”(所有小写字母),并且直接放置在表单的表面上(而不是在groupbox、panel或其他控件容器中),那么此代码应该可以工作并简化您的工作

foreach (var c in this.Controls.OfType<ProgressBar>().Where(x => x.Name == "test") 
{
   c.Value = 23;
}

这里的技巧是控件不是列表或IEnumerable,而是控件集合

我建议使用扩展控件。将此类添加到项目中:

public static class ControlExtensionMethods
{
    public static IEnumerable<Control> All(this System.Windows.Forms.Control.ControlCollection controls)
    {
        foreach (Control control in controls)
        {
            foreach (Control grandChild in control.Controls.All())
                yield return grandChild;

            yield return control;
        }
    }
}

来源:

代码看起来不错。你能拿到progressBar吗?通过放置断点检查。也考虑看。我放置了另一个语句,那就是运行的语句。因此,我的if中的条件不满足。。我不明白为什么“这”是什么?可能您将ProgressBar放在不同的控件中(例如面板)?我很确定您有一些嵌套的容器,这意味着
ProgressBar
不直接包含在表单控件中,它可能包含在另一个容器中。因此,使用Soner Gonul建议的
Find
方法是最好的选择。Odrai是对的,我把条放在一个分组框中。你的例子也很好用。标记为已接受答案,因为它是第一个发布在“答案”列上的答案。如果您发现其他答案有帮助,您也可以对其进行投票。这是向您表示感谢的一种方式。顺便说一句,您不需要通过像
ControlCollection
这样的键支持集合进行任何循环,只需在
ControlCollection
类的索引器中传递控件名,如果该键存在,您应该获得控件,否则您将获得
null
,像这样
panel1.Controls[“test”]
我不太理解你的最后一条评论,因为我还不太了解ControlCollection。如果你能提供一个有用的编辑后,你已经张贴,这将是cool@KingKing就在这里。如果您完全确定groupbox中存在名为“test”的控件,则在controls集合中进行简单的查找将生成progressbar。在这种情况下,循环是不必要的,我可能不需要那么多的通用性。你给了我错误的第一个提示,但是史蒂夫的回答是第一个,这稍微容易一点。非常感谢你。投票作为useful@Vantalk:我很高兴能帮助你:)
ProgressBar pb = this.groupBox1.Controls["test"] as ProgressBar;
if(pb != null) pb.Value = 23;
public static class ControlExtensionMethods
{
    public static IEnumerable<Control> All(this System.Windows.Forms.Control.ControlCollection controls)
    {
        foreach (Control control in controls)
        {
            foreach (Control grandChild in control.Controls.All())
                yield return grandChild;

            yield return control;
        }
    }
}
foreach(var textbox in this.Controls.All())
{
    // Apply logic to a control
}