微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WinForms-如何遍历具有自定义页面和system.windows.forms选项卡的tabcontrol的选项卡?

如何解决WinForms-如何遍历具有自定义页面和system.windows.forms选项卡的tabcontrol的选项卡?

我制作了一些预建的选项卡页面,这些页面继承自systems.windows.forms.tabpage类,以帮助聚集相关元素并提高灵活性和可读性。问题在于这样做,我做到了,这样当我想要通过使用foreach来实现特定的Tabpage布局时,就无法遍历tabcontrol的页面。我收到以下错误

system.invalidCastException:'无法将类型'System.Windows.Forms.TabPage'的对象转换为类型'GraphicProject.ProductionTabPage'。”

解决此问题,我有哪些选择可以使我继续制作这些预制组件?下面的代码

表单设计器代码

sta=time.time(); out,err = sp.Popen('adb shell screencap > /sdcard/scrs/scr.1',stdout=sp.PIPE).communicate(); sto=time.time(); sto-sta
0.4085681438446045

TabPage代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。