十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
我是VB.Net 2010,你看看能行不。
站在用户的角度思考问题,与客户深入沟通,找到宁海网站设计与宁海网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都做网站、企业官网、英文网站、手机端网站、网站推广、国际域名空间、虚拟空间、企业邮箱。业务覆盖宁海地区。
Public Class Form1
Private MyTextBoxArray As New ArrayList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 8 '利用循环语句动态加入控件数组。
Dim FirstTextBox As New TextBox
'定义文本框对象
Me.Controls.Add(FirstTextBox)
'将一个文本框控件加入到Form上
MyTextBoxArray.Add(FirstTextBox)
'将文本框控件加入到例表集合中
MyTextBoxArray.Item(i).top = i * MyTextBoxArray.Item(i).height + 20 '修改新加入控件在Form上的位置
MyTextBoxArray.Item(i).left = 100
FirstTextBox.TabIndex = i
'修改新加入控件的TabIndex值
MyTextBoxArray.Item(i).text = "TextBox" i.ToString '修改默认文本。
Next
End Sub
End Class
你这是控件数组?你这明明就是9个不同的 TextBox 控件啊,只不过名字长得像而已。
vb.net和vb6.0不同,无法直接使用控件数组。不过可以通过其他方式变通一下。
比如现在有10个label,要将这10个label的text属性统一设置为“这是第X个标签”(X为1-10)。
1.建立10个label,名称分别为label1、label2、label3。。label10
2.代码:
For i = 1 To 10
Me.FindControl("label" i).Text = "这是第" i “个标签”
Next i
关键点是Me.FindControl()的方法,在代码中Me.FindControl("label1").text与label1.text是一样的。
Imports System.Text
Imports System.Windows
Imports System.Windows.Forms
Public Class Netin '类 名
'Inherits Forms.Form
Public Shared Netin As New Form
Public components As ComponentModel.IContainer
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
DebuggerStepThrough()
Public Sub InitializeComponent()
Me.components = New ComponentModel.Container()
Dim resources As ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(bjt))
Me.TextBox1 = New TextBox(Me.components)
Me.Button1 = New Button(Me.components)
'For … Next
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.m = New ContextMenuStrip(Me.components)
Me.m1.Text = "菜单项1"
'设置并添加相关控件,(窗体)Netin 初始化。
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
'加载后显示窗体Netin。
End Sub
Public Sub ObjClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click,Button3.Click,TextBox1.Click
MessageBox.Show("你按了鼠标")
End Sub
Public WithEvents TextBox1 As TextBox
Public WithEvents Button1 As Button
Public WithEvents Button2 As Button
Public WithEvents Button3 As Button
Friend WithEvents m As ContextMenuStrip
Friend WithEvents m1 As ToolStripMenuItem
End Class
Class MainWindow
Netin.Show()
Netin.Button2.Text = "Main"
……
Dim Str(20) As String '20数据元素
Dim Text(20) As TextBox
Dim n As Integer = 0
For Each i In Text
i = New TextBox '实例化
i.Location = New Point(50, n * 20)
Me.Controls.Add(i) '添加窗体
Dim Index As Integer = i.TabIndex '没index属性
Str(n) = Index
i.Text = Str(n) 'str数组值赋值给文本框
n += 1
Next