Blank value obtained when retrieving the text property for a control

I'm currently working through a rather odd issue that I can't seem to figure out. I'm a novice programmer who is currently using to automate some lab equipment via the GPIB interface.

I'm using Excel to collect and tabulate my data. So the core of my problem is that I'm using a Backgroundworker to perform the "saving" of my data that I obtain from various lab equipment. Often times, I will have to record the assembly data of the specimens I'm testing. Here, I created another form with relevant fields to fill out all the data I'd ever desire to remember.

The backgroundworker thread will go into the secondary form, I've named it DAq6.vb, and pull the data. However, I've been noticing that it returns "" rather than the actual string that is present.

Public Function GetControlValue(ByVal ctl As Object) As String
    Dim text As String
        If ctl.InvokeRequired Then
            text = ctl.Invoke(New GetControlValueInvoker(AddressOf GetControlValue), ctl)
            If TypeName(ctl) = "ComboBox" Then
                text = ctl.Text
            ElseIf TypeName(ctl) = "NumericUpDown" Then
                text = ctl.value
            ElseIf TypeName(ctl) = "TextBox" Then
                text = ctl.Text
            ElseIf TypeName(ctl) = "RadioButton" Then
                text = ctl.Checked
            ElseIf TypeName(ctl) = "Decimal" Then
                text = ctl.Value
            ElseIf TypeName(ctl) = "Label" Then
                text = ctl.Text
                text = "Type name unknown"
            End If
        End If
        Return text
    Catch ex As Exception
        MsgBox("Error: " & Err.Description)
    End Try
End Function

Above is the function that I use to safely retrieve a "control value" from a different thread.

And below is representative of the method I use to obtain values from the form I'm currently in (DAq1.vb). This code works, and it successfully retrieves the strings.

objRange = objSheet1.Range("B1")
objRange.Value = GetControlValue(txtDate)

Here is code that doesn't work, curiously. I'm baffled as to why it wouldn't work.

objRange = objSheet6.Range("G2")
objRange.Value = GetControlValue(DAq6.txtLotNum1)

Why would this be the case? Again, I'm a novice programmer that is primarily self-taught in syntax, so I apologize if this causes any cringing. But I have dug around the internet looking for an issue for a few days now, and I'm stumped.

Thank you so much for even just reading all of this!

Edit: I've adjusted the function according to Bjørn-Roger's suggestions. After inserting a breakpoint right before the error producing lines of code, I see that it correctly goes to the TypeName(ctl) = "TextBox" portion of the if statement. Yet, it still returns a "" rather than the correct value.


Ensure you return code on all paths and that you reference the correct instance of DAq6.

Here's a simple example:

Public Class DAq1
    'Inherits Form

    Public Sub New()
    End Sub

    Public Function GetControlValue(ByVal ctl As Control) As String
        If (ctl Is Nothing) Then
            Throw New ArgumentNullException("ctl")
        End If
            If (ctl.InvokeRequired) Then
                Return CStr(ctl.Invoke(New GetControlValueInvoker(AddressOf GetControlValue), ctl))
                If (TypeOf ctl Is NumericUpDown) Then
                    Return DirectCast(ctl, NumericUpDown).Value.ToString()
                ElseIf (TypeOf ctl Is RadioButton) Then
                    Return DirectCast(ctl, RadioButton).Checked.ToString()
                    'Fallback to Control.Text
                    Return ctl.Text
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & Err.Description)
        End Try
        Return Nothing
    End Function

    Private Sub _DoWork(s As Object, e As DoWorkEventArgs) Handles worker.DoWork
        Dim f As DAq6 = DirectCast(e.Argument, DAq6)
        e.Result = Me.GetControlValue(f.txtLotNum1)
    End Sub

    Private Sub _Completed(s As Object, e As RunWorkerCompletedEventArgs) Handles worker.RunWorkerCompleted
        If (e.Error Is Nothing) Then
            MessageBox.Show(CStr(e.Result), Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            MessageBox.Show(e.Error.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub _Shown(s As Object, e As EventArgs) Handles Me.Shown
        Dim f As New DAq6()
        f.txtLotNum1.Text = "YES!"
    End Sub

    Private WithEvents worker As New BackgroundWorker
    Public Delegate Function GetControlValueInvoker(ctl As Control) As String

    Public Class DAq6
        Inherits Form
        Public ReadOnly txtLotNum1 As New TextBox
    End Class

End Class

Need Your Help

PHP foreach Construct Confusion

php mysql pdo foreach

I am having a hard time wrapping my head around the foreach construct. I have found numerous examples of course, but I never seem to be able to adapt them to my needs.

"INSERT INTO" doesn't work

php mysql sql sql-insert

I know there's couple of topics about why INSERT INTO doesn't work, but I didn't find any solutions for me.