Buttons to be renamed by the user

左心房为你撑大大i 提交于 2019-12-02 10:51:34

问题


I am having problems coding some of my buttons. This is what I've got so far:

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

What I'm trying to do, is if I click button 7, an input box comes up for the user to input the button's name. No matter what I have tried between the >>>>> <<<<<, I can't seem to get it right.


回答1:


It sounds like you just want a person to click on a button and change the text of that button?

If that is correct, something like this would work in your click method:

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

If every button needs that same input, then try something like this:

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

You should strongly consider moving away from using that Buttons array. If you need to hold a reference of those buttons in a list, use a List(of Button) instead.



来源:https://stackoverflow.com/questions/11690108/buttons-to-be-renamed-by-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!