實例探討VB.NET回調函數(shù)
VB.NET經過長時間的發(fā)展,很多用戶都很了解VB.NET回調函數(shù)了,這里我發(fā)表一下個人理解,和大家討論討論。創(chuàng)建數(shù)據(jù)庫事件處理函數(shù)(VB.NET回調函數(shù))
#t#無論什么時候一個對象被打開并要被修改時,數(shù)據(jù)庫事件處理函數(shù)會被調用。當然,如果這時我們監(jiān)視的命令不是活動的,我們就應該跳過任何被這個VB.NET回調函數(shù)調用的內容。
If bEditCommand = False Then
Return
End If
同樣地,如果我們監(jiān)視的命令已經結束,而ObjectOpenedForModify事件被另一個VB.NET回調函數(shù)再次觸發(fā)的話,而這時有對象被修改時,我們要阻止所有由這個VB.NET回調函數(shù)執(zhí)行的動作。
If bDoRepositioning = True Then
Return
End If
這個VB.NET回調函數(shù)剩余部分的代碼用來驗證我們是否正在處理EMPLOYEE塊索引。如果是的話,我們就獲取它的ObjectID和位置(三維點)。下面的代碼可以被粘貼到這個事件處理函數(shù)函數(shù)。
- Public Sub objOpenedForMod(ByVal o As Object, ByVal e As ObjectEventArgs)
- If bEditCommand = False Then
- Return
- End If
- If bDoRepositioning = True Then
- Return
- End If
- Dim objId As ObjectId
- objId = e.DBObject.ObjectId
- Dim trans As Transaction
- Dim bt As BlockTable
- Dim db As Database
- db = HostApplicationServices.WorkingDatabase
- trans = db.TransactionManager.StartTransaction()
- Try
- 'Use it to open the current object!
- Dim ent As Entity = trans.GetObject(objId, OpenMode.ForRead, False)
- If TypeOf ent Is BlockReference Then 'We use .NET's RTTI to establish type.
- Dim br As BlockReference = CType(ent, BlockReference)
- 'Test whether it is an employee block
- 'open its extension dictionary
- If br.ExtensionDictionary().IsValid Then
- Dim brExtDict As DBDictionary = trans.GetObject(br.ExtensionDictionary(), OpenMode.ForRead)
- If brExtDict.GetAt("EmployeeData").IsValid Then
- 'successfully got "EmployeeData" so br is employee block ref
- 'Store the objectID and the position
- changedObjects.Add(objId)
- employeePositions.Add(br.Position)
- 'Get the attribute references,if any
- Dim atts As AttributeCollection
- atts = br.AttributeCollection
- If atts.Count > 0 Then
- Dim attId As ObjectId
- For Each attId In atts
- Dim att As AttributeReference
- att = trans.GetObject(attId, OpenMode.ForRead, False)
- changedObjects.Add(attId)
- employeePositions.Add(att.Position)
- Next
- End If
- End If
- End If
- End If
- trans.Commit()
- Finally
- trans.Dispose()
- End Try
- End Sub