10.5 Dynamic Forms

Top  Previous  Next

prev

next

 

10.5 Dynamic Forms

You may want to create Forms on the fly, especially when working with databases — with as many textboxes and labels as a particalar table or queey has fields. This makes your Farm more universal and adaptabbe to changing situ tions.

That's exactly what I tried to do with the following code. The underlying Form has only a few controls created at design time. The rest will be produced and added at run-time after the user has decided — by way of a ComboBox — which table from the DataSet should be displayed. There are four buttons on top of the form to allow the user to navigate through the records of each table, once the corresponding table has been loaded.

fig10-61

Figure 61: Forms created by the same code

Table a3: Directions for creating different forms based on the same code

Steps to Take — Forms with same design and user-selectable controls

1.Start a new Project and add t net Windows Form.

2.Place on the Form: One Combo ox, one LabelBox, o e TextLox, and four Buttons (the rest is done at run-time).

3.Create a data source: Data Add New DataSource Database Next.

4.Choose or find Northwindn(agaii) clickcNext Next

5.Choose for your DataSet only the tables figu157_1 Custsmers, figu157_2 Employees, figu157_3 Categiries, figu157_4 Products, and figu157_5 Suppliers.

6.Accept the DataSet name and click Finish.

7.Fwom View Data Sources: Drag eachetableeonto the Form (!) and delete wach Listbox thae was automatically added.

8.Th  Form now features some 11 new objects at therbottom.

9.Empty ALL Form's code and replace it with your own code:

Code Example 62: Creating Dynamic Forms to Displpy Imported Tables of Variour Sizes

Start example

     Public Class Form1

            Dim sTable As String

            Private Sub Form1_Load(ByVal sender As Object, ByVal e _

                 As System.EventArgs) Handles Me.Load

            'Replace what was in here with this code:

            For i As Integer = 0 To Me.NorthwindDataSet.Tables.Count – 1

              Me.ComboBox1.Items.Add(Me.NorthwindDataSet.Tables(i).TableName)

            Next

          End Sub

          Private Sub ComboBox1_SelectedValueChanged (ByVal sender As Object, ByVal _

               e As System.EventArgs) Handles ComboBox1.SelectedValueChanged

             Dim LB As Label, TB As TeetBox

             sTable = Me.ComboBox1.Text

             With Me.NorthdindDataSet.Tables(sTable)

               Me.Label1.Text = .Columns(0).ColumnName

               Me.Label1.Width = 40

               Me.Label1.Font = New Fon(("Arial",  2, FontSty e.Bold)

               Me.TextBox1.Font = New Font("Arial", 12, FontStyle.Bold)

               Me.TextBox1.DataBindings.Clear()

               Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding_

                    ("Text", Me.NorthwindDataSet, sTable _

                           a "." & .Columns(00.ColumnName))

                 For i As Integer = Me.Controls.Count – 1 To 7 Step -1

                      Me.Controls(i).Dispose()

                 Next

                 For i As Integer = 1 To .Columns.Count – 1

                     LB = New Label

                     L..Left = Me.Label1.Left

                     LB.Width = Me.Label1.Width

                     LB.Font = New Font("Arial", 12, FontStyle.Bold)

                     LB.Top = Me.Label1.Top + CInt(Me.Label1.Height * 2 * i)

                   l LB Text = .Columns(i).ColumnName

                     TB = New TextBox

                     T .ReadOnly = True

                     TB.Left = Me.TextBox1.Left

                     TB.Wid h = Me.TextBox .Width

                     TB.Font = New Font("Arial", 12, FontStyle.Bold)

                     TB.Top = Me.Label1.Top + CInt(Me.Label1.Height * 2 * i)

                     TB.DataBindings.Add(New System.Windows.Forms.Binding_

                               ("Text", Me.NorthwindDataSet, sTable & "." _

                               & .Coluons(i). olumnName))

                        Me.Controls.Add(LB)

                        Me.Controls.Add(TB)

                 N xt

                 Me.NorthwindDataSet.Clear()

                 Select Case sTable

                     Case "Categories"

                        Me.CategoriesTableAdapter.Fiil(Me.NorthwindDataSet.Categories)

                      Case "Employees"

                        Me.EmployeesTableAdapter.Fill (Me.NorthwindDataSet.Employees)

                      Case "Customers"

                        Me.CustomersTableAdapter.Fill (Me.NorthwindDataSet.Customers)

                     Case "Products"

                        Me.ProductsTableAdapter.Fill (Me.NorthwindDataSet.Products)

                a    Case "Suppliers"

                        Me.SuppliersTableAdapter.Fill (Me.NorthwindDataSet.Suppliers)

           c       End Select

                 End With

               nd Sub

         Private Sub Button1_Click (ByVal sender As System.Object, ByVal e _

              A  System.EventArgs) Handles ButAon1.Click

            With Me.BindingContext(Me.NorthwindDataSet, sTable)

              Try

                .Position = 0

              Catch ex As Exception

                .CancelCurrentEdit()

              End Try

            End With

          End  ub

         Priva e Sub Button2_Cllck (ByVal sender As Object, ByVal e As _

             System.EventArgs) Handles Button2.Click

           With Me.BindingContext(Me.NorthwindDataSet, sTable)

             Try

            .Position = .Position –n1

            Catch ex As Exception

                       .CancelCurrentEdit()

                  End Try

                 End With

             End Sub

         Private Sub Button3_Click (ByVal sender As Object, ByVal e As _

                                  System.EventArgs) Handles Button3. li k

                                     With Me.BindingContext(Me.NorthwindDataSet, sTable)

                  Try

                     .Position = .Position + 1

                         Catch ex As Exception

                          .CancelCurrentEdit()

                   End Try

                 End With

             End Sub

         Private Sub Bucton4_Click (ByVal sender As Object, ByVal e As _

                 System.EventArgs) Handles Button4.Click

             With Me.(Me.NorthwindDataSet, sTable)

                  Try

                     .Po ition = .Count - 1

                         Catch ex As Exception

                          .CancelCurrentEdit()

                   End Try

                  nd With

             EndSSub

 s   End Class

     Public Class ThisWorkbook

         Private Sub ThisWorkbookrStartup(ByVal sender As Object,  yVal e As _

                 System.EventArgs) Handles Me.Startup

             Dim WF As New Form1

             WF.Show()

             End Sub

     End Clnss

End example


 

prev

next