add controls at runtime

Posted on 2012-09-04
Last Modified: 2012-09-04
I am trying to add 3 controls in a form per record; at run time I read the table information and add the controls but only the last 3 controls are showing up in my form at the end. I am not sure what is the problem. Could somebody please help me please?
Here is my code:

 Dim lblCtrl As Label
 Dim txtbx1Ctrl As TextBox
 Dim txtbx2Ctrl As TextBox

Private Sub frmUtil_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim oraAircraftCmd As New OracleClient.OracleCommand
            oraAircraftCmd.Connection = oraConn
            oraAircraftCmd.CommandType = CommandType.Text
            oraAircraftCmd.CommandText = "SELECT * FROM MASTER ORDER BY AC"
            Dim oraACReader As OracleClient.OracleDataReader = oraAircraftCmd.ExecuteReader
            If oraACReader.HasRows = True Then
                While oraACReader.Read

                    lblName = "lblAC" & oraACReader("AC").ToString
                    txtbx1Name = "txtbxHrs" & oraACReader("AC").ToString
                    txtbx2Name = "txtbxCyc" & oraACReader("AC").ToString

                    With Me.lblCtrl
                        .Name = lblName
                        .Location = New System.Drawing.Point(CtrlXlbl, CtrlYLoc)
                        .Text = oraACReader("AC").ToString
                    End With
                    lblCtrl = New Label
                    lblCtrl.Visible = True
                    'AddHandler lvlCtrl.Click, AddressOf lvlCtrl

                    With Me.txtbx1Ctrl
                        .Name = "txtbxHrs" & oraACReader("AC").ToString
                        .Location = New System.Drawing.Point(CtrlX1HrsLoc, CtrlYLoc)
                        .Size = New System.Drawing.Size(70, 22)
                    End With
                    txtbx1Ctrl = New TextBox
                    txtbx1Ctrl.Visible = True
                    With Me.txtbx2Ctrl
                        .Name = "txtbxCyc" & oraACReader("AC").ToString
                        .Location = New System.Drawing.Point(CtrlX2HrsLoc, CtrlYLoc)
                        .Size = New System.Drawing.Size(70, 22)
                    End With
                    txtbx2Ctrl = New TextBox
                    txtbx2Ctrl.Visible = True
                    CtrlYLoc = CtrlYLoc + 30

                End While
end sub
Question by:mwells08
    LVL 35

    Expert Comment

    i don't see where you assign initial values to CtrlXlbl and CtrlYLoc. So it is possible that controls are added one on top of another
    LVL 83

    Accepted Solution

    Move this line

                 lblCtrl = New Label

    to before

                 With Me.lblCtrl

    Similar for other two controls.

    Author Closing Comment

    Thank you both for your replies. Moving the line  lblCtrl = New Label before  With Me.lblCtrl

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now