VB.net使用变量更改按钮控件中的文本

时间:2016-01-24 23:58:14

标签: vb.net

如何使用变量.text

更改特定控件
If Me.Controls.OfType(Of Button) = ("Button" & Bindex) Then
  button.Text = PostMyresult
End If

所以,没有Joy,附加的是代码块。希望它不会受到太多污染:

`Private Sub Button34_Click(sender As Object,e As EventArgs)处理Button34。点击

    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`

4 个答案:

答案 0 :(得分:2)

如果你有Linq,你可以这样做:

    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

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:1)

溶液:

DirectCast(Me.Controls.Find(“Label”&amp; Bindex,True)(0),Label).Text = SendMyAvg

答案 3 :(得分:0)

尝试一下:

    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