Select runtime made controls

Hello, I' have a problem:

First I tell you what the aim is: I make a visualisation of a machine. On loading the visualisation form, i make new pictureboxes with the picture of (for example) a grey motor. In my main form, I make a connection with a PLC. Every 500ms I get an update from the status of the machine. The main form checks what elements had changed, and if they are used in this visualisation form, update them in this form. Now the problem is to select this exact picturebox, to change its picture to (for example) a green or red motor when the status is on or fault.

Here the code on for making the new pictureboxes:

Public Sub NewPicture(ByVal ID As Integer, ByVal Type As Integer, ByVal PositieX As Integer, ByVal PositieY As Integer, ByVal Tooltip As String)

'Create the picture

Dim Pic As New PictureBox

'Specify the location and te size

Pic.Location = New System.Drawing.Point(PositieX, PositieY)

Pic.Size = New System.Drawing.Size(100, 100)

Pic.SizeMode = PictureBoxSizeMode.AutoSize

Pic.Name = "Pic" & Str(ID)

Pic.Image = Tekening_Ophalen(Type, 0) 'this function selects the type of picture: small motor/medium motor/Blower, ....

Pic.BackColor = Color.Transparent

Pic.BorderStyle = BorderStyle.None

ToolTipVisualisatieElementen.SetToolTip(Pic, Tooltip)

'Add it to the forms control collection

Me.Controls.Add(Pic)

'Link the event to the event handler

AddHandler Pic.Click, AddressOf Me.VisualisationElementClick

End Sub

The code for loading the form:

Private Sub FrmVisualisatieBiologie_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim teller As Integer

For teller = 0 To aantal_VisualisatieElementen - 1 '

Call NewPicture(VisElBiologie(teller).ID, VisElBiologie(teller).Type, VisElBiologie(teller).Positie.X, VisElBiologie(teller).Positie.Y, VisElBiologie(teller).Tooltiphelp)

Next

End Sub

So far so good, this works...

Now the aim is that the main form gives a list of elements that were the status was changed in the way or an array with 2 colums : (VisElBiologie().ID, Status).

Public Sub Update_Status(ByVal ID() As Integer, ByVal Status() As Integer)

Dim Teller As Integer

Dim Pic As PictureBox

Pic.Name = "Pic" & Str(ID(Teller))

For Teller = 0 To ID.Length - 1

Pic.Image = Tekening_Ophalen(ID(Teller), Status(Teller))

Next

End Sub

How can I assign this pic to the previous created picturebox (see The fault given is the folowing: "Warning 1 Variable 'Pic' is used before it has been assigned a value. A null reference exception could result at runtime."

Thanks in advance

Paul



Answer this question

Select runtime made controls

  • Dan L

    Solution Found !!!!

    Public Sub Update_Status(ByVal ID() As Integer, ByVal Status() As Integer)

    Dim Teller As Integer

    Dim Teller1 As Integer

    Dim Pic As PictureBox

    Dim lstControls As Control()

    Dim Naam As String

    For Teller = 0 To ID.Length - 1 'Counter Teller loops through all the changed elements

    Naam = "Pic" & Format(ID(Teller), "0000") 'Create the name of how the picturebox should be like

    lstControls = Me.Controls.Find(Naam, True)

    For Teller1 = 0 To lstControls.Length - 1 'LstControls = List of all the controls with this name

    Pic = lstControls(Teller1) 'Assign picturebox to a control in the list of controls

    Pic.Image = Tekening_Ophalen(ID(Teller), Status(Teller)) 'this function selects the type of picture: small motor/medium motor/Blower, .... and its state: 0=OFF, 1=ON, 2=Fault, ...

    Next

    Next

    End Sub


  • MrBradford

    Perhaps you would do it the other way around:

    For the visible form, go through all the controls which need updating and grab the appropriate Data Point. You can store information in the TAG of each object which references the data source (This Tag object can reference pretty much anything). In this way, you don't need a reference to the control, and each Data Point can actually update the status of multiple Controls.



  • Select runtime made controls