Get element's text by TabIndex in c# winform

2019-02-24 23:43发布

How to get element's text by TabIndex in Windows Forms? smth like:

"this.Controls.GetElementByTabindex(1).text"

Is it possible?

3条回答
Viruses.
2楼-- · 2019-02-25 00:23

try this.

   string tabText= tabControl1.SelectedTab.Text;
   MessageBox.Show(tabText);
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-25 00:23

In case you don't want to use linq, this can do this:

int index = 1;    
string text;

foreach(Control control in Controls)
{
    if(control.TabIndex == index)
    {
        text = control.Text;
        break;
    }
 }
查看更多
Juvenile、少年°
4楼-- · 2019-02-25 00:25

Yes, it is possible with LINQ:

var text = this.Controls.OfType<Control>()
               .Where(c => c.TabIndex == index)
               .Select(c => c.Text)
               .First();

If you want to do it with extension method:

public static class MyExtensions
{
    public static string GetElementTextByTabIndex(this Control.ControlCollection controls,int index)
    {
        return controls.OfType<Control>()
                       .Where(c => c.TabIndex == index)
                       .Select(c => c.Text).First();
    }
}

string text = this.Controls.GetElementTextByTabIndex(1);
查看更多
登录 后发表回答