我正在尝试创建一个循环,循环遍历我的groupbox中的所有控件,查找其中包含文本的每个控件,并将tabstop属性设置为false。但是,一些控件的tabstop属性必须始终为true,即使控件中有文本。
这是我的代码:
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。
我做错了什么?
发布于 2013-06-17 16:50:08
我猜这行代码
if (!string.IsNullOrEmpty(c.Text))正在为您不希望将TabStop设置为false的控件执行,并且该控件当前包含一些文本。
要解决此问题,请按如下方式重新排序测试:
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;
}
}
}你可以将其简化为:
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);
}
}
}https://stackoverflow.com/questions/17143508
复制相似问题