查找文本框中复选框,按名称C#的任何对象 [英] Find textBox, checkBox, any object by name C#

查看:282
本文介绍了查找文本框中复选框,按名称C#的任何对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很奇怪,但我似乎无法找到.NET CF的特定的textBox(i)或复选框(I)在.NET 3.5我可以创建这个功能:

This is really weird, but I can't seem to find a specific textBox(i) or checkBox(i) in .NET CF. In .NET 3.5 I can create this function:

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

在这个例子中,它得到的复选框的名称(即checkBox1,checkBox2等)。

In this example, it gets the name of the checkBox (i.e checkBox1, checkBox2, etc).

然而,在.NET CF 3.5 WINCE6,它不断告诉我,我需要控制里面[]的索引,因为它不能将字符串转换为int。有谁知道如何找到特定的对象,而不使用foreach语句?因为它遍历所有的复选框,这是的foreach有用的,但不是这个。由于我在基于ARM的发展,速度就是一切。我使用VS2008 C#开发桌面和移动应用。

However in .NET CF 3.5 for WINCE6, it keeps on telling me that I need an index inside Controls[] because it can't convert string to int. Does anyone know how to find the specific object without using that foreach statement? That foreach is useful but not for this because it loops through all the checkBoxes. Since I'm developing in ARM based, speed is everything. I'm using VS2008 C# to develop a desktop and mobile app.

感谢您阅读!

推荐答案

通过作为评价星10 PictureBoxs下面将循环在我的情况下,从灰色到蓝色的改变它们。的PictureBoxs在以下约定,pbStarX命名。其中X是一个数字1-10。例如:pbStar1,pbStar2,pbStar3,等等...

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...

注:使用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的

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();
}

这篇关于查找文本框中复选框,按名称C#的任何对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆