Results 1 to 10 of 10

Thread: Adding controls dynamically at runtime causing problems

Threaded View

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Adding controls dynamically at runtime causing problems

    Hey fellas,
    So what I'm trying to do is build 16 forms dynamically at runtime (all will be identical). These 16 forms each will display a camera, which is using an ActiveX control made by a 3rd party. I am able to create the forms correctly and provide all necessary parameters for each control, but since I have to declare my own AddHandler event which checks if the camera connected, I need to somehow reference the ActiveX control at runtime, but I can't tell it which form it's on because it says it's not declared.

    Here is the code I'm using, Line 83 is where error occurs (in the Status Event)

    vb.net Code:
    1. Option Strict On
    2. Public Class MainForm
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         'Setup the layout of all 16 cameras at run time
    6.         Me.Width = Screen.PrimaryScreen.WorkingArea.Width
    7.         BeginCameraLayout()
    8.     End Sub
    9.  
    10.     Private Sub BeginCameraLayout()
    11.         Dim screens As Integer
    12.         Dim count As Integer = 1
    13.         Dim camWidth As Integer = CInt(Screen.PrimaryScreen.WorkingArea.Width / 4)
    14.  
    15.         For screens = 1 To 16
    16.             'This adds the necessary controls to the camera forms
    17.             Dim liveStatusLabel As New Label
    18.             Dim f As New Form
    19.             f.Controls.Add(liveStatusLabel)
    20.             'Format the new form's properties before displaying
    21.             f.Width = CInt(Screen.PrimaryScreen.WorkingArea.Width / 4)
    22.             f.Height = CInt((Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 4)
    23.             f.ShowInTaskbar = False
    24.             f.ShowIcon = False
    25.             f.ControlBox = False
    26.             f.Text = "Camera " & screens
    27.             f.MinimizeBox = False
    28.             f.Show()
    29.             f.Controls.Add(AddVideoScreen(screens)) 'Call to create the ActiveX control
    30.             Select Case screens
    31.                 Case 1, 2, 3
    32.                     f.Top = Me.Height
    33.                     f.Left = (camWidth * count) - camWidth
    34.                     count += 1
    35.                 Case 4
    36.                     f.Top = Me.Height
    37.                     f.Left = (camWidth * count) - camWidth
    38.                     count = 1
    39.                 Case 5, 6, 7
    40.                     f.Top = Me.Height + f.Height
    41.                     f.Left = (camWidth * count) - camWidth
    42.                     count += 1
    43.                 Case 8
    44.                     f.Top = Me.Height + f.Height
    45.                     f.Left = (camWidth * count) - camWidth
    46.                     count = 1
    47.                 Case 9, 10, 11
    48.                     f.Top = Me.Height + f.Height + f.Height
    49.                     f.Left = (camWidth * count) - camWidth
    50.                     count += 1
    51.                 Case 12
    52.                     f.Top = Me.Height + f.Height + f.Height
    53.                     f.Left = (camWidth * count) - camWidth
    54.                     count = 1
    55.                 Case 13, 14, 15
    56.                     f.Top = Me.Height + f.Height + f.Height + f.Height
    57.                     f.Left = (camWidth * count) - camWidth
    58.                     count += 1
    59.                 Case 16
    60.                     f.Top = Me.Height + f.Height + f.Height + f.Height
    61.                     f.Left = (camWidth * count) - camWidth
    62.             End Select
    63.         Next
    64.     End Sub
    65.  
    66.     Private Function AddVideoScreen(ByVal screen As Integer) As AxVIDEOCONTROLLib.AxVideoControl
    67.         Dim vcLive As New AxVIDEOCONTROLLib.AxVideoControl
    68.         vcLive.Dock = DockStyle.Fill
    69.         vcLive.DLLPath = Application.StartupPath & "\TL4516UL.dll"
    70.         vcLive.UserName = "live"
    71.         vcLive.Password = "live"
    72.         vcLive.IPAddress = "100.100.100.100"
    73.         vcLive.Port = 53705
    74.         vcLive.CameraID = screen
    75.         vcLive.Connect()
    76.         AddHandler vcLive.Status, New AxVIDEOCONTROLLib._DVideoControlEvents_StatusEventHandler(AddressOf vcLive_Status)
    77.         Return vcLive
    78.     End Function
    79.  
    80.     Private Sub vcLive_Status(ByVal sender As Object, ByVal e As AxVIDEOCONTROLLib._DVideoControlEvents_StatusEvent)
    81.         If ((e.strMessage = "Status:Connection Established") OrElse e.strMessage.Contains("Error")) Then
    82.             If (e.strMessage = "Status:Connection Established") Then
    83.                f.vcLive.PlayLive() 'This line throws error. 'f' is not declared          
    84.             End If
    85.         End If
    86.     End Sub
    87.  
    88. End Class


    Also, if I comment out the error line, just to test, I get this error:

    A first chance exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.VIDEOCONTROLLib.dll

    on the line referencing the .dll path. I am thinking it may be because after the first iteration, the .dll is in use and therefore future iterations can't use it, causing the error. I'm trying to take a very OOP approach to this project (keeping things in their own subs and whatnot) but the referencing and scope is throwing me off quite a bit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width