首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环访问groupbox中的控件并设置Tabstop属性

循环访问groupbox中的控件并设置Tabstop属性
EN

Stack Overflow用户
提问于 2013-06-17 16:41:44
回答 1查看 122关注 0票数 0

我正在尝试创建一个循环,循环遍历我的groupbox中的所有控件,查找其中包含文本的每个控件,并将tabstop属性设置为false。但是,一些控件的tabstop属性必须始终为true,即使控件中有文本。

这是我的代码:

代码语言:javascript
复制
    foreach (Control c in deliveryGroup.Controls)
    {
        if (c is Label || c is Button)
        {
            c.TabStop = false;
        }
        else
        {
            if (!string.IsNullOrEmpty(c.Text))
            {
                c.TabStop = false;
            }
            else if (c.Name == "cmbPKPAdrID")
            {

            }
            else if (c.Name.ToString() == "cmbPKPType")
            {
                c.TabStop = true;  <<------- never enters here
            }
            else if (c.Name.ToString() == "dtpPKPDate")
            {
                c.TabStop = true;  <<------- never enters here
            }
            else
            {
                c.TabStop = true;
            }
        }
    }

我的问题是我的程序可以运行,但从来不会运行到我用箭头标记的代码中。它跳出来并将tabstop属性设置为false,即使我希望它在控件具有特定名称时将其设置为true。

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-06-17 16:50:08

我猜这行代码

代码语言:javascript
复制
if (!string.IsNullOrEmpty(c.Text))

正在为您不希望将TabStop设置为false的控件执行,并且该控件当前包含一些文本。

要解决此问题,请按如下方式重新排序测试:

代码语言:javascript
复制
foreach (Control c in deliveryGroup.Controls)
{
    if (c is Label || c is Button)
    {
        c.TabStop = false;
    }
    else
    {
        if (c.Name == "cmbPKPAdrID")
        {

        }
        else if (c.Name == "cmbPKPType")
        {
            c.TabStop = true;
        }
        else if (c.Name == "dtpPKPDate")
        {
            c.TabStop = true;
        }
        else if (!string.IsNullOrEmpty(c.Text))
        {
            c.TabStop = false;
        }
        else
        {
            c.TabStop = true;
        }
    }
}

你可以将其简化为:

代码语言:javascript
复制
foreach (Control c in deliveryGroup.Controls)
{
    if (c is Label || c is Button)
    {
        c.TabStop = false;
    }
    else
    {
        if (c.Name == "cmbPKPAdrID")
        {

        }
        else if (c.Name == "cmbPKPType")
        {
            c.TabStop = true;
        }
        else if (c.Name == "dtpPKPDate")
        {
            c.TabStop = true;
        }
        else
        {
            c.TabStop = string.IsNullOrEmpty(c.Text);
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17143508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档