Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

add controls at runtime

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
1 Solution
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
Move this line

             lblCtrl = New Label

to before

             With Me.lblCtrl

Similar for other two controls.
mwells08Author Commented:
Thank you both for your replies. Moving the line  lblCtrl = New Label before  With Me.lblCtrl

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now