Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Creating forms dynamically in VB???

Creating forms dynamically in VB???

Scheduled Pinned Locked Moved Visual Basic
databasequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NearyM
    wrote on last edited by
    #1

    Is it possible? I need to create a standard form and then populate it with text from a database, putting the text into label controls. The rows in the database will vary so I can't pre-fab the form with X number of labels and then just set their caption property. The form will need to be created on the fly each time it is loaded. As an alternative (and a very weak one at that) I can add labels for the max possible rows and then set the ones without captions to not be visible. If this is my only choice, is there a way to resize the form using VB? Mike

    I 1 Reply Last reply
    0
    • N NearyM

      Is it possible? I need to create a standard form and then populate it with text from a database, putting the text into label controls. The rows in the database will vary so I can't pre-fab the form with X number of labels and then just set their caption property. The form will need to be created on the fly each time it is loaded. As an alternative (and a very weak one at that) I can add labels for the max possible rows and then set the ones without captions to not be visible. If this is my only choice, is there a way to resize the form using VB? Mike

      I Offline
      I Offline
      Itanium
      wrote on last edited by
      #2

      Yes! Mixture of code and pseudocode is here. Hope that this will work. text = get a text from Data Base dim myLabel as new Label myLabel.Text = text myLabel.AutoSize = True mylabel.location = some location myForm.control.add(mylabel) myForm.height = myForm.height + myLabel.height :) sorry for my bad English.

      E 1 Reply Last reply
      0
      • I Itanium

        Yes! Mixture of code and pseudocode is here. Hope that this will work. text = get a text from Data Base dim myLabel as new Label myLabel.Text = text myLabel.AutoSize = True mylabel.location = some location myForm.control.add(mylabel) myForm.height = myForm.height + myLabel.height :) sorry for my bad English.

        E Offline
        E Offline
        Edbert P
        wrote on last edited by
        #3

        I suppose you could. I am not very sure about this because although I've been thinking about it for my next project, I haven't tested it out yet. Here's an example you might develop on (change it according to your need): 1. A function to create the dynamic form (it gets the parameters from database) Public Function CreateForm(ByVal formName As String, ByVal location As Point, ByVal size As Size) As Form Dim frmDyne As Form frmDyne.Name = formName frmDyne.Location = location frmDyne.Size = size CreateForm = frmDyne End Function 2. A function to create the dynamic controls Public Sub CreateControls(ByRef frmObject As Form) Dim intX As Integer Dim intTotalControls As Integer Dim arrControl(5, 5) As String 'Read database and put into array (for example) For intX = 1 To intTotalControls If arrControl(0, 0) = "Textbox" Then Dim txtBox As TextBox txtBox.Name = arrControl(0, 1) 'Name of control txtBox.Text = arrControl(0, 2) 'The text to display txtBox.Top = arrControl(0, 3) 'Top txtBox.Left = arrControl(0, 4) 'etc... txtBox.Width = arrControl(0, 5) txtBox.Height = arrControl(0, 6) 'and so on... frmObject.Controls.Add(txtBox) ElseIf arrControl(0, 0) = "ComboBox" Then 'Create a combobox here End If Next End Sub You can pass the form as a reference or call the function to create the controls when you create the form too, so they're all in one function or sub. The point is you can create a dynamic form using this. PS: Please tell me whether your project works or not, thanks! Edbert P

        N 1 Reply Last reply
        0
        • E Edbert P

          I suppose you could. I am not very sure about this because although I've been thinking about it for my next project, I haven't tested it out yet. Here's an example you might develop on (change it according to your need): 1. A function to create the dynamic form (it gets the parameters from database) Public Function CreateForm(ByVal formName As String, ByVal location As Point, ByVal size As Size) As Form Dim frmDyne As Form frmDyne.Name = formName frmDyne.Location = location frmDyne.Size = size CreateForm = frmDyne End Function 2. A function to create the dynamic controls Public Sub CreateControls(ByRef frmObject As Form) Dim intX As Integer Dim intTotalControls As Integer Dim arrControl(5, 5) As String 'Read database and put into array (for example) For intX = 1 To intTotalControls If arrControl(0, 0) = "Textbox" Then Dim txtBox As TextBox txtBox.Name = arrControl(0, 1) 'Name of control txtBox.Text = arrControl(0, 2) 'The text to display txtBox.Top = arrControl(0, 3) 'Top txtBox.Left = arrControl(0, 4) 'etc... txtBox.Width = arrControl(0, 5) txtBox.Height = arrControl(0, 6) 'and so on... frmObject.Controls.Add(txtBox) ElseIf arrControl(0, 0) = "ComboBox" Then 'Create a combobox here End If Next End Sub You can pass the form as a reference or call the function to create the controls when you create the form too, so they're all in one function or sub. The point is you can create a dynamic form using this. PS: Please tell me whether your project works or not, thanks! Edbert P

          N Offline
          N Offline
          NearyM
          wrote on last edited by
          #4

          Itanium and Edbert: Thanks for the speedy replies. I'll try this method today and let you know what happens. Mike

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups