1

我在编写某些按钮时遇到问题。这是我到目前为止所得到的:

Public Class Form1

Dim Button(12) As Button
Dim X As Integer

Private Sub EventName()
    Dim message, title, defaultValue As String
    Dim myValue As Object
    If Label4.Text = "Admin" Then
        ' Set prompt.
        Message = "Enter Product Name"
        ' Set title.
        title = "Product Name"
        defaultValue = ""   ' Set default value.

        'Display message, title, and default value.
        myValue = InputBox(Message, title, defaultValue)

        Button(X).Text = myValue
    End If
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click
    'Dim message, title, defaultValue As String
    'Dim myValue As Object
    For Me.X = 1 To 10

        >>>>>    <<<<<<<

    Next
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Button(1) = Button1
    Button(2) = Button2
    Button(3) = Button3
    Button(4) = Button4
    Button(5) = Button5
    Button(6) = Button6
    Button(7) = Button7
    Button(8) = Button8
    Button(9) = Button9
    Button(10) = Button10
    Button(11) = Button11
    Button(12) = Button12
End Sub

我想要做的是,如果我单击按钮 7,则会出现一个输入框供用户输入按钮的名称。无论我在 之间尝试了什么>>>>> <<<<<,我似乎都无法做到正确。

4

1 回答 1

1

听起来您只是希望一个人单击一个按钮并更改该按钮的文本?

如果这是正确的,这样的事情将在您的点击方法中起作用:

With DirectCast(sender, Button)
  .Text = InputBox("Button Name", "Button Name", .Text)
End With

如果每个按钮都需要相同的输入,请尝试以下操作:

Dim value As String = InputBox("Button Name", "Button Name")
For Each btn As Button In Buttons
  If btn IsNot Nothing Then
    btn.Text = value
  End If
Next

您应该强烈考虑不再使用该 Buttons 数组。如果您需要在列表中保存这些按钮的引用,请改用 a List(of Button)

于 2012-07-27T15:01:22.643 回答