问题描述
我想开发一个程序来动态创建控件,创建这些控件的基础是数据库表。
以下是详细信息:
我有一个数据库表,其中字段定义为
CustomerNumber varchar(10)
客户名称varchar(60)
IsActive布尔值
我运行程序,将创建2个文本框控件和1个复选框。 CustomerNumber,固定长度为10,CustomerName,固定长度为60. Textbox将生成一个事件,以测试用户的输入是否不大于其定义的长度。
TIA
Jovi
Hi,
I want to develop a program that will create controls on the fly and the basis for creating these controls is the database table.
Here are the details:
I have a database table where fields are defined as
CustomerNumber varchar(10)
CustomerName varchar(60)
IsActive boolean
When I run the program, 2 textbox controls and 1 checkbox will be created. CustomerNumber with fixed-length of 10 and CustomerName with fixed-length of 60. Textbox will generate an event to test if the input of the user is not greater than its defined length.
TIA
Jovi
推荐答案
Private currentRow As Integer
4.添加主表单加载事件代码:
4. Add main form Load event code:
Private Sub FrmDynamic_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
currentRow = 0
CreateTextControl("Customer number", "customerNumber", 10, "0000054112", False)
CreateTextControl("Customer name", "customerName", 60, "ACME Ltd", False)
CreateTextControl("Customer description", "customerDescription", 1000, "No customer description provided", True)
CreateNumericControl("Employees", "employeeCount", 1, 150, 15)
CreateCheckBoxControl("Special discount", "discount", True)
End Sub
5.添加代码以创建动态控件:
5. Add code to create dynamic controls:
Private Sub CreateTextControl(labelTitle As String, fieldName As String, maxLength As Integer, value As String, big As Boolean)
Dim textBox As New TextBox
textBox.Name = fieldName
textBox.MaxLength = maxLength
textBox.Dock = DockStyle.Top
textBox.Text = value
If (big) Then
textBox.Multiline = True
textBox.Height = 150
End If
tlpLayout.Controls.Add(textBox, 1, currentRow)
CreateLabel(labelTitle, currentRow)
currentRow = currentRow + 1
End Sub
Private Sub CreateNumericControl(labelTitle As String, fieldName As String, minValue As Integer, maxValue As Integer, value As Integer)
Dim numericUpDown As New NumericUpDown
numericUpDown.Name = fieldName
numericUpDown.Minimum = minValue
numericUpDown.Maximum = maxValue
numericUpDown.Dock = DockStyle.Fill
numericUpDown.Value = value
tlpLayout.Controls.Add(numericUpDown, 1, currentRow)
CreateLabel(labelTitle, currentRow)
currentRow = currentRow + 1
End Sub
Private Sub CreateCheckBoxControl(labelTitle As String, fieldName As String, value As Boolean)
Dim checkBox As New CheckBox
checkBox.Name = fieldName
checkBox.Checked = value
tlpLayout.Controls.Add(checkBox, 1, currentRow)
CreateLabel(labelTitle, currentRow)
currentRow = currentRow + 1
End Sub
Private Sub CreateLabel(labelTitle As String, rowIndex As Integer)
Dim label As New Label
label.Text = labelTitle
label.Dock = DockStyle.Top
tlpLayout.Controls.Add(label, 0, rowIndex)
End Sub
如您所见,有三种类型的控制我们将动态构建(你需要自己创建休息):
1. TextBox - 用于文本。适用于小型和大型文本的单线和多线模式
2.数字 - 用于数字值
3.复选框 - 用于布尔值
您只需要编写例程来为每个db字段类型创建控件。然后构建您的表单动态迭代您的数据库表模式,确定字段类型,名称和othed属性并为该字段创建控件。
我希望我帮助你一点点bit。
As you can see, there are three type of controls we will build dynamically (you need to create rest on your own):
1. TextBox - for text. Works in single and multi line mode for small and large text
2. Numeric - for numeric values
3. Checkbox - for boolean values
All you need is to write routines to create control for every db field type. Then to build your form dynamically iterate your db table schema, determine field type, name and othed attributes and create control for that field.
I hope i help you a little bit.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Create control
Dim customerNumberTextBox As New TextBox
' Set control's properties
customerNumberTextBox.Size = New Size(100, 20)
customerNumberTextBox.Location = New Point(20, 20)
customerNumberTextBox.MaxLength = 10
customerNumberTextBox.Text = "Value from DB"
' Add control to container (in this case will be Form, but you can use i.e. Panel)
Me.Controls.Add(customerNumberTextBox)
End Sub
这篇关于如何在VB.net中创建控件和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!