VB.net使用变量更改按钮控件中的文本
如何使用variable.text更改特定控件VB.net使用变量更改按钮控件中的文本,vb.net,Vb.net,如何使用variable.text更改特定控件 If Me.Controls.OfType(Of Button) = ("Button" & Bindex) Then button.Text = PostMyresult End If 所以,没有快乐,附加的是代码块。希望它不要太污染: `私有子按钮34\u单击(发送者作为对象,e作为事件参数)处理按钮34。单击 Dim DaButton As String For Bindex As Integer = 1 To
If Me.Controls.OfType(Of Button) = ("Button" & Bindex) Then
button.Text = PostMyresult
End If
所以,没有快乐,附加的是代码块。希望它不要太污染:
`私有子按钮34\u单击(发送者作为对象,e作为事件参数)处理按钮34。单击
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`
Dim DaButton作为字符串
对于Bindex,整数=1到2
DaButton=(“按钮”&Bindex&“.Text=SAMPLE”&Bindex)
MessageBox.Show(DaButton)
'Me.Button1.Text=“SAMPLE1”
CollectSample(DaButton&“.Text”&“.txt”)“作品从光谱采集样本
CheckSample(DaButton&“.Text”&“.txt”)“工作错误检查
'MessageBox.Show(SampleFlag)
如果SampleFlag为0,则
sendmayavg=Math.Round(sendmayavg,2)
如果SendMyAvg<1,则
MessageBox.Show(“平均样本数<1”)
'Me.Button1.Text=“SAMPLE1”
对于Me.Controls中的每个myCtrl作为控件
如果(myCtrl的类型为按钮),则
如果myCtrl.Name=“Button”&Bindex,则
myCtrl.Text=DaButton
如果结束
如果结束
下一个
出口接头
如果结束
'MessageBox.Show(“按钮”&Bindex&“/”&SendMyAvg)
对于Me.Controls中的每个myCtrl作为控件
如果(myCtrl的类型是Label)&myCtrl.Name=“Label”&Bindex,那么
myCtrl.Text=SendMyAvg
MessageBox.Show(“按钮”&Bindex&“/”&SendMyAvg)
如果结束
下一个
'按钮1.Text=SendMyAvg
'MsgBox(“平均值为”&SendMyAvg)
如果结束
下一个
端接头`
试试这个:
For Each myBtn as Button In Me.Controls.OfType(Of Button)
If myBtn.Name = "Button" & Bindex Then
myBtn.Text = PostMyResult
End If
Next
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
试试这个:
For Each myBtn as Button In Me.Controls.OfType(Of Button)
If myBtn.Name = "Button" & Bindex Then
myBtn.Text = PostMyResult
End If
Next
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
如果你有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
如果你有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
解决方案:
DirectCast(Me.Controls.Find(“Label”&Bindex,True)(0),Label)。Text=SendMyAvg解决方案:
DirectCast(Me.Controls.Find(“Label”&Bindex,True)(0),Label)。Text=SendMyAvg尝试以下操作:
For Each myBtn as Button In Me.Controls.OfType(Of Button)
If myBtn.Name = "Button" & Bindex Then
myBtn.Text = PostMyResult
End If
Next
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
试试这个:
For Each myBtn as Button In Me.Controls.OfType(Of Button)
If myBtn.Name = "Button" & Bindex Then
myBtn.Text = PostMyResult
End If
Next
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
Me.Controls.OfType(Of Button)
将是作为按钮的控件的子集。您需要迭代并找到所需的控件。Me.Controls.OfType(Of Button)
将是作为按钮的控件的子集。如果你在Me.Controls.OfType(Of button)中为每个btn As按钮使用,那么你需要迭代并找到你想要的按钮,这将改变每个按钮,而不仅仅是他/她正在寻找的按钮,你可以去掉第一个If块。然后在选项下进行检查,谢谢您提供更多的知识!答案已编辑。你说得对。它还将减少必须迭代的控件。再次感谢。我建议添加一个。Where(…)
子句,而不是使用If
。没有乐趣,因为这是我的第一个VB项目,可能遗漏了一些东西。这将改变每个按钮,而不仅仅是他/她正在寻找的按钮,如果你在我的每个btn中使用作为按钮。控件。按钮的类型有点像OP,你可以去掉第一个If块。然后在选项下进行检查,谢谢您提供更多的知识!答案已编辑。你说得对。它还将减少必须迭代的控件。再次感谢。我建议添加一个。Where(…)
子句,而不是使用If
。没有乐趣,因为这是我的第一个VB项目,可能遗漏了一些东西。对于给出否定答案的人。为什么?不是我的否定意见,但这可能是因为您选择的按钮的文本属性是“Button1”+Bindex
,而问题可能是询问的按钮的名称属性是“Button1”&Bindex
(请注意,最好在VB中使用字符串连接运算符-&哦)。嗯,我不认为这真的值得否定,但谢谢你。编辑。给给出否定答案的人。为什么?不是我的否定意见,但这可能是因为您选择的按钮的文本属性是“Button1”+Bindex
,而问题可能是询问的按钮的名称属性是“Button1”&Bindex
(请注意,最好在VB中使用字符串连接运算符-&哦)。嗯,我不认为这真的值得否定,但谢谢你。编辑。