mwells08
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.OracleDataRea der = oraAircraftCmd.ExecuteRead er
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(CtrlX lbl, 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(CtrlX 1HrsLoc, 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(CtrlX 2HrsLoc, 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
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
oraAircraftCmd.CommandText
Dim oraACReader As OracleClient.OracleDataRea
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(CtrlX
.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(CtrlX
.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(CtrlX
.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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for your replies. Moving the line lblCtrl = New Label before With Me.lblCtrl
worked.
worked.