Hi.....

I want to create controls in runtime and attach them to an already connected database..

the controls must vary e.g.Array of Strings can be used.

Plz Help...

Also,
plz tell me how to place these controls on a panel in a orderly fashion...

the panel has it's autoscroll property set to true....

I've done something where I created amount of controls accorrding to specified table's columns. Where you draw a textbox for a text/varcharfield and a datetimepicker for a datetime column type.

What i did was add controls to a FlowLayoutPanel , it takes care of spacing the controls .

Tell me if I make sense , so I will tell you more further.

lost you on flow layout panel..???
will try before pestering you on this..
but am totally lost on creating runtime controls...
kinda doing the same thing...
the selected table will result in a complete form, using the table name as query....

I was creating a simple form where you create update delete records etc. . For every table chosen , there would be equal amount of controls as there are column for that SQL table.

To make it better, i had to find the column type , and if column type was
numeric use numeric box
datetime use date picker
text use textbox
etc.

If you want code just say

though am using access database...
but i will appreciate the code if u please....

Another thing is , which you should search is whether you can do this via data binding!
I don't know data binding , i think if you search it you might find better option to this.

Here is code (it is messy!! can improve on it though )

void RefreshGrid(string tableName)//shows table contents of specified table in datagridview , and draws controls below datagrid view
        { 
             try
            { 
                cmd.CommandText = "select * from  " + tableName; //sql injection possibility
                DataTable tempTable = new DataTable();
                SqlDataAdapter datAdap = new SqlDataAdapter(cmd.CommandText, conn);
                datAdap.Fill(tempTable);
                dataGridView1.DataSource = tempTable;//datagridview has table contents
                flowLayoutPanel1.Controls.Clear();//clear flowlayout panel (which is below datagridview)

                
//code readability can become problem here
                    foreach (DataColumn col in tempTable.Columns)
                    {
                        //case Data Type draw this control


                        if (col.DataType.ToString().ToUpper().Equals("SYSTEM.DATETIME"))
                        {
                            DateTimePicker dtm = new DateTimePicker();
                            dtm.Format = DateTimePickerFormat.Short;
                            dtm.Width = 100;
                            dtm.Enabled = false;
                            dtm.Name = "dtmPreview" + col.ColumnName;
                            flowLayoutPanel1.Controls.Add(dtm);
                        }
                        else
                        {
                            TextBox txtPreview = new TextBox();
                            txtPreview.ReadOnly = true;
                            txtPreview.Name = "txtPreview" + col.ColumnName;
                            flowLayoutPanel1.Controls.Add(txtPreview);
                        }
//...not whole code but you get the idea

try this
you need a flow layout panel on your form
set the anchor so that it can expand in all directions

Public Sub AddTextBoxes(ByVal flPanel As FlowLayoutPanel, ByVal controlCount As Integer)
        flPanel.Controls.Clear()
        For x As Integer = 0 To controlCount - 1
            Dim txt As New TextBox()
            flPanel.Controls.Add(txt)

        Next
    End Sub

test it by changing the parameter controlCount's value

I Got thi code, but it doesn't seem to be correct...
Help..??

If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton


Private Sub ctlCommand_Click()
  ctlText.Text = "You Clicked the Command button"
End Sub

Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
' test for the click event of the TreeView
  If Info.Name = "Click" Then
    ctlText.Text = "You clicked " _
     & ctlDynamic.object.selecteditem.Text
  End If
End Sub

Private Sub Form_Load()
  Dim i As Integer
  ' Add the license for the treeview to the license collection.
  ' If the license is already in the collection you will get
  ' the run-time error number 732.
  Licenses.Add "MSComctlLib.TreeCtrl"
  ' Dynamically add a TreeView control to the form.
  ' If you want the control to be added to a different
  ' container such as a Frame or PictureBox, you use the third
  ' parameter of the Controls.Add to specify the container.
  Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
    "myctl", Form1)
  ' set the location and size of the control.
  ctlDynamic.Move 1, 1, 2500, 3500
  ' Add some nodes to the control.
  For i = 1 To 10
    ctlDynamic.object.Nodes.Add Key:="Test" & Str(i), Text:="Test" _
      & Str(i)
    ctlDynamic.object.Nodes.Add Relative:="Test" & Str(i), _
      Relationship:=4, Text:="TestChild" & Str(i)
  Next i
  ' Make the control visible.
  ctlDynamic.Visible = True
  ' add a textbox
  Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
  ' Set the location and size of the textbox
  ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
    1, 2500, 100
  ' Change the backcolor.
  ctlText.BackColor = vbYellow
  ' Make it visible
  ctlText.Visible = True
  ' Add a CommandButton.
  Set ctlCommand = Controls.Add("VB.CommandButton", _
    "ctlCommand1", Form1)
  ' Set the location and size of the CommandButton.
  ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
    ctlText.Height + 50, 1500, 500
  ' Set the caption
  ctlCommand.Caption = "Click Me"
  ' Make it visible
  ctlCommand.Visible = True
End Sub

That code that you use ,
do you run it in VB 6 , and do you add controls that come from
third party(not the ones that already come in the environment)

Hi, i asked a question but i think i forgot to post it (i saw it just after i posted this sorry!)
My q was whether you use VB6 and not VB.NET .
My other q was whether you add controls that do not come standard with the environment.

After looking around I realised that the code you posted is on Microsoft website and that you do use VB 6 (the method parameter lists also indicate VB 6)

I'll try to make VB 6 code coz i use it as well...

Hi , i've got the vb6 code to add a textbox control at runtime

Instructions after the code

Dim WithEvents ctlText As VB.TextBox
Private Sub Command1_Click()
' add a textbox
  Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
 'move it
  ctlText.Move (Me.Left + 750), Me.Top + 1200, 1500, 100 'just for interest Me.Left = 2135 and Me.Top equals 3045 ' had to change these a few times
  ctlText.Visible = True
End Sub

1.Create vb 6 app with Form1
2.Paste the code in the form and create button Name = "Command1"
3.Put button in top left corner
4.Set form's WindowState property to maximized
5.Then test

If you see a white textbox below button , the code works
NOTE

To make code better you must
1.Pre check if code already exists on form(try clicking button twice!)
2.You will need to adjust/investigate the coordinates

sorry was unable ti be online in between....

The code is in VB, i understood that...
Actually i am currently woking on vb.net but many classes and objects used are not present in VB.NET,.....

i guess what i require is the new classes and objects, properties, and have currently no idea...

tried using the same from the visual studio intellisense, but i think i might be using the wrong ones....

try this
you need a flow layout panel on your form
set the anchor so that it can expand in all directions

Public Sub AddTextBoxes(ByVal flPanel As FlowLayoutPanel, ByVal controlCount As Integer)
        flPanel.Controls.Clear()
        For x As Integer = 0 To controlCount - 1
            Dim txt As New TextBox()
            flPanel.Controls.Add(txt)

        Next
    End Sub

test it by changing the parameter controlCount's value

Though in the end unable to draw on the form itself, this has proved to be a viable and easier solution...

This question has already been answered. Start a new discussion instead.