Link to home
Start Free TrialLog in
Avatar of mwells08
mwells08Flag for United States of America

asked on

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
                    Me.Controls.Add(lblCtrl)
                    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
                    Me.Controls.Add(txtbx1Ctrl)
                    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
                    Me.Controls.Add(txtbx2Ctrl)
                    txtbx2Ctrl.Visible = True
                    CtrlYLoc = CtrlYLoc + 30

                End While
end sub
Avatar of YZlat
YZlat
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwells08

ASKER

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