Hey all,


I am having trouble converting a string value to a text box name.
The text boxes are created at runtime and I need to get the text value out so I can store them. I keep getting the error


Error 24 Value of type 'String' cannot be converted to 'System.Windows.Forms.TextBox'.
Here may code

PartName = CType("Txtdynamic" & intcountt & "_mycntrl", TextBox).text

wHAT DO I need to do .
Please really need to sort this


Thanks

James

Dim txt As Object = Me.Controls.Find("Txtdynamic" & intcountt & "_mycntrl", True)(0)
        If txt Is Nothing Then
            MsgBox("textbox not found")
        Else
            MsgBox(CType(txt, TextBox).Text)
        End If
Dim txt As Object = Me.Controls.Find("Txtdynamic" & intcountt & "_mycntrl", True)(0)
        If txt Is Nothing Then
            MsgBox("textbox not found")
        Else
            MsgBox(CType(txt, TextBox).Text)
        End If

Thanks for that but still no go now I get the is error

Unable to cast object of type 'System.Windows.Forms.Control[]' to type 'System.Windows.Forms.TextBox'.


Any ideas
Thanks

Edited 6 Years Ago by JAM1011: spelling

Dim txt As Object = Me.Controls.Find("Txtdynamic" & intcountt & "_mycntrl", True)(0)
        If txt Is Nothing Then
            MsgBox("textbox not found")
        Else
            MsgBox(CType(txt, TextBox).Text)
        End If

Thanks for that but still no go know I get the is error

Unable to cast object of type 'System.Windows.Forms.Control[]' to type 'System.Windows.Forms.TextBox'.


Any ideas
Thanks

Dim txt As Object = Me.Controls.Find("Txtdynamic" & intcountt & "_mycntrl", True)(0)
        If txt Is Nothing Then
            MsgBox("textbox not found")
        Else
            MsgBox(CType(txt, TextBox).Text)
        End If

Thanks for that but still no go know I get the is error

Unable to cast object of type 'System.Windows.Forms.Control[]' to type 'System.Windows.Forms.TextBox'.


Any ideas
Thanks

Then something is wrong on your code. here is a complete example....

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For intcountt As Integer = 0 To 10
            Dim txt As New TextBox
            With txt
                .AutoSize = True
                .Location = New System.Drawing.Point(3, 33 * intcountt)
                .Name = "Txtdynamic" & intcountt & "_mycntrl"
                .Size = New System.Drawing.Size(150, 23)
                .TabIndex = intcountt
                .Text = "im control nr." & intcountt
                AddHandler .TextChanged, AddressOf txtTextChanged
            End With
            Me.Controls.Add(txt)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txt As Object = Me.Controls.Find("Txtdynamic4_mycntrl", True)(0)
        If txt Is Nothing Then
            MsgBox("textbox not found")
        Else
            MsgBox(CType(txt, TextBox).Text)
        End If
    End Sub

    Private Sub txtTextChanged(ByVal sender As Object, ByVal e As EventArgs)
        'whatever should happen here
    End Sub
This article has been dead for over six months. Start a new discussion instead.