863
社区成员
发帖
与我相关
我的任务
分享
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private ReNew As Integer
Public Property WillReNew As Integer
Get
Return ReNew
End Get
Set(value As Integer)
ReNew = value
End Set
End Property
Dim b As testclass
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
Debug.Print("在主程序中:" & Me.WillReNew)
'第一次点击时返回123,第二次点击时还是返回123
Call b.test()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ReNew = 123 '对Form1的WillReNew属性进行初始化,为123。
b = New testclass
b.application = New Application
b.application.Visible = True
b.application.Workbooks.Add()
End Sub
End Class
Imports Microsoft.Office.Interop.Excel
Public Class testclass
Private WithEvents app As Application
Public Property application As Application
Get
Return app
End Get
Set(value As Application)
app = value
End Set
End Property
Public Sub test()
Debug.Print("在testclass类中:" & Form1.WillReNew)
' '第一次点击时返回123,第二次点击时还是返回123
End Sub
' 出现问题的事件
Private Sub refresh(wkbk As Workbook, ByRef cancel As Boolean) _
Handles app.WorkbookBeforeClose
Debug.Print("在testclass类的WorkbookBeforeClose事件中:" & Form1.WillReNew)
'这里Form1.WillReNew返回integer的初始值0,
'在调试中发现它进入Form1中对其每一个属性或域值进行了初始化
'(类似于重新声明了一个Form1的对象)
'猜测可能是线程的问题,因为下一次点击窗口时还是能返回123
End Sub
End Class
Debug.Print("在testclass类中:" & Form1.WillReNew)
Public Form1 As New Form1
Imports Microsoft.Office.Interop.Excel
Public Class testclass
Public ev_event()
Public mainForm As Form1 '在这里索引主程序
Private WithEvents app As Application
Public Property application As Application
Get
Return app
End Get
Set(value As Application)
app = value
End Set
End Property
Public Sub test()
Me.mainForm = Form1
'在第3步时,将Form1的实例传给testclass的mainform字段,
'但是问题是,这里还是能将form1作为主程序的实例的
Debug.Print("在testclass类中:" & Form1.WillReNew)
End Sub
Private Sub refresh() Handles app.WindowResize
Debug.Print("在testclass类的WorkbookBeforeClose事件中:" & mainForm.WillReNew) '返回integer的初始值0
End Sub
End Class
所以第一个问题还是没有解决:为什么第4步与第5步中会出现这种不同。
都是调用 Form1.WillReNew,为什么会一个要能索引到主程序对象,而一个却要重新初始化一个对象???