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:
Option Strict On
Public Class MainForm
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Setup the layout of all 16 cameras at run time
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
BeginCameraLayout()
End Sub
Private Sub BeginCameraLayout()
Dim screens As Integer
Dim count As Integer = 1
Dim camWidth As Integer = CInt(Screen.PrimaryScreen.WorkingArea.Width / 4)
For screens = 1 To 16
'This adds the necessary controls to the camera forms
Dim liveStatusLabel As New Label
Dim f As New Form
f.Controls.Add(liveStatusLabel)
'Format the new form's properties before displaying
AddHandler vcLive.Status, New AxVIDEOCONTROLLib._DVideoControlEvents_StatusEventHandler(AddressOf vcLive_Status)
Return vcLive
End Function
Private Sub vcLive_Status(ByVal sender As Object, ByVal e As AxVIDEOCONTROLLib._DVideoControlEvents_StatusEvent)
If ((e.strMessage = "Status:Connection Established") OrElse e.strMessage.Contains("Error")) Then
If (e.strMessage = "Status:Connection Established") Then
f.vcLive.PlayLive() 'This line throws error. 'f' is not declared
End If
End If
End Sub
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.
Last edited by stateofidleness; May 4th, 2010 at 12:25 PM.
Re: Adding controls dynamically at runtime causing problems
Why not create a form at design time with everything all hooked up, then at run time, simply create new instances of the form?
then you could skip the call to f.Controls.Add(AddVideoScreen(screens))
And your status event would be contained in the form, then you could simply use Me. to get to the form instance as needed.
Re: Adding controls dynamically at runtime causing problems
Of course it doesn't know f, you haven't declared it. You've only declared a variable named f in the BeginCameraLayout method, which is a local variable and thus does not exist in any other method.
First of all, I think your AddHandler line is a bit strange. It seems you are creating an event delegate manually, which you shouldn't have to. Doesn't this work:
vb.net Code:
AddHandler vcLive.Status, AddressOf vcLive_Status
I'm not sure if it makes a difference, but it might.
Anyway, if that works, the 'sender' parameter in the vcLive_Status method should be the AxVideoControl object that raised the event. So, you can simply cast it, and use its methods:
vb.net Code:
Dim vcLive = DirectCast(sender, AxVIDEOCONTROLLib.AxVideoControl)
vcLive.PlayLive()
About the other error, I'm hoping it doesn't occur after you use this code, because I've no idea what could cause that.
EDIT
While my solution should work, and explains what is going wrong, techgnome's solution is better. Just put the control on a form during design-time and handle its event on that form.
EDIT2
But, you may get into 'trouble' with the CameraID integer. Instead of passing it when creating the camera control (which you now do during design-time probably, or still in code but only once), you can make your form accept the ID as an argument in its contstructor, or as a public property (or both).
vb.net Code:
Public Class CameraForm
Inherits Form
Public Sub New(ByVal cameraId As Integer)
Me.InitializeComponent() 'this is always required
vcLive.CameraID = cameraId 'vcLive is the name of the camera control on the form
'set other properties here, such as size and position
End Sub
'...
End Class
Now, you just pass the id when you create the form:
vb.net Code:
For screen As Integer = 1 To 16
Dim f As New CameraForm(screen) 'pass it here
'you don't need to set 38 gazillion properties here, you can set them in the forms constructor instead
'of course you still CAN set the properties here, it's just not as 'elegant' :)
f.Show()
Next
Last edited by NickThissen; May 4th, 2010 at 12:48 PM.
Re: Adding controls dynamically at runtime causing problems
ahh much appreciated guys.
I actually started out with a "template" form to use for all 16 cameras, but then wasn't sure if I'd need to create 16 of these forms or what.
As I may have different views (4 cameras 2x2, 16 cameras 4x4 etc), I would just set up my For loop accordingly? The properties of the form will change if I do this though which is why I can't declare them on the Form in design time. The .Top, .Left, .Width and .Height properties will all change depending on how many are showing.
How can I still dynamically create these forms (based off the form in Designer View) and still dynamically set their properties?
Re: Adding controls dynamically at runtime causing problems
You can set the properties that are always fixed (ShowInTaskbar, ShowIcon, ControlBox, etc, just a few I could see quickly) during design-time, or in the forms constructor or Form_Load event. You can then, after declaring a new form or after calling its Show method, set the other properties like you are doing now. It doesn't really matter though, you could also set all the properties like you are doing now. It just seems more appropriate to have most of these properties (well, as much as possible) in the form class itself so you don't clutter up your form creation code with stuff you can set elsewhere.
Re: Adding controls dynamically at runtime causing problems
don't use the AddHandler.... plop the control right onto the form then double click it, and set the event handler code just like any other control you are using... the reason to use add handler is if you want to dynamically link code to a controls (or a lot of controls to one event handler)... this is one case where it may not be working right for some reason.
Re: Adding controls dynamically at runtime causing problems
It seems you are not setting a whole load of properties on the AxVideoPlayer control that you were setting before (CameraID, Username, Password, IPAddress, etc). Not sure if that is related.
Anyway, you don't need to use AddHandler when you added your control during design-time. Just handle its Status event as you would handle a button's Click event. That said, AddHandler should work just fine too, so that can't cause your problem.
Re: Adding controls dynamically at runtime causing problems
as I have no documentation with the correct usage of the .dll, I'm kinda shooting in the dark. Removing the Addhandler and doing it through design-view worked a charm. The event is now firing as expected.
The problem I'm having is I don't think I am supposed to create a new connection for each camera with separate logins etc..
I think I need to set all that and then connect, and while connected get a different cameraID for each ActiveX control.. I'm just not sure how to do that
Pseudo of what I think should be done
Set properties for the connection
Connect
Load 16 Form instances
Load a different CameraID for each form