Neil Johnson - 2011-03-01

Hi everyone

I've created an HMI for use with a SLC over ethernet, which is working well. I'd like to go further by creating a simple text input box, into which the user can type a numeric value. This would be updated in the relevant PLC location upon pressing enter.

I've begun to adapt some code as follows, but when I build the project and try to drag this control onto the main form, it generates the error, "Object reference not set to an instance of an object". I'm baffled, as this is just an adapted copy of some other controls which are working fine. Any suggestions?

Public Class TextInputBox
    Inherits TextBox
#Region "Properties"
    '*****************************************************
    '* Property - Component to communicate to PLC through
    '*****************************************************
    Private m_CommComponent As ICommComponent
    Public Property CommComponent() As ICommComponent
        Get
            Return m_CommComponent
        End Get
        Set(ByVal value As ICommComponent)
            m_CommComponent = value
        End Set
    End Property
    '*****************************************
    '* Property - Address in PLC to Link to
    '*****************************************
    Private m_PLCaddress As String = "N7:0"
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Public Property PLCaddress() As String
        Get
            Return m_PLCaddress
        End Get
        Set(ByVal value As String)
            m_PLCaddress = value
        End Set
    End Property
    '*****************************************
    '* Property - What to do to bit in PLC
    '*****************************************
    Public Enum OutputTypes
        MomentarySet
        MomentaryReset
        SetTrue
        SetFalse
        Toggle
    End Enum
    Private m_OutputType As OutputTypes = OutputTypes.MomentarySet
    Public Property OutputType() As OutputTypes
        Get
            Return m_OutputType
        End Get
        Set(ByVal value As OutputTypes)
            m_OutputType = value
        End Set
    End Property
    '* This is necessary to make the background draw correctly
    '*  http://www.bobpowell.net/transcontrols.htm
    '*part of the transparent background code
    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or 32
            Return cp
            Return MyBase.CreateParams
        End Get
    End Property
#End Region
#Region "Events"
    '****************************
    '* Event - Enter key
    '****************************
    Private Sub MomentaryButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.Enter
        If m_PLCaddress <> "" Then
            Try
                Select Case m_OutputType
                    'Momentary set high function'
                    Case OutputTypes.MomentarySet : m_CommComponent.WriteData(m_PLCaddress, TextBox1.Text)
                End Select
            Catch
            End Try
        End If
    End Sub
    '********************************************************************
    '* When an instance is added to the form, set the comm component
    '* property. If a comm component does not exist, add one to the form
    '********************************************************************
    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        If Me.DesignMode Then
            '********************************************************
            '* Search for ICommComponent component in parent form
            '* If one exists, set the client of this component to it
            '********************************************************
            Dim i = 0
            Dim j As Integer = Me.Parent.Site.Container.Components.Count - 1
            While m_CommComponent Is Nothing And i < j
                If Me.Parent.Site.Container.Components(i).GetType.GetInterface("ICommComponent") IsNot Nothing Then m_CommComponent = Me.Parent.Site.Container.Components(i)
                i += 1
            End While
            '************************************************
            '* If no comm component was found, then add one and
            '* point the CommComponent property to it
            '*********************************************
            If m_CommComponent Is Nothing Then
                Me.Parent.Site.Container.Add(New EthernetIPforSLCMicro)
                m_CommComponent = Me.Parent.Site.Container.Components(Me.Parent.Site.Container.Components.Count - 1)
            End If
        End If
    End Sub
#End Region
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        'TextBox1
        Me.TextBox1.Location = New System.Drawing.Point(0, 0)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        Me.ResumeLayout(False)
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub
End Class