Hi everyone,

I was wondering if it is possible to point to a textbox created in the form designer via a string variable. Basically, I'm creating a golf management system with a feature allowing a user to define their own course. Each hole has a corresponding textbox into which a user can input the hole's par and then the system should total up the first 9 pars, the last 9 pars and then all 18 pars; displayed in 3 different textboxes.

The input textboxes have been named txtP1, txtP2, ..., txtP18, so I was wondering if I could simply concatenate a loop control variable onto a constant of "txtP" to point to each input box.

This is what I have at the moment, but it's throwing up errors saying that .text is not a member of string.

Sub UpdatePar()
        Dim inpar, outpar, totalpar As Integer
        Dim pointer As String

        For hole = 1 To 18
            pointer = "txtP" + Str(hole)
            If hole <= 9 Then
                outpar += Cint(pointer.text)
            End If
            If hole >= 10 Then
                inpar += Cint(pointer.text)
            End If

            totalpar = inpar + outpar

            txtIn.Text = inpar
            txtOut.Text = outpar
            txtTotalPar.Text = totalpar
End Sub

Is there a way to achieve this in Express 2008? Any help would be greatly appreciated.

Recommended Answers

All 2 Replies

See if this helps.

Public Class Form1
    Private inPar, outPar As Integer '// Declared once.

    Private Sub txtP1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                    Handles txtP1.TextChanged, txtP2.TextChanged, txtP3.TextChanged, _
                                    txtP18.TextChanged, txtP16.TextChanged, txtP17.TextChanged '// add your other txtP# TextBoxes here.
        txtIn.Text = CStr(inPar) : txtOut.Text = CStr(outPar) : txtTotalPar.Text = CStr(inPar + outPar)
    End Sub

    Sub UpdatePar()
        inPar = 0 : outPar = 0  '// reset.
        For hole As Integer = 1 To 18
            For Each ctl As Control In Me.Controls '// Loop thru all Controls on Form.
                '// Locate TextBox AndAlso with the proper Name AndAlso make sure it is Numeric for adding under par scores.
                If TypeOf (ctl) Is TextBox AndAlso ctl.Name = "txtP" & hole.ToString AndAlso IsNumeric(ctl.Text) Then
                    If hole <= 9 Then If Not ctl.Text = "" Then outPar += CInt(ctl.Text) '// if TextBox has a value, add.
                    If hole >= 10 Then If Not ctl.Text = "" Then inPar += CInt(ctl.Text) '// if TextBox has a value, add.
                    Exit For '// Exit the loop since TextBox was located.
                End If
    End Sub
End Class
commented: Fast response, useful code and well explained through notes. +0

Thanks for the quick reply codeorder! This works perfectly =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.