【问题标题】:Find textBox, checkBox, any object by name C#按名称查找文本框、复选框、任何对象 C#
【发布时间】:2012-06-11 18:29:50
【问题描述】:

这真的很奇怪,但我似乎无法在 .NET CF 中找到特定的 textBox(i) 或 checkBox(i)。在 .NET 3.5 中我可以创建这个函数:

void checking(int input)
{
    CheckBox checkbox = (CheckBox)this.Controls["checkBox" + input.toString()];
    if(checkbox.isChecked)
      //do something here
}

在本例中,它获取复选框的名称(即 checkBox1、checkBox2 等)。

但是在 WINCE6 的 .NET CF 3.5 中,它不断告诉我我需要在 Controls[] 中创建一个索引,因为它无法将字符串转换为 int。有谁知道如何在不使用该 foreach 语句的情况下找到特定对象?那个 foreach 很有用,但不是因为它循环遍历所有的复选框。由于我是基于 ARM 开发的,所以速度就是一切。我正在使用 VS2008 C# 开发桌面和移动应用程序。

感谢阅读!

【问题讨论】:

    标签: c# object arm windows-ce


    【解决方案1】:

    下面将循环使用 10 个用作评级星的图片框,在我的例子中将它们从灰色变为蓝色。 PictureBox 以下列约定命名,pbStarX。其中 X 是数字 1-10。例如:pbStar1、pbStar2、pbStar3 等...

    注意:使用 c#.Net VS 2010

    for (int x = 1; x <= 10; x++)
    {
        PictureBox pb = (PictureBox)this.Controls.Find("pbStar" + x, true)[0];
        pb.Image = MyProject.Properties.Resources.star_blue;
    }
    

    在使用 c#.Net Compact Framework 时可能会替代

    private Control FindControl(Control parent, string ctlName)
    {
        foreach(Control ctl in parent.Controls)
        {
            if(ctl.Name.Equals(ctlName))
            {
                return ctl;
            }
    
            FindControl(ctl, ctlName);                     
        }
        return null;
    }
    

    像这样使用上面的函数...

    Control ctl = FindControl(this, "btn3");
    if (ctl != null)
    {
        ctl.Focus();
    }
    

    【讨论】:

    • 感谢您的回复,但 .NET CF 中不存在 Find。我知道如何在 .NET 中使用它,但看起来 .NET CF 出于某种原因删除了此选项。
    • 上面添加的替代方法 (foreach) 对您根本不起作用吗?
    • 说实话,那个项目很久以前就被放弃了。但是,很高兴知道答案。我正在转向 android/cocoa...所以 .NETCF 对我来说有点死。
    • 不错的功能,但代码有一个错误:FindControl(ctl, ctlName);应该是:var returnedControl = FindControl(ctl, ctlName); if (returnedControl != null) { return returnedControl; }
    【解决方案2】:

    您正在使用 un integer 索引器,应将 un integer 传递给它以检索对象。 试试这样的:

    void checking(int input) 
    { 
        CheckBox checkbox = (CheckBox)this.FindControl("checkBox" + input.toString()); 
        if(checkbox.isChecked) 
          //do something here 
    } 
    

    这样你就可以通过 id 找到控制权

    【讨论】:

    • 这不是网络应用,我认为System.Windows.Forms.Control 没有名为FindControl 的方法。
    • Groo 是正确的。没有 FindControl。
    • 到目前为止,我正在使用 PITA hack 来获得我需要的东西。我几乎用过:
      int index = this.Controls.Indexof(checkbox1); CheckBox checkbox = (CheckBox)this.Controls[index-input];
      因为 checkbox1 的索引是 20,所以 checkbox2 是 19, checkbox3 是 18...等等。只要我正确索引该死的东西,这种方法就可以工作。如果我不小心转到不是 CheckBox 类型的索引,则会引发异常。
    【解决方案3】:

    它应该可以工作,但您也可以使用

    CheckBox checkbox = (CheckBox)this.Controls.Find("checkBox" + input.toString())[0];
    

    【讨论】:

    • 感谢您的回复阿西夫。但是,它告诉我 System.Windows.Forms.Control.ControlCollection 不包含“查找”的定义。
    【解决方案4】:

    我的朋友们很好用 :) 请试试这个方法

    bool chkValue;
    string chkName="checkbox1";
    CheckBox myCheckBox = this.Controls.Find(chkName, true).First() as CheckBox;
    chkValue = myCheckBox.Checked;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-19
      • 2022-11-27
      • 2020-04-19
      • 2013-01-08
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多