.Net中应用XML动态生成窗体

        最近要做一个项目,这个项目以前有个版本,我很佩服原来那个程序员,用了很多编程技巧,写了很多代码。看了他的代码很多地方我都有很大的提高,可惜客户不认这些,于是又要我们重写。老版本里很多地方其实没必要的,而且如果用户对数据库字段修改了也会导致程序不能使用。
        为了避免以上问题出现,我想到了用XML来预先定义窗体,通过运行时动态生成窗体来提高程序的可重用性,这样就可以写很少的代码生成不同需要的窗体。而且当有类似的项目需求时,可以很容易的就做出来。
        好了,让我来说说这个项目大体思路吧(所用代码为这个项目的一个类似工程)。 项目工程下载
        首先定义一个XML文件:FormStyle.xml,这个项目所有的程序都是围绕这个XML文档来操作的。
None.gif <!-- FormStyle.xml -->  
None.gif
<!-- 该文档用来定义窗体 -->  
None.gif
<? xml version="1.0" encoding="utf-8"  ?>   
None.gif
< FormStyles >  
None.gif    
< Form  Name ="Add"  Width ="300"  Height ="400" >  
None.gif        
< Button  Name ="OK"  Text ="确定"  Width ="75"  Height ="23"  Left ="100"  Top ="30" >  
None.gif        
</ Button >  
None.gif    
</ Form >  
None.gif    
< Form  Name ="Del"  Width ="350"  Height ="450" >  
None.gif        
< Button  Name ="Cancel"  Text ="取消"   Width ="75"  Height ="23"  Left ="100"  Top ="30" >  
None.gif        
</ Button >  
None.gif        
< TextBox  Name ="txtName"  Width ="100"  Height ="25"  Left ="100"  Top ="60" ></ TextBox >  
None.gif    
</ Form >  
None.gif    
< Form  Name ="Search"  Width ="500"  Height ="400" >  
None.gif        
< Button  Name ="OK"  Text ="确定"  Width ="75"  Height ="23"  Left ="150"  Top ="30" ></ Button >  
None.gif        
< Button  Name ="Cancel"  Text ="取消"  Width ="75"  Height ="23"  Left ="60"  Top ="30" ></ Button >  
None.gif        
< TextBox  Name ="txtOK"  Width ="100"  Height ="25"  Left ="100"  Top ="60" ></ TextBox >  
None.gif    
</ Form >  
None.gif    
< Form  Name ="Modify"  Width ="500"  Height ="400" >  
None.gif        
< Button  Name ="OK"  Text ="确定"  Width ="75"  Height ="23"  Left ="150"  Top ="30" ></ Button >  
None.gif        
< Button  Name ="Cancel"  Text ="取消"  Width ="75"  Height ="23"  Left ="60"  Top ="30" ></ Button >  
None.gif    
</ Form >  
None.gif
</ FormStyles >
文档中用<Form></Form>来定义一个窗体,该节点里的其他元素定义窗体中的控件。
然后就是在主应用程序(frmMain.cs)中通过读取Xml文档来生成菜单项:
None.gif XmlDocument xmlReader  =   null ;             //  定义XML文档类 
None.gif
 
None.gif
if (xmlReader  ==   null
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    xmlReader 
= new XmlDocument(); 
InBlock.gif    xmlReader.Load(Application.StartupPath 
+ "\\..\\..\\FormStyle.xml"); 
ExpandedBlockEnd.gif}
 
None.gif
//  通过遍历Xml文档的子节点来生成菜单项,菜单项的Text属性值为<Form></Form>节点 //  中的"Name"属性值。 
None.gif
for ( int  i = 0 , count = xmlReader.DocumentElement.ChildNodes.Count; i  <  count; i ++
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
if(xmlReader.DocumentElement.ChildNodes[i].Name == "Form"
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        MenuItem mnuChild 
= new MenuItem(); 
InBlock.gif        mnuChild.Text 
= xmlReader.DocumentElement.ChildNodes[i].Attributes["Name"].Value; 
InBlock.gif        mnuEdit.MenuItems.Add(mnuChild); 
InBlock.gif        mnuChild.Click 
+= new EventHandler(CreateChildForm);    // 将菜单的Click事件关联到//CreateChildForm方法 
ExpandedSubBlockEnd.gif
    }
 
ExpandedBlockEnd.gif}
 
None.gif 
None.gif
//  菜单Click事件的处理方法 
None.gif
private   void  CreateChildForm( object  sender, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    frmChild child; 
InBlock.gif    
string FormStyle; 
InBlock.gif 
InBlock.gif    FormStyle 
= (sender as MenuItem).Text; 
InBlock.gif    child 
= new frmChild(FormStyle); 
InBlock.gif    child.MdiParent 
= this
InBlock.gif    child.Show(); 
ExpandedBlockEnd.gif}
 
下面是子窗体frmChild.cs的主要代码,具体功能在构造函数中实现:
None.gif private  XmlDocument xmlReader  =   null
None.gif 
None.gif
public  frmChild( string  FormStyle) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
// 
InBlock.gif    
// Windows 窗体设计器支持所必需的 
InBlock.gif    
// 
InBlock.gif
    InitializeComponent(); 
InBlock.gif 
InBlock.gif    
if(xmlReader == null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        xmlReader 
= new XmlDocument(); 
InBlock.gif        xmlReader.Load(Application.StartupPath 
+ "\\..\\..\\FormStyle.xml"); 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif 
InBlock.gif    XmlNode xnode 
= xmlReader.DocumentElement.SelectSingleNode(string.Format("descendant::Form[@Name='{0}']", FormStyle));        // 这里用到了XPath来确定要在Xml文档中读取的节点,具体关于// XPath的知识请读者自行查看相关资料吧 
InBlock.gif
    if(xnode != null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
for(int i=0; i < xnode.ChildNodes.Count; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
if(xnode.ChildNodes[i].Name == "Button"
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Button btnOk 
= new Button(); 
InBlock.gif                btnOk.Width 
= int.Parse(xnode.ChildNodes[i].Attributes["Width"].Value); 
InBlock.gif                btnOk.Height 
= int.Parse(xnode.ChildNodes[i].Attributes["Height"].Value); 
InBlock.gif                btnOk.Left 
= int.Parse(xnode.ChildNodes[i].Attributes["Left"].Value); 
InBlock.gif                btnOk.Top 
= int.Parse(xnode.ChildNodes[i].Attributes["Top"].Value); 
InBlock.gif                btnOk.Text 
= xnode.ChildNodes[i].Attributes["Text"].Value; 
InBlock.gif                
this.Controls.Add(btnOk); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else if(xnode.ChildNodes[i].Name == "TextBox"
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                TextBox txtAdd 
= new TextBox(); 
InBlock.gif                txtAdd.Width 
= int.Parse(xnode.ChildNodes[i].Attributes["Width"].Value); 
InBlock.gif                txtAdd.Height 
= int.Parse(xnode.ChildNodes[i].Attributes["Height"].Value); 
InBlock.gif                txtAdd.Left 
= int.Parse(xnode.ChildNodes[i].Attributes["Left"].Value); 
InBlock.gif                txtAdd.Top 
= int.Parse(xnode.ChildNodes[i].Attributes["Top"].Value); 
InBlock.gif                
this.Controls.Add(txtAdd); 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
不足之处:没有完成动态生成的控件的事件处理函数。有兴趣的朋友自己完成吧

转载于:https://www.cnblogs.com/srw962/archive/2005/04/03/131202.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值