简体   繁体   English

将用户控件添加到TabPage

[英]Adding a user control to a TabPage

I create a TabPage and add on the TabPage a user control. 我创建一个TabPage并在TabPage上添加一个用户控件。

private void AddScriptTab(TabControl tab_contr, string tab_name, int idx)
    {
        TabPage tab_page = new TabPage();
        UserControlScript user_contr = new UserControlScript(serial_port, ref tabs);
        tab_contr.TabPages.Insert(idx, tab_page);
        tab_page.Text = tab_name;
        tab_page.Tag = "script";
        user_contr.Dock = DockStyle.Fill;
        user_contr.Tag = idx.ToString();
        tab_page.Controls.Add(user_contr);
    }

Now in the created TabPage I click on the button in my user control and check the tag I assigned 现在,在创建的TabPage中,单击用户控件中的按钮并检查分配给我的标签

user_contr.Tag = idx.ToString(); user_contr.Tag = idx.ToString();

 private void buttonParamsLoad_Click(object sender, EventArgs e)
  {
       foreach (TabParams tab in tablist)
        {
            if (tab.Tag.ToString() == Tag.ToString())
                tab.File = file_path;
        }
  }

But I getting an exception Tag is null . 但是我得到一个异常Tag为null Why? 为什么?

OMG! 我的天啊! I found the problem. 我发现了问题。 I should add components in the right order. 我应该以正确的顺序添加组件。

private void AddScriptTab(TabControl tab_contr, string tab_name, int idx)
    {
        TabPage tab_page = new TabPage();
        UserControlScript user_contr = new UserControlScript(serial_port);

        tab_page.Text = tab_name;
        tab_page.Tag = "script";

        user_contr.Dock = DockStyle.Fill;
        user_contr.Tag = idx.ToString();
        tab_page.Controls.Add(user_contr);

        tab_contr.TabPages.Insert(idx, tab_page);
    }

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

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