Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms-如何通过tabcontrol';有自定义页和system.windows.forms选项卡页的选项卡页?_C#_Winforms_Tabcontrol - Fatal编程技术网

C# WinForms-如何通过tabcontrol';有自定义页和system.windows.forms选项卡页的选项卡页?

C# WinForms-如何通过tabcontrol';有自定义页和system.windows.forms选项卡页的选项卡页?,c#,winforms,tabcontrol,C#,Winforms,Tabcontrol,我制作了一些从systems.windows.forms.tabpage类继承的预构建选项卡页,以帮助对相关元素进行集群,并提高灵活性和可读性。问题是,在这样做的过程中,我这样做是为了在需要使用foreach的特定tabpage布局时,不能循环遍历tabcontrol的页面。我得到以下错误: “System.InvalidCastException:'无法将'System.Windows.Forms.TabPage'类型的对象强制转换为'GraphicProject.ProductionTabP

我制作了一些从systems.windows.forms.tabpage类继承的预构建选项卡页,以帮助对相关元素进行集群,并提高灵活性和可读性。问题是,在这样做的过程中,我这样做是为了在需要使用foreach的特定tabpage布局时,不能循环遍历tabcontrol的页面。我得到以下错误:

“System.InvalidCastException:'无法将'System.Windows.Forms.TabPage'类型的对象强制转换为'GraphicProject.ProductionTabPage'类型。”

为了解决这个问题,我有哪些选项可以让我继续制作这些预构建组件?代码如下

表单设计器代码

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace FlowCal_Time_Analyzer
{
    partial class Form1
    {
        
        
        private System.ComponentModel.IContainer components = null;

        

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabControl1.SuspendLayout();
            this.tabControl1.Controls.Add(new System.Windows.Forms.TabPage());
            this.tabControl1.Controls.Add(new ProductionTabPage());
            foreach (ProductionTabPage PTG in tabControl1.TabPages) //failure happens here on first element
            {
                PTG.Initialize();
            }
            this.tabControl1.ResumeLayout(false);
        }
    }
}

foreach(tabControl1.TabPages中的TabPage tp){if(tp是ProductionTabPage){((ProductionTabPage)tp.Initialize();)
这对我来说很有效,谢谢!但我有点搞不清楚为什么会起作用。这不就是像我最初在代码中指定的那样执行隐含强制转换吗?它起作用是因为已知类型是productiontabpage,还是因为tabpage是productiontabpage继承的?后者。术语把它称为向上铸造还是向下铸造并不清楚,但只有一种作品,从专门的到更基本的类型;-)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Wpf;
using System.Threading;

namespace FlowCal_Time_Analyzer
{
    class ProductionTabPage : TabPage
    {
        private Button GetProductionDataButton = new Button();
        private DateTimePicker ProductionDatePicker = new DateTimePicker();
        private DateTime m_selectedDate;

        public void Initialize()
        {
            this.ProductionDatePicker.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ProductionDatePicker.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.ProductionDatePicker.Location = new System.Drawing.Point(83,4);
            this.ProductionDatePicker.Margin = new System.Windows.Forms.Padding(2);
            this.ProductionDatePicker.Name = "GraphDatePicker";
            this.ProductionDatePicker.Size = new System.Drawing.Size(96, 20);
            this.ProductionDatePicker.TabIndex = 1;
            this.ProductionDatePicker.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
            this.ProductionDatePicker.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GraphDatePicker_MouseUp);

            this.GetProductionDataButton.Location = new System.Drawing.Point(4, 4);
            this.GetProductionDataButton.Name = "GetProductionDataButton";
            this.GetProductionDataButton.Size = new System.Drawing.Size(75, 23);
            this.GetProductionDataButton.TabIndex = 0;
            this.GetProductionDataButton.Text = "Get Device Count";
            this.GetProductionDataButton.UseVisualStyleBackColor = true;
            this.GetProductionDataButton.Click += new System.EventHandler(this.GetProductionDataButton_Click);

            this.Controls.Add(ProductionDatePicker);
            this.Controls.Add(GetProductionDataButton);


        }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            m_selectedDate = ProductionDatePicker.Value.Date;
        }

        private void GraphDatePicker_MouseUp(object sender, MouseEventArgs e)
        {
            ProductionDatePicker.Select();
            SendKeys.Send("%{DOWN}");
        }

        private void GetProductionDataButton_Click(object sender, EventArgs e)
        {
            DBProductionModule dB_Production = new DBProductionModule();
            dB_Production.GetProductionData(m_selectedDate);
        }
    }
}