通过c#winform中的TabIndex获取元素的文本
如何通过Windows窗体中的TabIndex获取元素的文本? 像:
"this.Controls.GetElementByTabindex(1).text"
可能吗?
是的, LINQ
:
var text = this.Controls.OfType() .Where(c => c.TabIndex == index) .Select(c => c.Text) .First();
如果你想用扩展方法做:
public static class MyExtensions { public static string GetElementTextByTabIndex(this Control.ControlCollection controls,int index) { return controls.OfType() .Where(c => c.TabIndex == index) .Select(c => c.Text).First(); } } string text = this.Controls.GetElementTextByTabIndex(1);
试试这个。
string tabText= tabControl1.SelectedTab.Text; MessageBox.Show(tabText);
如果您不想使用linq,可以这样做:
int index = 1; string text; foreach(Control control in Controls) { if(control.TabIndex == index) { text = control.Text; break; } }