VB动态添加控件的问题——TextBox文本框

2024-12-02 11:11:39
推荐回答(4个)
回答1:

Private Sub Command1_Click()
Dim a As TextBox
Dim i As Integer
For i = 0 To 9
Set a = Form1.Controls.Add("VB.TextBox", "Text" & i)
a.Visible = True
a.Move i * 500, i * 400, 1000, 300
a.BackColor = RGB(255, 255, 255)
a.Text = "Text" & i
Next
Lab.Caption = "动态添加控件成功"
End Sub

回答2:

Private Sub Command1_Click()
Dim a As TextBox
Dim i As Integer
For i = 0 To 9
Set a = Form1.Controls.Add("VB.TextBox", "Text" & i)
a.Visible = True
a.Move i * 500, i * 400, 1000, 300
a.BackColor = RGB(255, 255, 255)
a.Text = "Text" & i
Next
MsgBox "动态添加控件成功"
End Sub

回答3:

1. 首先,你是动态添加textbox文本框的,那么你Dim a As Label肯定类型不匹配,可以直接Dim a
2. textbox文本框没有Caption属性,只有text属性

修改这两处后,可以执行了

回答4:

Dim a As textbox

a.text = "text" & i