在一个标签页上,有33个文本框,我想获取tabindex为13到33的文本框的值。我已将所有的tab索引设置为这些数字,但出于某种原因无法正常工作。
我目前拥有的代码:
int startTabIndex = 13;
int endTabIndex = 33;
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
if (ctrl.TabIndex == startTabIndex || ctrl.TabIndex <= endTabIndex)
{
MessageBox.Show(ctrl.Text);
}
}
}
MessageBox.Show(ctrl.Text)
只是一个测试,以查看它是否正常工作。
此外,可以使用 LINQ 实现吗?如何实现?
我改变了我的代码,使用 LINQ 获取文本框的值(名称和文本),根据 Steve 提供的答案。我应该如何检查这些值是否被传递(例如在消息框中显示文本或其他方式)?
int startTabIndex = 13;
int endTabIndex = 33;
System.Collections.Generic.Dictionary<string, string> dictionary = Controls.OfType<TextBox>()
.Where(t => t.TabIndex >= startTabIndex && t.TabIndex <= endTabIndex)
.Select(x => new System.Collections.Generic.KeyValuePair<string, string>(x.Name, x.Text))
.ToDictionary(z => z.Key, z => z.Value);
foreach (var d in dictionary)
{
MessageBox.Show(d.Value);
}
当我点击插入按钮时,没有任何反应。我有什么遗漏吗?
Steve 给了我一个很好的例子,它有效,但它从文本框的末尾开始(tabindex 33),然后向后处理到开头(tabindex 13)。如何才能颠倒这个顺序呢?