我需要在 Visual Basic 中構建許多表單,但是手動完成需要很多時間。作為一個實驗,我想知道是否可以生成帶有多個引數的表單,但是我找不到撰寫表單的方法。例如,我將如何宣告和初始化按鈕、標簽、文本框等并將它們編碼為流布局或表格布局。我是在 Class 或 Form.vb 或 Module 中構建它嗎?
請讓我知道,是否可能。
uj5u.com熱心網友回復:
下面是一個如何在 WinForms 中動態創建控制元件并將它們添加到容器控制元件的示例。我還向您展示了如何連接事件處理程式。要測驗代碼,請創建一個新表單并向其添加一個名為“Panel1”的面板。然后將此代碼粘貼到 .vb 檔案中。您可以添加對更多控制元件型別的支持。
此擴展方法很有用,可確保 WinForms 在您從容器中清除控制元件時銷毀控制元件(需要進入其自己的檔案)
Module ExtensionMethods
<Extension()>
Sub Clear(ByVal controls As Control.ControlCollection, ByVal dispose As Boolean)
For ix As Integer = controls.Count - 1 To 0
If dispose Then
controls(ix).Dispose()
Else
controls.RemoveAt(ix)
End If
Next
End Sub
End Module
此代碼顯示如何動態創建控制元件
''' <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
' This is only really needed in an ASP.NET solution where
' you are re-rendering the controls in the PreRender
' event because things have changed since the OnInit event.
' Although, if you need to change the form based on what
' was put into it (like a dynamic questionnaire) then this would
' also be applicable in a WinForms scenario.
Panel1.Controls.Clear(True)
' 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496417.html
標籤:VB.net
