简体   繁体   English

VB.NET中的动态控制

[英]Dynamic control in VB.NET

This is my code for dynamic textbox controls in button click event. 这是我的按钮单击事件中动态文本框控件的代码。 The code is working well. 该代码运行良好。 If i click the button 3 times, it is generated 3 text boxes. 如果我单击按钮3次,它将生成3个文本框。 But I have no idea to assign text box values to a variable. 但是我不知道将文本框值分配给变量。 I dont know the names of dynamic generated controls. 我不知道动态生成的控件的名称。 if i want to add value to 3rd text box, how to do it? 如果我想在第3个文本框中添加值,该怎么做?

Dim txtBx As TextBox
Static x As Integer
Static i As Integer

txtBx = New TextBox

txtBx.Location = New Point(10, 10 + x)
txtBx.Size = New Size(100, 20)

i = i + 1
x = x + 20

Me.Controls.Add(txtBx)

if i create normal textbox i can do it with, 如果我可以创建普通的文本框,

TextBox3.Text = "Some value"

But I dont know to do this for dynamic controls. 但是我不知道为动态控件执行此操作。

Here's an example, storing the references in a List(Of Textbox): 这是一个将引用存储在List(Of Textbox)中的示例:

Public Class Form1

 Private tbList As New List(Of TextBox) Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim tb As TextBox Dim n As Integer n = tbList.Count + 1 tb = New TextBox With tb .Location = New Point(10, 10 + (n * 20)) .Name = "dynTB" & n.ToString .Size = New Size(100, 20) End With Me.tbList.Add(tb) Me.Controls.Add(tb) End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click ' Testing: If Me.tbList.Count >= 3 Then Me.tbList(2).Text = "This is textbox 3" End Sub 

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM