AddReference "System.Drawing" Imports System.Data Imports System.Drawing Imports System.Windows.Forms 'Imports System.ComponentModel 'This is the code that actually shows/runs the Form Public Class RunMyForm Private Sub Main break Dim oMyForm As New WinForm(ThisApplication) oMyForm.Show() End Sub End Class Public Class WinForm Inherits System.Windows.Forms.Form Private m_Inv As Inventor.Application Private m_ApplicationEvents As Inventor.ApplicationEvents Private bEventHandler As Boolean Private bindingSource1 As New BindingSource() Public oLargerFont As System.Drawing.Font = New Font("Arial", 10) Public Sub New(ByRef g_Inv As Inventor.Application) m_Inv = g_Inv m_ApplicationEvents = m_Inv.ApplicationEvents oForm = Me With oForm .FormBorderStyle = FormBorderStyle.SizableToolWindow .StartPosition = FormStartPosition.CenterScreen .Width = 775 .Height = 500 .TopMost = True .Font = oLargerFont .Text = "Open Views List" .Name = "Open Views List" .ShowInTaskbar = False End With Dim oDGView As New DataGridView With oDGView .Top = 25 .Left=25 .Width = 700 .Height = 350 .Name = "DGView1" .Anchor =CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right Or System.Windows.Forms.AnchorStyles.Left ), System.Windows.Forms.AnchorStyles) End With oDGView.SelectionMode=DataGridViewSelectionMode.FullRowSelect oDGView.ColumnHeadersHeight = 28 oDGView.ShowEditingIcon=False oDGView.MultiSelect=False oDGView.AllowUserToAddRows = False oDGView.AllowUserToDeleteRows = False oDGView.AllowUserToOrderColumns = False oForm.Controls.Add(oDGView) AddHandler oDGView.SelectionChanged, AddressOf oDGView_SelectionChanged Dim oButton1 As New Button() With oButton1 .Text = "Close" .Top = 400 .Left = 650 .Enabled = True .AutoSize = True .Anchor =CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) End With oForm.Controls.Add(oButton1) AddHandler oButton1.Click, AddressOf oButton1_Click Dim oButton2 As New Button() With oButton2 .Text = "Refresh" .Top = 400 .Left = 550 .Enabled = True .AutoSize = True .Anchor =CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) End With oForm.Controls.Add(oButton2) AddHandler oButton2.Click, AddressOf oButton2_Click AddHandler m_ApplicationEvents.OnActivateView, AddressOf oApp_ActiveViewChange AddHandler m_ApplicationEvents.OnNewView, AddressOf oApp_OnNewView AddHandler m_ApplicationEvents.OnCloseView, AddressOf oApp_OnCloseView FillBox End Sub Private Sub FillBox() bEventHandler = False Dim myApp As Inventor.Application = m_Inv Dim myForm As WinForm = Me Dim myView As Inventor.View Dim myActiveView As Inventor.View = myApp.ActiveView Dim myRefedDoc As Inventor.Document Dim oDGView As DataGridView=myForm.Controls.Find("DGView1", False).FirstOrDefault() If oDGView Is Nothing Then MsgBox("DataGrid not found") Exit Sub End If Dim oColl As New Collection Dim sLabel As String Dim i As Integer For Each myView In myApp.Views myRefedDoc = myView.Document If Not myRefedDoc.Propertysets(3).Item(14).value = String.Empty Then sLabel =myRefedDoc.PropertySets(3).Item(14).Value Else sLabel=" --- NO NAME ---- " End If oColl.Add(New ViewItem(myView, myrefeddoc, sLabel)) If myView.HWND = myApp.ActiveView.HWND Then i = oColl.Count - 1 Next bindingSource1.position = i bindingSource1.datasource = oColl oDGView.DataSource = bindingSource1 'oColl oDGView.Rows(i).Selected = True oDGView.CurrentCell = oDGView.Rows(i).Cells(2) oDGView.Columns.Item(0).Visible = False oDGView.Columns.Item(1).Visible = False oDGView.Columns.Item(2).HeaderText = "Bezeichnung" oDGView.Columns.Item(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill oDGView.Columns.Item(2).ReadOnly=True bEventHandler = True End Sub Private Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) RemoveHandler m_ApplicationEvents.OnActivateView, AddressOf oApp_ActiveViewChange RemoveHandler m_ApplicationEvents.OnNewView, AddressOf oApp_OnNewView RemoveHandler m_ApplicationEvents.OnCloseView, AddressOf oApp_OnCloseView m_ApplicationEvents = Nothing Me.Close Me.Dispose gc.Collect End Sub Private Sub oButton2_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) fillbox End Sub Private Sub oDGView_SelectionChanged(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) If bEventHandler = False Then Exit Sub Dim myForm As WinForm = Me Dim oDGView As DataGridView=myForm.Controls.Find("DGView1", False).FirstOrDefault() If oDGView Is Nothing Then MsgBox("DataGrid not found") Exit Sub End If Dim oView As Inventor.View If oDGView.SelectedCells.Count>0 Then oView=oDGView.SelectedCells(0).Value oView.Activate End If End Sub Private Sub oApp_ActiveViewChange(ViewObject As Inventor.View, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) If BeforeOrAfter = EventTimingEnum.kAfter Then If bEventHandler = False Then Exit Sub Dim myApp As Inventor.Application = m_Inv Dim myForm As WinForm = Me Dim myActiveView As Inventor.View = myApp.ActiveView Dim oDGView As DataGridView=myForm.Controls.Find("DGView1", False).FirstOrDefault() For Each oRow As DataGridViewRow In oDGView.Rows Dim oView As Inventor.View = oRow.Cells(0).Value Dim iHWND As Integer = 0 Try iHWND = oView.HWND If iHWND = myActiveView.HWND Then If oRow.Selected = False oRow.Selected = True oDGView.CurrentCell = oRow.Cells(2) Exit For End If End If oView = Nothing Catch End Try Next End If End Sub Private Sub oApp_OnCloseView(ViewObject As Inventor.View, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) If BeforeOrAfter=EventTimingEnum.kBefore Then RemoveRow(ViewObject) End If End Sub Private Sub oApp_OnNewView(ViewObject As Inventor.View, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) If BeforeOrAfter=EventTimingEnum.kAfter Then AddRow(ViewObject) End If End Sub Private Sub RemoveRow(ByVal View As Inventor.View) Dim myApp As Inventor.Application = m_Inv Dim myForm As WinForm = Me Dim myView As Inventor.View = View Dim oDGView As DataGridView=myForm.Controls.Find("DGView1", False).FirstOrDefault() For Each oRow As DataGridViewRow In oDGView.Rows Dim oView As Inventor.View = oRow.Cells(0).Value Dim iHWND As Integer = 0 Try iHWND = oView.HWND If iHWND = myView.HWND Then oDGView.Rows.Remove(oRow) Exit For End If oView=Nothing Catch ex As Exception MsgBox(ex.Message ) End Try Next End Sub Private Sub AddRow(ByVal View As Inventor.View) Dim myApp As Inventor.Application = m_Inv Dim myForm As WinForm = Me Dim myView As Inventor.View = View Dim oDGView As DataGridView = myForm.Controls.Find("DGView1", False).FirstOrDefault() myRefedDoc = myView.Document Dim sLabel As String If Not myRefedDoc.Propertysets(3).Item(14).value = String.Empty Then sLabel=myRefedDoc.Propertysets(3).Item(14).value Else sLabel=" --- NO NAME ---- " End If Dim oGV As New ViewItem(myView, myrefeddoc, sLabel) bindingSource1.insert(oDGView.Rows.Count, oGV) oDGView.Rows(oDGView.Rows.Count-1).Selected = True oDGView.CurrentCell = oDGView.Rows(oDGView.Rows.Count-1).Cells(2) End Sub End Class Public Class ViewItem Private oView As Inventor.View Private oDoc As Inventor.Document Private sBez As String Public Property View() As Inventor.View Get Return oView End Get Set(ByVal Value As Inventor.View) oView = Value End Set End Property Public Property Doc() As Inventor.Document Get Return oDoc End Get Set(ByVal Value As Inventor.Document) oDoc = Value End Set End Property Public Property Bez() As String Get Return sBez End Get Set(ByVal Value As String) sBez = Value End Set End Property Public Sub New(ByVal View As Inventor.View, ByVal Doc As Inventor.Document, ByVal Bez As String) oView=View oDoc = Doc sBez = bez End Sub End Class