简体   繁体   English

在标签页中找到ListView控件

[英]Find a ListView control in tab page

I have 2 listviews in tab page. 我在标签页中有2个listview。 however I am looking for a function to find the right control by name: 但是我正在寻找一个函数来按名称查找正确的控件:

I have 我有

foreach (Control c in form.Controls) // loop through form controls
{
    if (c is TabControl)
    {
        TabControl f = (TabControl)c;

        foreach (Control tab in f.Controls)
        {
            TabPage tabPage = (TabPage)tab;

            foreach (Control control in tabPage.Controls)
            {
                MessageBox.Show(control.Name);

                // code to go here
            }
        }     
    }
}

The Controls collection has a Find function that returns an array: Controls集合具有一个Find函数,该函数返回一个数组:

Control[] ctrls = this.Controls.Find("listView1", true);
if (ctrls.Length == 1) {
  MessageBox.Show("Found " + ctrls[0].Name);
}

This will search for a control with the specified name in the controls of the specified control and all his sons. 这将在指定控件及其所有儿子的控件中搜索具有指定名称的控件。

public Control findControlbyName(String name, Control parent){
    foreach (Control ctr in parent.Controls)
    {
        if (ctr.Name.Equals(name)) return ctr;
        else return findControlbyName(name, ctr);
    }
    return null;
}

You just need to do: 您只需要做:

findControlbyName("NameOfTheListView",this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM