加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

有多处代码访问同一窗体,保持原窗体不变.

(2007-11-09 20:42:04)
标签:

IT/科技

窗体

 如果在项目中有多处代码访问同一窗体,则你必须把它的同一实例指针传递给这些代码,否则新创建的窗体实例就不再是原先的窗体了。

下面的代码实现窗体Form1和Form2之间互相调用,Form1为主窗体。Form1上的按钮BtnShowFrm2的标题为“显示Form2”,Form2上的按钮BtnShowFrm1的标题为“显示Form1”。

1、Form1中的代码:

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifPublic Class Form1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
Inherits System.Windows.Forms.Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
'创建Form2的一个新的实例
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
    Dim Frm2 As New Form2()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    
Public Function Instance2(ByVal frm As Form2)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Frm2 
= frm
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    
End Function

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    
Private Sub BtnShowFrm2_Click(ByVal sender As System.Object, ByVal As System.EventArgs) _
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
Handles BtnShowFrm2.Click
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
'以下语句保证在Form2以及其他窗体中访问Form1时,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
        '都将得到Form1的同一个窗体实例。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
        Frm2.Instance(Me)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Frm2.Show()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
Me.Hide()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    
End Sub

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif
End Class

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
2、Form2中的代码:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifPublic Class Form2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
Inherits System.Windows.Forms.Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
Dim frm1 As Form1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
'借助一个新增的Instance属性来生成窗体frm1的实例
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
 Public Function Instance(ByVal frm As Form1)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  frm1 
= frm
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif 
End Function

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif 
Private Sub BtnShowFrm1_Click(ByVal sender As System.Object, ByVal As System.EventArgs) _
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
Handles BtnShowFrm1.Click
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  
Me.Hide()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  frm1.Show()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif 
End Sub

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif 
Private Sub Form2_Closed(ByVal sender As ObjectByVal As System.EventArgs) Handles MyBase.Closed
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  
'如果Form2被关闭,则设置Form1的按钮BtnShowFrm2不可用。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
  frm1.BtnShowFrm2.Enabled = False
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif  frm1.Show()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif 
End Sub

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif
End Class

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有