首页 > 编程 > .NET > 正文

[VB.NET]浅谈MDI窗体的多窗体编程2

2024-07-10 13:00:46
字体:
来源:转载
供稿:网友
---关于with结构内的mdi窗体实例--

在上一篇内,介绍了mdi窗体的实例

http://blog.csdn.net/allenle/archive/2005/02/18/293122.aspx

在第二段代码中写到


private shared fr as new frmname
private sub toolbar1_buttonclick()sub toolbar1_buttonclick(byval sender as system.object, byval e as system.windows.forms.toolbarbuttonclickeventargs) handles toolbar1.buttonclick
select case e.button.text
case "ok" '"ok" is toolbarbutton.text
if fr is nothing or fr.isdisposed then
fr = new frmname
fr.mdiparent = me
fr.show()
else
fr.mdiparent = me
fr.show()
fr.focus()
end if
end select
end sub

今天又调试了一下,使用了with结构,代码如下

private shared fr as new frmname
private sub toolbar1_buttonclick()sub toolbar1_buttonclick(byval sender as system.object, byval e as system.windows.forms.toolbarbuttonclickeventargs) handles toolbar1.buttonclick
select case e.button.text
case "ok" '"ok" is toolbarbutton.text
with fr
if fr is nothing or fr.isdisposed then
fr = new frmname
.mdiparent = me
.show()
else
.mdiparent = me
.show()
.focus()
end if
end with
end select
end sub


调试出现一错误:cannot access a disposed object named "frmname".

想了一下,修改了代码,如下
就是把with的范围缩小到fr实例化之后,调试成功~
private shared fr as new frmname
private sub toolbar1_buttonclick()sub toolbar1_buttonclick(byval sender as system.object, byval e as system.windows.forms.toolbarbuttonclickeventargs) handles toolbar1.buttonclick
select case e.button.text
case "ok" '"ok" is toolbarbutton.text
if fr is nothing or fr.isdisposed then
fr = new frmname
with fr
.mdiparent = me
.show()
end with
else
with fr
.mdiparent = me
.show()
.focus()
end with
end if
end select
end sub
所以大家使用的时候要当心点
分析,就是关闭了实例了一次的窗口后,fr is nothing ,所以fr是不被with所使用的,改了with的使用范围之后就没有错误出现.
---end---


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表