user3314404
user3314404

Reputation: 79

VB.net change text in button control with variable

How can I change a specific control, with variable .text

If Me.Controls.OfType(Of Button) = ("Button" & Bindex) Then
  button.Text = PostMyresult
End If

So , no Joy, attached is the code block. Hope its not too polluted:

` Private Sub Button34_Click(sender As Object, e As EventArgs) Handles Button34.Click

    Dim DaButton As String
    For Bindex As Integer = 1 To 2

        DaButton = ("Button" & Bindex & ".Text = SAMPLE" & Bindex)
        MessageBox.Show(DaButton)
        ' Me.Button1.Text = "SAMPLE1"
        CollectSample(DaButton & ".Text" & ".txt")  'works grabs sample from spectrum anlzr
        CheckSample(DaButton & ".Text" & ".txt")    'works error check

        ' MessageBox.Show(SampleFlag)
        If SampleFlag <> 0 Then
            SendMyAvg = Math.Round(SendMyAvg, 2)
            If SendMyAvg < 1 Then
                MessageBox.Show("Average Sample Is < 1 ")
                ' Me.Button1.Text = "SAMPLE1"
                For Each myCtrl As Control In Me.Controls
                    If (TypeOf myCtrl Is Button) Then
                        If myCtrl.Name = "Button" & Bindex Then
                            myCtrl.Text = DaButton
                        End If
                    End If
                Next

                Exit Sub
            End If
            '  MessageBox.Show("Button" & Bindex & " / " & SendMyAvg)

            For Each myCtrl As Control In Me.Controls
                If (TypeOf myCtrl Is Label) & myCtrl.Name = "Label" & Bindex Then
                    myCtrl.Text = SendMyAvg
                    MessageBox.Show("Button" & Bindex & " / " & SendMyAvg)
                End If
            Next
            '   Button1.Text = SendMyAvg
            '  MsgBox("Avg Is " & SendMyAvg)
        End If
    Next
End Sub`

Upvotes: 0

Views: 3170

Answers (4)

Void LT
Void LT

Reputation: 5

Try this:

    Dim c As Control
    Dim myButton As String

    For i = 1 To 2
        myButton = "Button" & i
        c = Controls.Find(myButton, True).FirstOrDefault()
        c.Text = "New Text"
    Next i

Upvotes: 0

user3314404
user3314404

Reputation: 79

solution:

DirectCast(Me.Controls.Find("Label" & Bindex, True)(0), Label).Text = SendMyAvg

Upvotes: 1

Nathu
Nathu

Reputation: 262

Well if you have Linq, you can do this:

    Dim btn = Me.Controls.OfType(Of Button).Where(Function(x) x.Name = "Button1" & Bindex)
    If btn.Count > 0 Then
        btn(0).Text = "New Text"
    End If

Upvotes: 2

Aethan
Aethan

Reputation: 1985

Try this:

  For Each myBtn as Button In Me.Controls.OfType(Of Button)
       If myBtn.Name = "Button" & Bindex Then
            myBtn.Text = PostMyResult
       End If
  Next

Upvotes: 1

Related Questions