单击按钮切换到动态创建的 TabPage

Switching to dynamically created TabPage on button click

我正在练习编码并尝试通过动态预填充存储在文本文件中的项目来创建收银台。我可以根据项目的类别动态创建选项卡、按钮和创建菜单按钮。我挣扎的地方是我试图在单击按钮时切换选项卡。选项卡有一个名称,即类别 ID,文本显示类别。如果尝试切换选项卡,我会收到以下错误:

Error CS0266 Cannot implicitly convert type 'object' to 'System.Windows.Forms.TabPage'. An explicit conversion exists (are you missing a cast?)

我假设我需要创建标签页或其他内容,但我不知道如何执行此操作。已经玩了几个小时了!这是我的代码....

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] loadedFile = File.ReadAllLines(@"h:\shopItemsV2.txt");
        foreach (string line in loadedFile)
        {
            // Split string and create required variables
            string[] newBtnData = line.Split(',');
            int catID = int.Parse(newBtnData[0]);
            string cat = newBtnData[1];
            string item = newBtnData[2];
            double price = double.Parse(newBtnData[3]);

            // Create tab if needed
            if (tabControl1.TabCount < catID)
            {
                TabPage tp = new TabPage()
                {
                    Text = cat,
                    Name = catID.ToString()
                };
                tabControl1.TabPages.Add(tp);
                // Add FlowLayoutPanel with unique name
                FlowLayoutPanel fp = new FlowLayoutPanel()
                {
                    Name = "fp" + catID.ToString(),
                    Dock = DockStyle.Fill
                };

                // Add FlowLayoutPanel to correct Tab
                tabControl1.TabPages[catID-1].Controls.Add(fp);

                // Create Button for menu
                Button newMenuBtn = new Button()
                {
                    Name = cat + "Btn",
                    Tag = catID,
                    Width = 100,
                    Height = 50,
                    Text = cat,
                };
                newMenuBtn.Click += SwitchTab;
                menuFP.Controls.Add(newMenuBtn);
            }

            // Create Button
            Button newBtn = new Button()
            {
                Name = item,
                Tag = price,
                Width = 100,
                Height = 100,
                Text = item,
            };
            newBtn.Click += AddItem;

            //Add button to correct tab
            foreach (TabPage tabP in tabControl1.TabPages)
            {

                if (tabP.Name == catID.ToString())
                {
                    Control fp = this.Controls.Find("fp"+catID, true).First();
                    fp.Controls.Add(newBtn);
                }
            }
        }
    }

    private void SwitchTab(object sender, EventArgs e)
    {
        // Create button, switch to required Tab
        // Tabs named the same as Tag on the button
        Button clickedBtn = sender as Button;        
        tabControl1.SelectedTab = clickedBtn.Tag;
    }
}

如有任何帮助,我们将不胜感激。

您可以在按钮的 Tag() 属性 中存储 任何内容。考虑到这一点,存储对 TabPage 的引用!

变化:

// Create Button for menu
Button newMenuBtn = new Button()
{
    Name = cat + "Btn",
    Tag = catID,
    Width = 100,
    Height = 50,
    Text = cat,
};

收件人:

// Create Button for menu
Button newMenuBtn = new Button()
{
    Name = cat + "Btn",
    Tag = tp; // store the reference to the TabPage you created
    Width = 100,
    Height = 50,
    Text = cat,
};

那么 Uwe Keim 的建议应该可行:

tabControl1.SelectedTab = clickedBtn.Tag as TabPage;
private void AddNewPr_Click(object sender, EventArgs e)
    {
        TabPage tab = new TabPage();
        _list = new ListBox();
        _list2 = new ListBox();
        PictureBox picBox = new PictureBox();
        picBox.Click = picBox_Click;

        //More stuff here

        //Add the controls        
        tabControl1.Controls.Add(tab);
        tab.Controls.Add(list);
        tab.Controls.Add(list2);
        tab.Controls.Add(pictureBox);
    }