【问题标题】:Visual Basic Generate a FormVisual Basic 生成表单
【发布时间】:2022-06-23 23:17:21
【问题描述】:

我需要在 Visual Basic 中构建许多表单,但手动构建需要很多时间。作为一个实验,我想知道是否可以生成带有多个参数的表单,但我找不到编写表单的方法。例如,我将如何声明和初始化按钮、标签、文本框等并将它们编码为流布局或表格布局。我是在 Class 或 Form.vb 或 Module 中构建它吗?

如果可能,请告诉我。

【问题讨论】:

  • Forms 的意思是你有一个 Windows 窗体项目?在这种情况下,vba 标记不正确。删除它并添加winforms 标记——您为什么认为,如果您在代码中从头开始创建表单,这可能会花费更少的时间?你仍然必须验证它是否正确。除了某些细节之外,您是否有重复相同设计的相同表单?还是某些仅在运行时已知的部分?其他? -- 您可以构建一个模板表单并从中派生具有类似布局的其他表单。 VS(.Net Framework)中还有一个Inherited Form模板。

标签: vba vb.net


【解决方案1】:

这是一个如何在 WinForms 中动态创建控件并将它们添加到容器控件的示例。我还向您展示了如何连接事件处理程序。要测试代码,请创建一个新表单并向其添加一个名为“Panel1”的面板。然后将此代码粘贴到 .vb 文件中。您可以添加对更多控件类型的支持。

''' <summary>
''' Function for creating a textbox and adding it to a control
''' </summary>
''' <param name="Container">The container control to add the textbox to</param>
''' <param name="Name">The Name (Id) of the TextBox (must be unique)</param>
''' <param name="LabelText">The text of the label to create to go with it</param>
''' <param name="Top">The Top (y) position of the TextBox</param>
''' <param name="Left">The Left (x) position of the TextBox</param>
''' <param name="Value">The value of the TextBox</param>
''' <returns></returns>
Private Function AddTextBox(ByRef Container As Control, Name As String, LabelText As String, Top As Integer, Left As Integer, Value As String) As TextBox
    Dim tb As New TextBox With {.Name = Name, .Top = Top, .Left = Left, .Text = Value}
    Dim lbl As New Label With {.Name = Name & "_lbl", .Top = Top, .Left = Left - 150, .Text = LabelText}
    Container.Controls.Add(lbl)
    Container.Controls.Add(tb)
    Return tb
End Function

''' <summary>
''' Function for creating a button and adding it to a control
''' </summary>
''' <param name="Container">The container control to add the button to</param>
''' <param name="Name">The Name (Id) of the Button (must be unique)</param>
''' <param name="Text">The Text of the Button</param>
''' <param name="Top">The Top (y) position of the Button</param>
''' <param name="Left">The Left (x) position of the Button</param>
''' <returns></returns>
Private Function AddButton(ByRef Container As Control, Name As String, Text As String, Top As Integer, Left As Integer) As Button
    Dim btn As New Button With {.Name = Name, .Text = Text, .Top = Top, .Left = Left, .Visible = True, .AutoSize = True}
    Container.Controls.Add(btn)
    Return btn
End Function

''' <summary>
''' A click handler for our Button
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub button_Click(sender As Object, e As EventArgs)
    Dim tb_Name As TextBox = Panel1.Controls.Find("tb_Name", False)(0)
    Dim tb_EmailAddress As TextBox = Panel1.Controls.Find("tb_EmailAddress", False)(0)
    MessageBox.Show("Name: " & tb_Name.Text & "  Email Address: " & tb_EmailAddress.Text)
End Sub

''' <summary>
''' A sub for building our form
''' </summary>
Private Sub BuildForm()
    ' Clear existing controls
    Panel1.Controls.Clear()
    ' Create a "Name" textbox
    AddTextBox(Panel1, "tb_Name", "Your Name", 10, 200, "")
    ' Create an "EmailAddress" textbox
    AddTextBox(Panel1, "tb_EmailAddress", "Your EmailAddress", 40, 200, "")
    ' Create a button to Save Changes
    Dim SaveButton As Button = AddButton(Panel1, "btn_Save", "Save Changes", 70, 200)
    ' Add a handler to the button click event
    AddHandler SaveButton.Click, AddressOf button_Click
End Sub

''' <summary>
''' Build the form as soon as it's shown
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    BuildForm()
End Sub

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多