Creating forms dynamically in VB???
-
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
-
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
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.
-
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.
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
-
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