我在Vb.net上读控制数组。但我无法理解它。 [英] I'm reading control arrays in Vb.net. But I'm unable to understand it.

查看:44
本文介绍了我在Vb.net上读控制数组。但我无法理解它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim WithEvents button1 As Button
    Dim WithEvents button2 As Button
    Dim WithEvents button3 As Button
    Friend WithEvents button As Button



    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        button1 = New Button
        button2 = New Button
        button3 = New Button

        AddHandler button1.Click, AddressOf Button4_Click
        AddHandler button2.Click, AddressOf Button4_Click
        AddHandler button3.Click, AddressOf Button4_Click

        button1.Size = New Size(100, 50)
        button1.Location = New Point(28, 186)
        button1.Text = "Button1"

        button2.Size = New Size(100, 50)
        button2.Location = New Point(165, 186)
        button2.Text = "Button2"

        button3.Size = New Size(100, 50)
        button3.Location = New Point(314, 186)
        button3.Text = "Button3"

        Controls.Add(button1)
        Controls.Add(button2)
        Controls.Add(button3)

    End Sub

    Private Sub button_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If sender Is button1 Then
            TextBox1.Text = "You clocked button1"
        ElseIf sender Is button2 Then
            TextBox1.Text = "You have clicked button2"
        ElseIf sender Is button3 Then
            TextBox1.Text = "you have clicked button3"
        End If

    End Sub

End Class

推荐答案

嗯。

看看你的代码:

Um.
Look at your code:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    button1 = New Button
    button2 = New Button
    button3 = New Button

    AddHandler button1.Click, AddressOf Button4_Click
    AddHandler button2.Click, AddressOf Button4_Click
    AddHandler button3.Click, AddressOf Button4_Click



每次单击按钮4时,都会创建三个新按钮并为它们添加处理程序。但是你添加的处理程序是创建四个按钮的例程的地址,而不是显示消息的按钮...

也许你想这样做:


Every time you click Button 4, you create three new buttons and add a handler to them. But the handler you add is the address of the routine that creates the four buttons, rather than the one that displays a message...
Perhaps you want to do this instead:

AddHandler button1.Click, AddressOf button_click
AddHandler button2.Click, AddressOf button_click
AddHandler button3.Click, AddressOf button_click


这篇关于我在Vb.net上读控制数组。但我无法理解它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆