如何在vb.net中创建按钮控件数组?! [英] How to create an array of button control in vb.net?!

查看:86
本文介绍了如何在vb.net中创建按钮控件数组?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我是来自越南的新手(所以我不擅长英语)!



我在VB.NET中遇到问题。

据我们所知,VB支持的控件数组,但不支持VB.NET。



我在表单上设计了很多按钮(100按钮)。

然后我为它们设置的名称是Button1,Button2,... Button100。



我想创建一个数组来包含这个按钮(我在表单上设计的Button1,.. Button100) - (不创建一个包含的阵列)新按钮)。



那么,我该怎么办呢?请告诉我一个方法!



请帮帮我! :confused:

Hello everyone!

I'm a newbie who is from Vietnam (so I'm not good at English)!

I have a problem in VB.NET.
As far as we have known, VB's supported control array, but VB.NET isn't supported.

I've designed a lot of buttons (100 button) on a form.
Then I've set names for them are Button1, Button2,...Button100.

I'd like to create an array to contain this Buttons (Button1,..Button100 that I have designed on the form)-(NOT CREATE AN ARRAY TO CONTAIN NEW BUTTONS).

So, How can I do it? please show me a way!

help me please! :confused:

推荐答案

CMember 7655557写道:
CMember 7655557 wrote:

每个按钮的文字是从0到99的数字。我想使用For ... Next循环在按钮数组的每个按钮的文本上添加0到99之间的数字。

The Text of each button is a number which from 0 to 99. I'd like to use the For...Next loop to add number which from 0 to 99 on the text of each button of button array.





澄清后,我建议使用以下代码。你不需要数组来做到这一点。





After your clarification, I would suggest to use following code. You don't need array to do that.

'Dim buttons() As Button


For i = 1 To 100
  Dim btns() As Button = Controls.Find("Button" & i, True)
  Dim btn As Button
  If btns IsNot Nothing Then
    btn = btns(0)
    'If buttons Is Nothing Then
    '  ReDim buttons(0)
    'Else
    '  ReDim Preserve buttons(buttons.Length)
    'End If
    'buttons(UBound(buttons)) = btn
    btn.Text = i - 1 'here you can change whatever you want
  End If
Next

尝试Followinn链接



动态创建数组



这个懒散的编程亲爱的尝试goog le search。
Tries Followinn links

Dynamic creatio nof array

This lasy programming dear tries google search.


步骤1,创建一个名为Form1的表单。

步骤2,添加一个名为TableLayoutPanel1的TableLayoutPanel。

步骤3,将其设为10 X 10,每个10%。

步骤4,将其锚点设置为T,B,L和R.(设计时可以是任意大小)

步骤5,将代码复制到Form1.vb。

第6步,运行它,然后你就可以了。

第7步,如果适合你,请将此标记为5.00。我希望在3天内获得10 * 5.00.



Step 1, Create a Form called Form1.
Step 2, Add a TableLayoutPanel called TableLayoutPanel1.
Step 3, Make it 10 X 10 with 10% each.
Step 4, Set its anchor to T, B, L, and R. (It could be in any size in design time)
Step 5, Copy the code to Form1.vb.
Step 6, Run it, and you get it.
Step 7, If it works for you, please mark this one as 5.00. I wish to get 10 * 5.00 in 3 days.

Public Class Form1
    'Use 0 based array
    Private NRow As Integer = 9
    Private NCol As Integer = 9
    Private BtnArray((NRow + 1) * (NCol + 1) - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod (NCol + 1), i \ (NCol + 1))
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
   System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class


这篇关于如何在vb.net中创建按钮控件数组?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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