c# - Winforms RadPageView 找控件

标签 c# winforms dynamic telerik tabcontrol

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

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

    }

最佳答案

我在 Internet 上找到了这个示例,它运行良好。

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

关于c# - Winforms RadPageView 找控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538452/

相关文章:

c# - 你能有太多的用户控件吗

C# - 在动态生成的程序集中引用类型

c# - 如何将数据从一个 UnmanagedMemoryStream 复制到另一个

r - 基于用户输入 Shiny 的 ggplot 突出显示区域

c# - 是否有为 C# 程序创建配置文件的标准方法?

c# - 如何在 LinqToSQL 中搜索表?

c# - 我什么时候会在 using 指令上使用别名?

c# - Access-Control-Allow-Headers 不允许请求 header 字段 x-user-session

c# - 计算不同的结果-方法 PointToScreen 获取桌面(屏幕)相关控件的点

.net - *tag* 属性在 C# 中有什么用