您可以调整用户窗体的这些代码吗:使其小而高效

2024-05-28

当 userfrom 按以下顺序激活时,我想在运行时添加动态用户表单控件,例如(标签、文本框)。

我想要类似以下的东西

当用户表单激活时,它需要询问用户字段的数量,他/她想要插入? 如果用户回答7,则需要按以下顺序添加字段“3 列顺序"

标签1 文本框1 标签2 文本框1 标签3 文本框3

标签文本框 4 标签 5 文本框 5 标签 6 文本框 6

Label7 textbox7 ........等等基于现场用户的数量 问。

请求增强代码:

我尝试了以下代码:

Private Sub UserForm_Initialize()

Dim i As Long

number = 10 'InputBox("Enter no of text-boxes and labels you wish to create at run-time", "Enter TextBox & Label Number")

Dim txtB1 As control

For i = 1 To 5

Set txtB1 = Controls.Add("Forms.TextBox.1")

With txtB1
.Name = "txtBox" & i
.Height = 20
.Width = 50
.Left = 70
.Top = 20 * i * 1
End With

Next i


For i = 6 To 10

Set txtB1 = Controls.Add("Forms.TextBox.1")

With txtB1
.Name = "txtBox" & i
.Height = 20
.Width = 50
.Left = 200
.Top = 20 * i - 100 * 1
End With

Next i


Dim lblL1 As control

For i = 1 To 5

Set lblL1 = Controls.Add("Forms.Label.1")

With lblL1
.Caption = "Label" & i
.Name = "lbl" & i
.Height = 20
.Width = 50
.Left = 20
.Top = 20 * i * 1
End With

Next i


For i = 6 To 10

Set lblL1 = Controls.Add("Forms.Label.1")

With lblL1
.Caption = "Label" & i
.Name = "lbl" & i
.Height = 20
.Width = 50
.Left = 150
.Top = 20 * i - 100 * 1
End With

Next i


Dim q As Long

For q = 1 To 5

Controls("lbl" & q) = Cells(1, q)

Next q


For q = 6 To 10

Controls("lbl" & q) = Cells(1, q)

Next q

End Sub

这是在 3 列中执行无限数量的控件的一种方法。不要使用嵌套的 For 循环,而是尝试使用 Do 循环:

Option Explicit

Private Sub UserForm_Initialize()
   Dim nof As Integer
   nof = InputBox("Enter no of text-boxes and labels you wish to create at run-time", "Enter TextBox & Label Number")

   Dim nob As Integer
   nob = InputBox("Enter no of blanks you wish to create at run-time", "Enter Blanks")

   CreateFields nof, nob
End Sub

Private Sub CreateFields(ByVal NumberOfFields As Integer, _
                         ByVal NumberOfBlankFields As Integer)
   Dim i As Integer
   Dim j As Integer
   Dim Top As Single
   Dim Left As Single

   Top = 20
   Left = 20
   i = 1
   j = 1

   Do While i <= NumberOfFields + NumberOfBlankFields
      If i > NumberOfBlankFields Then
         CreateLabel "lbl" & j, "Label" & j, Top, Left
         CreateTextBox "txtBox" & j, "Text" & j, Top, Left + 30
         j = j + 1
      End If

      i = i + 1

      If i Mod 3 = 1 Then
         Top = Top + 50
         Left = 20
      Else
         Left = Left + 100
      End If
   Loop
End Sub

Private Sub CreateLabel(ByVal Name As String, _
                        ByVal Caption As String, _
                        ByVal Top As Single, _
                        ByVal Left As Single)
   With Controls.Add("Forms.Label.1")
      .Name = Name
      .Caption = Caption
      .Height = 20
      .Width = 50
      .Left = Left
      .Top = Top
   End With
End Sub

Private Sub CreateTextBox(ByVal Name As String, _
                          ByVal Text As String, _
                          ByVal Top As Single, _
                          ByVal Left As Single)
   With Controls.Add("Forms.TextBox.1")
      .Name = Name
      .Text = Text
      .Height = 20
      .Width = 50
      .Left = Left
      .Top = Top
   End With
End Sub

当然,您可以根据需要调整控件的间距。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

您可以调整用户窗体的这些代码吗:使其小而高效 的相关文章

随机推荐