Hi,
1. Open Visual Studio and create new WindowsForms Application Project.
2. Add TableLayoutPanel (name it: tlpLayout) to your main form (FrmDynamic) and set it Dock property to Dock.Fill
3. Declare private variable in your main form:
Private currentRow As Integer
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. 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
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.