【问题标题】:Switching to dynamically created TabPage on button click在按钮单击时切换到动态创建的 TabPage
【发布时间】:2019-03-24 07:28:42
【问题描述】:

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

错误 CS0266 无法将类型“object”隐式转换为“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;
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 请将您的代码减少到绝对最小值。另外,请告知发生错误的行。
  • 很可能,tabControl1.SelectedTab = clickedBtn.Tag; 是错误的行。一个快速的解决办法是改写tabControl1.SelectedTab = clickedBtn.Tag as TabPage;。另一方面,由于您之前在 Tag 属性中存储了 int,因此我的建议也行不通。
  • 抱歉,我认为我需要所有这些来显示上下文。是的,你是对的。那种 Works @UweKeim 我得到一个空白的白色 TabPage?
  • @UweKeim 我刚刚尝试使用字符串作为名称,但遇到了同样的问题
  • 我建议您使用调试器来单步调试代码、检查变量并查看转换过程中实际发生的情况。 (提示:as 转换在源不是所需类型时返回 null)。

标签: c# winforms tabpage


【解决方案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;

【讨论】:

    【解决方案2】:
    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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2016-12-04
      相关资源
      最近更新 更多