Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
OriginalGriff 12-Mar-14 5:21am    
And?
What have you tried?
Where are you stuck?

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:
VB
Private currentRow As Integer

4. Add main form Load event code:
VB
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:
VB
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.
 
Share this answer
 
Comments
jovi.salonga 14-Mar-14 1:47am    
Thanks a lot!!! It helped.
Marcin Kozub 14-Mar-14 4:24am    
You're welcome :)
Creating controls on the fly is fairly easy with .NET and VB. And you don't need to check text length within event. You can do this by setting MaxLength property of TextBox control.
VB
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
 
Share this answer
 
v3
Comments
jovi.salonga 13-Mar-14 0:21am    
Hi Kozub,

Thank you for your reply. Just a few clarifications and additional scenarios.

* FieldName "CustomerNumber" can be dynamic. Meaning I can name it to "CustomerNo" or even remove it from the table.

Here is the psuedocode:
Read the db schema and table definition
Iterate the fields and its attributes
Dynamically generate controls based on the fieldNames and field attributes.

My question now is, can I make "customerNumberTextbox" an object variable?

I hope i make sense.

TIA

Jovi
Marcin Kozub 13-Mar-14 9:06am    
Sorry, I accidentally post an answer to your question as another solution. Take a look at Solution 3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900