简体   繁体   中英

Adding a user control to a TabPage

I create a TabPage and add on the TabPage a user control.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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