She*_*ani 2 c# controls winforms
我对TextBox控件添加到窗体的Controls属性的顺序有一个特殊的问题.
目前,我有这个功能:
public static bool IsValidate(System.Windows.Forms.Form Frm)
{
foreach (Control ctrl in Frm.Controls)
if (ctrl is TextBox)
// if (((TextBox)ctrl).AccessibleDescription == "Valid" && ((TextBox)ctrl).Text == string.Empty)
if (((TextBox)ctrl).AccessibleDescription == "Valid" && ((TextBox)ctrl).Text.Trim()== "")
{
MessageBox.Show(((TextBox)ctrl).AccessibleName + " Can't be Blank", Program.companyName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
((TextBox)ctrl).Focus();
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
但它正在随机迭代文本框,即使我已经设置了它们的标签索引.
所以我再次开发相同的表单并按顺序创建文本框.但是,当我将表单传递给此函数时,它会随机迭代文本框.
我想知道控件的任何属性是否允许我管理它们的流程.
小智 15
你可以很容易地做到.请根据表单中的tabindex使用以下语法和排序控件
foreach (Control control in this.Controls.Cast<Control>()
.OrderBy(c => c.TabIndex))
{
}
Run Code Online (Sandbox Code Playgroud)