All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
asp.net2.0验证控件之ValidationSummary控件使用方法详
在asp.net2.0中使用存储过程_.net资料_编程技术
C#中常见的错误类型
Expression,Studio和Silverlight学习资源、安装问题汇总_[Asp.Net教程]
Delphi其他查询
用ASP+CSS实现网页随机背景_[Html教程]
Sql,server,优化其索引的小技巧_[SQL,Server教程]
解决sqlserver2005中文版与vs2005的WebApplicationProject的冲突_[SQL,Server教程]
数据库索引应用(ms-sql)_mssql学习_编程技术
一个有关SQL Server连接的问题_[SQL Server教程]
Reading number is top 10 pictures
一千块钱如何创业
The household design, do not do bridal chamber a pity
大年初五接财神
Female model behind the bitterness, often being overcharged2
西班牙山村小景2
Desktop Wallpapers1
西游日记1
这两天,中国人民到处都可以“看海”了
From China fortress sora aoi2
Hunan road lawenforcement vehicle pursue overload car caused the car turn and man is died
Download software ranking
asp.net技术内幕
Call Of Duty5
jBuilder2006
Take off clothes to survival
I'm come from Beijing1
The cock of the Grosvenor LTD handsome
Proficient in JavaScript
美女写真2
Unix video tutorial18
Unix video tutorial12
qq published in(发表于) 2014/7/11 9:20:34 Edit(编辑)
C#中窗体继承的应用

C#中窗体继承的应用

C#中窗体继承的应用

继承窗体的应用

本节通过一个实例说明如何在Windows应用程序中使用继承窗体。继承窗体运行结果如图1所示。



图1 继承窗体

程序开发步骤如下。

(1)新建一个Windows应用程序,命名为07_01,默认窗体为Form1.cs。

(2)在窗体Form1中分别添加一个TextBox控件和一个Button控件,然后添加一个继承于Form1的新窗体,命名为Form2。

(3)程序主要代码。

Form1窗体中定义virtual类型的无返回值方法msg(),该方法调用MessageBox对象的Show方法输出一个字符串。msg()方法的实现代码如下:

public virtual void msg()

{

MessageBox.Show("继承窗体实例");

}

在Form1窗体的【继承窗体】按钮的Click事件中,设置TextBox控件的文本值,并调用自定义方法msg()输出一个字符串。【继承窗体】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

textBox1.Text = "基窗体";

msg();

}

由于Form2窗体是继承于Form1窗体的,因此,如果直接运行Form2窗体,其效果跟运行Form1窗体相同。通过重写Form1窗体中的自定义方法msg()重新设置TextBox文本框的值,其运行结果就会发生变化。重写msg()方法的实现代码如下:

public override void msg()

{

textBox1.Text = "继承的窗体";

base.msg();

}

注意:在向窗体中添加控件时,其Modifiers属性默认为private。因此,如果继承这样的窗体,在继承窗体中,控件的属性全部为不可编辑状态。本示例在实现时,将基窗体Form1中控件的Modifiers属性全部设置为public,因此在继承窗体Form2中,可以根据实际情况对各控件进行编辑。

完整程序代码如下:

★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace _7_01

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

textBox1.Text = "基窗体";

msg();

}

public virtual void msg()

{

MessageBox.Show("继承窗体实例");

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

★ ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.