高手请解惑:窗体继承问题!(在线等待)

netpot 2003-04-15 05:23:31
一基类窗体,有两button控件及其相应click方法。
用一子窗体继承基类,click方法即窗体、button的多数属性可以继承实现。
但是有以下问题:
1)基类窗体的startPostion设置为CenterScreen,子类在设计时候看到的startPostion也是

CenterScreen,但在运行后不是。而如果将基类默认启动
位置改为centerParent,那么在子类设计时候改变其默认继承的centerParent为
centerScreen就可以实现(因为此时会在子类代码中加入frm.StartPostion代码行)

2)基类窗体无load事件,在子类中加入load事件
此时若用showDialog方法调用子类窗体不执行其load事件
调用show方法才可执行load事件

3)基类button的默认visible为true,在子类设计器中其默认属性却变成了false,
若在子类中将其修改为true会在相应代码中增加button1.visible=true。
但是问题是若不修改子类visible属性,(在相应代码中设置visible行),此时在执行到子类

initialComponent时候跟踪会发现其visible为false,然而当子类show以后后,该button竟然变成了可

见的(visible=true)

这3个问题感觉到很奇怪,是c#对继承控件的bug还是继承中有些属性的限制?
...全文
52 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
eTopFancy 2003-05-10
  • 打赏
  • 举报
回复
gz
hongshun 2003-04-29
  • 打赏
  • 举报
回复
你做个试验的代码发给我。

(简单的两个窗体。)

我自己做 没有问题。 你给我个看看

hongshun@hotmail.com
netpot 2003-04-24
  • 打赏
  • 举报
回复
UP 一下
zhuohs 2003-04-17
  • 打赏
  • 举报
回复
基类的方法应该都可以被派生类继承,你要是不满意效果的话,可以override基类的方法啊,有什么问题呢?
netpot 2003-04-17
  • 打赏
  • 举报
回复
Up一下!
盼回复!
分不够可再加!
netpot 2003-04-17
  • 打赏
  • 举报
回复
To : hongshun(好好)
呵呵,我一开始也怀疑是直接修改代码的父类问题,用vs的继承窗体也没解决。
你在子类中将button的visible改成false
然后运行看看button能否可见?
我又尝试了,可button还是可见的
附代码:
基类代码:输出类型为类库(class library)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace BaseClass
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class frmBase : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public System.Windows.Forms.Button btnExit;
public System.Windows.Forms.Button btnPrint;


public frmBase()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnExit = new System.Windows.Forms.Button();
this.btnPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Font = new System.Drawing.Font("楷体_GB2312", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnExit.Location = new System.Drawing.Point(112, 88);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 28);
this.btnExit.TabIndex = 10;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnPrint
//
this.btnPrint.Font = new System.Drawing.Font("楷体_GB2312", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnPrint.Location = new System.Drawing.Point(24, 88);
this.btnPrint.Name = "btnPrint";
this.btnPrint.Size = new System.Drawing.Size(75, 28);
this.btnPrint.TabIndex = 9;
this.btnPrint.Text = "打印";
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
//
// frmBase
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(272, 133);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnExit,
this.btnPrint});
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(280, 160);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(280, 160);
this.Name = "frmBase";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);

}
#endregion


/// <summary>
/// 打印
/// </summary>
/// <param name="sender">打印按钮被单击</param>
/// <param name="e"></param>
protected void btnPrint_Click(object sender, System.EventArgs e)
{
MessageBox.Show("打印按钮被单击!","提示");

}


/// <summary>
/// 退出窗体
/// </summary>
/// <param name="sender">退出按钮</param>
/// <param name="e"></param>
protected void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}

}
}

在同一解决方案另建个项目,添加继承窗体。设置为启动项目

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;

using System.Windows.Forms;


namespace BaseClass
{
public class frmChild : BaseClass.frmBase
{
private System.ComponentModel.IContainer components = null;

public frmChild()
{
// 该调用是 Windows 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// frmChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 133);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnExit,
this.btnPrint});
this.Name = "frmChild";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmChild());
}
}
}

请注意此时btnPrint和btnExit在子类中是没有代码的
因为在设计器将这两个button的visible设置为false的
但将visible设置为true时候,在子类代码中会加上visible=ture的代码,其实也就是说子类中button的默认visible是为false
而且更要命的是如果在设计时候是false,但子类运行时候这两个button依然显示!
bethrezen 2003-04-17
  • 打赏
  • 举报
回复
收藏
hongshun 2003-04-17
  • 打赏
  • 举报
回复
又试了一下。好像直接修改代码里的父类也 没有问题 :(
hongshun 2003-04-17
  • 打赏
  • 举报
回复

楼主提的三个问题。 我刚刚试验了一下。 好像都没有你的问题。

可能是你添加窗体的方法不得当。 你应该选择vs 中的 添加继承窗体的方法添加


直接在代码里 修改其父类。好像确实有些问题



netpot 2003-04-17
  • 打赏
  • 举报
回复
To zhuohs(凌九霄)
麻烦你仔细看看我的问题
关键部分不是方法的继承和重写
而是form的属性和textbox的属性继承的bug
netpot 2003-04-15
  • 打赏
  • 举报
回复
To seabirdforever(听海)
但是我的代码中基类Button的Click事件可以在子类中继承使用的(子类不写任何button事件)
而现在最迷惑的是基类窗体和button的属性继承
实际上多数的属性都可以得到继承,而且没出现问题
但就是基类窗体的startPositon属性、button的Visible属性继承有问题
还有子类的load和基类无关,基类根本就没使用窗体load事件。

seabirdforever 2003-04-15
  • 打赏
  • 举报
回复

关于你要很多类继承的窗体,建议你在基类 不用用事件

用方法 ,(用 On 方法代替相关事件)
因为事件不可以继承
on 方法可以继承
li_new 2003-04-15
  • 打赏
  • 举报
回复
你的研究很仔细。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧