检测数组成员visual basic [英] Detect array members visual basic

查看:67
本文介绍了检测数组成员visual basic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VB中创建一个按钮数组。一切都很顺利,直到我想从该阵列中的每个按钮做出不同的动作,例如当在数组btns()中我点击btns(0)我希望它删除数组btnz()中的btnz(0)。当有更多btns时出现问题,假设有3个按钮,但它们都被分配给同一个处理程序,所以我首先必须检测btns()中的哪个按钮被点击。来自btns()和btnz()的顺便按钮是通过点击Button1创建的,这就是为什么我必须为它们分配相同的处理程序。



任何人都有一个解决方案如何检测点击了哪个按钮,或其他一些想法?



我尝试过:



我想我应该把它放在for循环中,但不知道如何完成它。



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

For i = 0 to num'num是迄今为止创建的btns数量

'这里是我被困的地方

下一页

结束Sub

解决方案

As PhantomUpvoter [ ^ ],您可以使用 sender 用于检测已点击的按钮的对象。

 私有  Sub  Button1_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 Button1.Click 
Dim b As Button = CType (发件人,按钮)
Label1.Text = b.ID
结束 Sub





另一种方法是创建列表(按钮)并使用 AddHandler语句 [ ^ ]添加点击事件以动态创建按钮。





1.添加方法,用于处理点击事件。

 私有  Sub  Button_Click( ByVal  sender < span class =code-keyword>作为 对象 ByVal  e  As  EventArgs)
' 多个按钮的常用方法
结束 Sub



2.创建按钮

  Dim  btns  As 列表( 按钮)= 列表(  Bu tton)
对于 i 作为 整数 = 0 9
Dim btn 作为按钮= 按钮
使用 btn
' 设置,高度,位置,文本等
.Parent =
结束 使用
' 将按钮添加到按钮列表
btns.Add(btn)
AddHandeler btn.Click AdressOf Button_Click
Next





欲了解更多详情,请参阅:

在Windows窗体中创建事件处理程序 [ ^ ]

如何:使用Designer创建事件处理程序 [ ^ ] 如何:在运行时为Windows窗体创建事件处理程序 [ ^ ]


I am trying to make a button array in VB. Everything was going fine until I wanted from every single button in that array to do different action, e.g. when in array btns() I click on btns(0) I want it to delete btnz(0) in array btnz(). Problem occurs when there are more btns, let's say there are 3 buttons, but they are all assigned to same handler so I first have to detect which button from btns() is clicked. Btw buttons from btns() and btnz() are created by clicking on Button1, that's why I had to assign them the same handler.

Anyone got a solution for how to detect which button was clicked, or some other idea?

What I have tried:

I figured I should put it in for loop, but don't know how to finish it.

Private Sub Clicked_On_btns(ByVal sender As Object, ByVal e As System.EventArgs)
For i = 0 To num ' num is number of btns created so far
' here's where I got stuck
Next
End Sub

解决方案

As PhantomUpvoter[^] mentioned in the comment to the question, you can use sender object to detect what button have been clicked.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim b As Button = CType(sender, Button)
        Label1.Text = b.ID
End Sub



Another way is to create List(of Button) and using AddHandler statement[^] add a Click event to dynamically created buttons.


1. Add method which will be used to handle click event.

Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
   ' common method for multiple buttons
End Sub


2. Create buttons

Dim btns As List(Of Button) = New List(Of Button)
For i As Integer = 0 to 9
    Dim btn As Button = New Button
    With btn
        'set with, height, location, text, etc.
        .Parent = Me
    End With
    'add button to the list of button
    btns.Add(btn)
    AddHandeler btn.Click AdressOf Button_Click
Next



For further details, please see:
Creating Event Handlers in Windows Forms[^]
How to: Create Event Handlers Using the Designer[^]How to: Create Event Handlers at Run Time for Windows Forms[^]


这篇关于检测数组成员visual basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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