Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element's text by TabIndex in c# winform

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

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

Is it possible?

like image 630
DiA Avatar asked Dec 20 '22 19:12

DiA


1 Answers

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);
like image 81
Selman Genç Avatar answered Jan 03 '23 10:01

Selman Genç