[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---