vb.net 如何在visual basic 2012中添加文本框(0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22736666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:14:58  来源:igfitidea点击:

How to add textbox(0) in visual basic 2012

arraysvb.nettextbox

提问by t3cho

On Visual Basic 6.0 you add a TextBox on form and it has a name TextBox1after that I copy/paste that TextBox on form and I got TextBox1(0)paste again TextBox1(1)
How to do that on Visual Basic 2012 ??? I copy/paste text box and got Textbox1Textbox2? Do you understand the question?

在 Visual Basic 6.0 上,您在表单上添加了一个 TextBox,然后TextBox1我在表单上复制/粘贴该 TextBox 并TextBox1(0)再次粘贴TextBox1(1)
如何在 Visual Basic 2012 上执行此操作?我复制/粘贴文本框并得到Textbox1Textbox2? 你明白这个问题吗?

I tried to copy paste TextBox1and I got TextBox2

我试图复制粘贴TextBox1,我得到了TextBox2

The code I want to use is to check the TextBoxes something like

我想使用的代码是检查 TextBoxes 之类的东西

Dim i as integer
For i=1 to 5
   textbox(i).text="Anel"
Next

回答by Tun Zarni Kyaw

You have to draw FIVE textboxes and ONE button on the form

您必须在表单上绘制五个文本框和一个按钮

(1) Declare a Collectionat class level,

(1)Collection在类级别声明a ,

(2) In Form_Loadevent, add your textboxes to the Collection

(2) 在Form_Load事件中,将您的文本框添加到Collection

(3) You can access all your textboxes with forloop as shown in following code

(3) 您可以使用for循环访问所有文本框,如下面的代码所示

Dim AL As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AL.Add(TextBox1)
    AL.Add(TextBox2)
    AL.Add(TextBox3)
    AL.Add(TextBox4)
    AL.Add(TextBox5)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer
    For i = 1 To AL.Count
        AL(i).Text = "Hello"
    Next
End Sub

回答by Waterfull Idr

First create a list of the number of textboxes you want to modify and name the textboxes in a good way --> Exemple : -----> I have three textboxes names as following : " ttttaksa1" , "asiuhi2" , "EEFUGUYug3" -----> We have put a number at the end of the textbox so we can call mofidy each one according to its number -----> Now that we have 3 textboxes , we will make a list of strings with the numbers of our textboxes : in this case : 3 numbers ( NB : The list is of STRINGS ! ) The list will be like that : Dim n as New List(of string)=({"1","2","3"}) if we have 11 textboxes for exemple it will be like that :

首先创建一个你想要修改的文本框数量的列表,并以一种好的方式命名文本框 --> 示例:-----> 我有三个文本框名称如下:“ttttaksa1”、“asiuhi2”、“EEFUGUYug3” " -----> 我们在文本框的末尾放了一个数字,所以我们可以根据它的编号调用 mofidy -----> 现在我们有 3 个文本框,我们将制作一个字符串列表我们的文本框的数字:在这种情况下:3 个数字(注意:列表是 STRINGS !)列表将是这样的:Dim n as New List(of string)=({"1","2"," 3"}) 如果我们有 11 个文本框,例如它会是这样的:

Dim n as New List(of string)=({"1","2","3","4","5","6","7","8","9","10","11"})

Dim n as New List(of string)=({"1","2","3","4","5","6","7","8","9","10 ","11"})

Now , if you want to , for exemple , change the text of the two first textboxes (ttttaska1 and asiuhi2 ) , you will need to do that :

现在,例如,如果您想更改前两个文本框(ttttaska1 和 asiuhi2)的文本,您需要这样做:

For Each textbox In Me.Controls

        For Each s As String In n

            If textbox.Name.ToString.EndsWith(n) Then
                'Do what you want to your textbox ex : textbox.text="Anel"
                 textbox.forecolor=Color.Blue
            End If

        Next
    Next

I hope that was useful and thank you :D I am now making games and selling them , wanna join me ? facebook : Waterfull Idr Good luck :D

我希望这是有用的,谢谢:D 我现在正在制作和销售游戏,想加入我吗?facebook : Waterfull Idr 祝你好运 :D

回答by user6379523

Dim textBoxTemp As TextBox
Dim stringData As String

For index = 0 To 23
    stringData = ("txtBoxReal" & index)
    textBoxTemp = CType(Me.Controls(stringData), TextBox)
    textBoxTemp.Text = 0
Next