Get element's text by TabIndex in c# winform

有些话、适合烂在心里 提交于 2019-12-02 01:14:06

问题


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

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

Is it possible?


回答1:


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);



回答2:


try this.

   string tabText= tabControl1.SelectedTab.Text;
   MessageBox.Show(tabText);



回答3:


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;
    }
 }


来源:https://stackoverflow.com/questions/21300277/get-elements-text-by-tabindex-in-c-sharp-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!