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

时间:2018-10-19 11:50:44

标签: c# winforms tabpage

我正在练习编码,并尝试通过动态预填充存储在文本文件中的项目来创建翻盖。我可以根据项目的类别动态创建“标签”,“按钮”并创建菜单按钮。我苦苦挣扎的地方是尝试在单击按钮时切换Tab。选项卡具有一个名称,即类别ID,文本则显示类别。在尝试切换标签的情况下,出现以下错误:

  

错误CS0266无法将类型“对象”隐式转换为“ System.Windows.Forms.TabPage”。存在显式转换(您是否缺少演员表?)

我假设需要创建一个选项卡页面或其他内容,但是我找不到执行该操作的方法。坚持了几个小时!这是我的代码。...

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以将任何内容存储在按钮的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;

答案 1 :(得分:0)

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