visual basic变量按钮名称

时间:2017-11-21 14:07:47

标签: vb.net

在那

async

我不能这样工作。

我有按钮名称,如Dim btnid As Integer dgkayitlar.Rows(selectedrow).Cells(4).Value = btnid Form2.btnOda(btnid).BackColor = Color.Red btnOda101btnOda201

我想使用btnOda301代替101,102。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用像这样的词典

public btnOda as Dictionary(Of Integer, Button)

public Sub InitButtons()
    btnOda = new Dictionary(Of Integer, Button)()
    btnOda.add(101, btnOda101)
    btnOda.add(102, btnOda102)
    btnOda.add(301, btnOda301)
    '...

End Sub

public Sub MyFunc()
    Dim btnid As Integer
    btnOda(btnid).BackColor = Color.Red

End Sub

答案 1 :(得分:0)

你可以这样做。您将需要使用格式为btnOda_101的按钮名称,下划线允许代码拆分id

Dim btnId As Integer = 101

'Get list of buttons
Dim buttons = From c In Controls Where TypeOf (c) Is Button Select c

'Find the button with the desired ID
Dim button = (From b In buttons Where b.name.ToString.Split("_")(1) = btnId Select b).FirstOrDefault

'Use that button
Dim myButton As Button = CType(button, Button)
myButton.BackColor = Color.Red