繁体   English   中英

Winforms RadPageView查找控件

[英]Winforms RadPageView find control

我在Winform应用程序上的RadPageView控件内嵌套了控件。 RadPageView有一个子RadPageViewPage。 这两个控件都在窗体上,但是选项卡控件和该选项卡控件内部是动态添加的其他一些控件。 单击按钮,如何在动态生成的选项卡控件内查找和更改Textbox的值。

public Form1()
    {
        InitializeComponent();

        TabControl tb = new TabControl();
        tb.Width = 500;
        TabPage tp = new TabPage("Tab 1");

        Label lb = new Label();
        lb.Text = "Test";
        lb.Location = new Point(10, 10);

        TextBox txt = new TextBox();
        txt.Text = "Textbox";
        txt.Location = new Point(200, 10);

        tp.Controls.Add(lb);
        tp.Controls.Add(txt);

        tb.Controls.Add(tp);

        radPageViewPage1.Controls.Add(tb);


    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

我已经在互联网上找到了这个示例,它运行良好。

public Form1()
    {
        InitializeComponent();

        TabControl tb = new TabControl();
        tb.Width = 500;
        TabPage tp = new TabPage("Tab 1");

        Label lb = new Label();
        lb.Text = "Test";
        lb.Name = "lblTest";
        lb.Location = new Point(10, 10);

        TextBox txt = new TextBox();
        txt.Text = "Textbox";
        txt.Name = "txtName";
        txt.Location = new Point(200, 10);

        tp.Controls.Add(lb);
        tp.Controls.Add(txt);

        tb.Controls.Add(tp);

        radPageViewPage1.Controls.Add(tb);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        var crl = FindControl("txtName");
        MessageBox.Show(crl.Text);
    }

    Control FindControl(string target)
    {
        return FindControl(this, target);
    }

    static Control FindControl(Control root, string target)
    {
        if (root.Name.Equals(target))
            return root;
        for (var i = 0; i < root.Controls.Count; ++i)
        {
            if (root.Controls[i].Name.Equals(target))
                return root.Controls[i];
        }
        for (var i = 0; i < root.Controls.Count; ++i)
        {
            Control result;
            for (var k = 0; k < root.Controls[i].Controls.Count; ++k)
            {
                result = FindControl(root.Controls[i].Controls[k], target);
                if (result != null)
                    return result;
            }
        }
        return null;
    }

暂无
暂无

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

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