0

这真的很奇怪,但我似乎无法在 .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# 开发桌面和移动应用程序。

谢谢阅读!

4

4 回答 4

2

The following will cycle through 10 PictureBoxs used as rating stars changing them from gray to blue in my case. The PictureBoxs are named in the following convention, pbStarX. Where X is a number 1-10. Ex: pbStar1, pbStar2, pbStar3, etc...

Note: Using 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;
}

Alternative maybe when using 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;
}

Use the above function like this...

Control ctl = FindControl(this, "btn3");
if (ctl != null)
{
    ctl.Focus();
}
于 2013-10-05T03:44:59.437 回答
1

您正在使用 un integer indexer 并且应该将 un integer 传递给它以检索对象。尝试这样的事情:

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

这样,您将通过 id 找到控制权

于 2012-06-11T18:36:07.640 回答
1

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

CheckBox checkbox = (CheckBox)this.Controls.Find("checkBox" + input.toString())[0];
于 2012-06-11T18:38:25.877 回答
0

它的工作我的朋友:) 请尝试这种方式

bool chkValue;
string chkName="checkbox1";
CheckBox myCheckBox = this.Controls.Find(chkName, true).First() as CheckBox;
chkValue = myCheckBox.Checked;
于 2021-06-30T11:53:18.720 回答