troubleshooting Question

TextBox LostFocus event using wrong control as Sender?

Avatar of GIANTOCR
GIANTOCR asked on
Visual Basic.NET
9 Comments1 Solution349 ViewsLast Modified:
I created a new control, ValueTextBox, which is a TextBox with a few extra properties.

On Form load, the form dynamically adds 6 of these ValueTextBoxes and assigns a LostFocus event to the control that is supposed to validate the data by comparing it to some of the custom properties added to the ValueTextBox control.

When I type a value in the first ValueTextBox then click on the second ValueTextBox I get the following error "Conversion from string "" to type 'Double' is not valid." While still in Debugger mode I mouse hover over Sender in the LostFocus parameters and look at the Senders properties. It has the properties of the second ValueTextBox, ie the box that is getting focus instead of the box that is losing focus. Since this box is empty, it is trying to perform some operations on an empty and it does not like it.

Why is the Sender not the ValueTextBox that is losing focus, but the ValueTextBox that is getting focus?

While I am at it, I am not sure why the code is evaluating the empty value since it is inside an if then loop (If text_box.Text IsNot "" Then) supposed to keep empty boxes from being evaluated, so I have more than one problem here.

Thanks in advance for any assistance you may provide.
Public Class ValueTextBox
    Inherits TextBox
    Public hlim As Double
    Public llim As Double
    Public himess As String
    Public lomess As String
End Class
    Private Sub ValueTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        'Dim text_box As ValueTextBox = DirectCast(sender, ValueTextBox)
        Dim text_box As ValueTextBox = sender
        Dim val As Double = 0
        If text_box.Text IsNot "" Then
            val = text_box.Text
            If text_box.llim <> Nothing AndAlso val <= text_box.llim OrElse text_box.hlim <> Nothing AndAlso val >= text_box.hlim Then
                Dim msg As String
                Dim title As String
                Dim style As MsgBoxStyle
                Dim response As MsgBoxResult
                msg = "Verify that reading is correct"   ' Define message.
                style = MsgBoxStyle.YesNo
                title = "Reading of Out of Limits"   ' Define title.
                ' Display message.
                response = MsgBox(msg, style, title)
                If response = MsgBoxResult.Yes Then   ' User chose Yes.
                    If text_box.hlim <> Nothing AndAlso val >= text_box.hlim Then
                        title = "Warning!! Reading is above high limit."
                        If text_box.himess IsNot Nothing Then
                            msg = text_box.himess
                            msg = ""
                        End If
                        style = MsgBoxStyle.OkOnly
                        MsgBox(msg, style, title)
                    ElseIf text_box.llim <> Nothing AndAlso val <= text_box.llim Then
                        title = "Warning!! Reading is below low limit."
                        If text_box.lomess IsNot Nothing Then
                            msg = text_box.lomess
                            msg = ""
                        End If
                        style = MsgBoxStyle.OkOnly
                        MsgBox(msg, style, title)
                    End If
                    Exit Sub
                End If
            End If
        End If
    End Sub
    Private Sub ValueTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If IsNumeric(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub
    Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lblTitle As String = "Boiler 1 Readings"
        Label1.Text = lblTitle
        ReDim dpGroup(6)
        dpGroup(0) = New dp1
        dpGroup(0).dpName = "BLR1_Pres"
        dpGroup(0).dpDC = "Boiler 1 Pres"
        dpGroup(0).limLO = 50
        dpGroup(0).limHi = 70
        dpGroup(0).messHi = "Boiler 1 pressure is high."
        dpGroup(0).messLo = "Boiler 1 pressure is low."
        dpGroup(0).dpType = "A"
        dpGroup(1) = New dp1
        dpGroup(1).dpName = "FW_Temp"
        dpGroup(1).dpDC = "Feedwater Temp"
        dpGroup(1).limLO = 130
        dpGroup(1).limHi = 165
        dpGroup(1).messHi = "Feedwater temp is high."
        dpGroup(1).messLo = "Feedwater temp is low."
        dpGroup(1).dpType = "A"
        dpGroup(2) = New dp1
        dpGroup(2).dpName = "BLR_CHEM_Lev"
        dpGroup(2).dpDC = "Chemical Level"
        dpGroup(2).limLO = 140
        dpGroup(2).limHi = 200
        dpGroup(2).messHi = "Chemical level is high."
        dpGroup(2).messLo = "Call Water Treatment Company."
        dpGroup(2).dpType = "A"
        dpGroup(3) = New dp1
        dpGroup(3).dpName = "BLR2_Pres"
        dpGroup(3).dpDC = "Boiler 2 Pres"
        dpGroup(3).limLO = 50
        dpGroup(3).limHi = 70
        dpGroup(3).messHi = "Boiler 2 pressure is high."
        dpGroup(3).messLo = "Boiler 2 pressure is low."
        dpGroup(3).dpType = "A"
        dpGroup(4) = New dp1
        dpGroup(4).dpName = "CW_Temp"
        dpGroup(4).dpDC = "Citywater Temp"
        dpGroup(4).limLO = 50
        dpGroup(4).limHi = 80
        dpGroup(4).messHi = "Citywater temp is high."
        dpGroup(4).messLo = "Citywater temp is low."
        dpGroup(4).dpType = "A"
        dpGroup(5) = New dp1
        dpGroup(5).dpName = "BLR_INHB_Lev"
        dpGroup(5).dpDC = "Inhibitor Level"
        dpGroup(5).limLO = 140
        dpGroup(5).limHi = 200
        dpGroup(5).messHi = "Inhibitor level is high. "
        dpGroup(5).messLo = "Call Water Treatment Company"
        dpGroup(5).dpType = "A"
        For i As Integer = 0 To 5
            Dim txtValue As New ValueTextBox
            Dim lblDatapoint As New Label
            ' Set up some properties for the label.
            lblDatapoint.Text = dpGroup(i).dpDC
            lblDatapoint.Location = New Point(controlLocation.X, controlLocation.Y + 3)
            ' Set up some properties for the TextBox.
            txtValue.Name = "txtValue" & CStr(i)
            txtValue.Tag = dpGroup(i).dpName
            txtValue.hlim = dpGroup(i).limHi
            txtValue.llim = dpGroup(i).limLO
            txtValue.himess = dpGroup(i).messHi
            txtValue.lomess = dpGroup(i).messLo
            txtValue.Location = New Point(controlLocation.X + 120, controlLocation.Y)
            txtValue.Size = New Size(80, txtValue.Height)
            ' Add the label and TextBox to the controls collection.
            AddHandler txtValue.LostFocus, AddressOf ValueTextBox_LostFocus
            AddHandler txtValue.KeyPress, AddressOf ValueTextBox_KeyPress
            ' Increment the m_LocationY so the next control won't overwrite it.
            controlLocation.Y += txtValue.Height + 5
    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros