循环遍历组框中的控件并设置制表符属性
本文关键字:设置 制表符 属性 控件 遍历 循环 | 更新日期: 2023-09-27 18:18:45
我试图创建一个循环,通过我的组框中的所有控件循环,并找到每个控件确实有文本在其中,并设置tabstop属性为false。但是有些控件的tabstop属性必须总是为真,即使控件中有文本。
这是我的代码:
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。
我做错了什么?
我猜这行代码
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);
}
}
}