B窗体继承于A窗体,B启动:问题点

时间:2021-09-07 13:07:10
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls; type
TForm1 = class(TForm)
pnl1: TPanel;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} end.
//---------
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Unit1, ExtCtrls; type
TForm2 = class(TForm1) // 继承于TForm1类
pnl2: TPanel;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form2: TForm2; implementation {$R *.dfm} end.

代码

object Form1: TForm1
Left =
Top =
Width =
Height =
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch =
TextHeight =
object pnl1: TPanel
Left =
Top =
Width =
Height =
Caption = 'pnl1'
Color = clSkyBlue
TabOrder =
end
end
//------------
inherited Form2: TForm2
Width =
Height =
Caption = 'Form2'
PixelsPerInch =
TextHeight =
inherited pnl1: TPanel
Left =
Top =
Width =
Height =
end
object pnl2: TPanel
Left =
Top =
Width =
Height =
Caption = 'pnl2'
Color =
TabOrder =
end
end

窗体代码

B窗体继承于A窗体,B启动:问题点

Delphi2C#工具转换后,有异常无法编译

仅需要改 //Unit2.Designer.cs

namespace Unit2
{
partial class TForm2
{
public System.Windows.Forms.Panel pnl1;
public System.Windows.Forms.Panel pnl2; #region Windows Form Designer generated code
private void InitializeComponent()
{
this.pnl1 = new System.Windows.Forms.Panel();
this.pnl2 = new System.Windows.Forms.Panel();
this.pnl1.Size = new System.Drawing.Size(, );
this.pnl1.Location = new System.Drawing.Point(, );
this.pnl1.Enabled = true;
this.pnl1.Name = "pnl1";
this.pnl1.BackColor = System.Drawing.SystemColors.ButtonFace;
this.pnl2.Size = new System.Drawing.Size(, );
this.pnl2.Location = new System.Drawing.Point(, );
this.pnl2.Enabled = true;
this.pnl2.TabIndex = ;
this.pnl2.Name = "pnl2";
this.pnl2.Text = "pnl2";
this.pnl2.BackColor = ;
}
#endregion }
}

这是生成的默认代码

//这是修改后的代码
namespace Unit2
{
partial class TForm2
{
//public System.Windows.Forms.Panel pnl1;// 第一处屏蔽
public System.Windows.Forms.Panel pnl2; #region Windows Form Designer generated code
private void InitializeComponent()
{
//this.pnl1 = new System.Windows.Forms.Panel(); 第二 屏蔽
this.pnl2 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// pnl1
//
this.pnl1.BackColor = System.Drawing.SystemColors.MenuHighlight; //第二并列 在窗体改panel颜色,就自动加了这三句话
this.pnl1.Location = new System.Drawing.Point(, );
this.pnl1.Size = new System.Drawing.Size(, );
//
// pnl2
//
this.pnl2.BackColor = System.Drawing.Color.AliceBlue;//第三改颜色值
this.pnl2.Location = new System.Drawing.Point(, );
this.pnl2.Name = "pnl2";
this.pnl2.Size = new System.Drawing.Size(, );
this.pnl2.TabIndex = ;
this.pnl2.Text = "pnl2";
//
// TForm2 //第四 加以下一段代码
//
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.pnl2);
this.Location = new System.Drawing.Point(, );
this.Name = "TForm2";
this.Text = "Form1-form2";
this.Controls.SetChildIndex(this.pnl1, );
this.Controls.SetChildIndex(this.pnl2, );
this.ResumeLayout(false); }
#endregion }
}

、、-----------以下为错误的,不用看

using System;
using System.Windows.Forms;
using Unit1;
using Unit2;
namespace Project1.Units
{
public class Project1
{
// Form1
// Form2
[STAThread]
public static void Main(string[] args)
{
// Application.Initialize;
Unit1.Units.Unit1.Form1 = new TForm1();
Unit2.Units.Unit2.Form2 = new TForm2();
// Application.Run;
Application.Run(Unit2.Units.Unit2.Form2);//改第一处,这默认是Form1先启动,要改成Form2
} } // end Project1 }

改第一处

namespace Unit1
{
partial class TForm1
{
public System.Windows.Forms.Panel pnl1;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ToolTip toolTip1 = null; // Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
} #region Windows Form Designer generated code
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType());
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.pnl1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.Location = new System.Drawing.Point(, );
this.ClientSize = new System.Drawing.Size(, );
this.Font = new System.Drawing.Font("MS Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point ,((byte)()));
this.BackColor = System.Drawing.SystemColors.ButtonFace;
this.Name = "Form1";
this.Text = "Form1";
this.AutoScroll = true;
this.pnl1.Size = new System.Drawing.Size(, );
this.pnl1.Location = new System.Drawing.Point(, );
this.pnl1.Enabled = true;
this.pnl1.TabIndex = ;
this.pnl1.Name = "pnl1";
this.pnl1.Text = "pnl1";
this.pnl1.BackColor = ; //System.Drawing.Color.SkyBlue; 改第二处,这里要写成这样的格式
this.Controls.Add(pnl1);
this.ResumeLayout(false);
}
#endregion }
}

改第二处