Dynamically Add TextBox On Button Click

C

crjunk

Is is possible to dynamically create and add textboxes to a form when
a button is clicked?

I've written and tried to execute the following code:

Dim ctrl As Control
Dim ctlNameCB As ComboBox
Dim FieldName As String

Private Sub brnOverlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles brnOverlay.Click

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then
ctlNameCB = ctrl
FieldName = ctrl.Name

Dim MyTextBox = New TextBox

MyTextBox.text = ctlNameCB.SelectedText

MyTextBox.id = "txt" & ctlNameCB.Name
MyTextBox.location.x = ctlNameCB.Location.X
MyTextBox.location.y = ctlNameCB.Location.Y
MyTextBox.Size.Width = (ctlNameCB.Size.Width - 10)
MyTextBox.Size.Height = ctlNameCB.Size.Height

End If
Next

End Sub
End Class


But I'm receiving the following error when executing it: "Late-bound
assignment to a field of value type 'Point' is not valid when 'Point'
is the result of a late-bound expression"

The error is occurring when the "MyTextBox.location.x =
ctlNameCB.Locaiton.X " is executed.

I'm coming at this from an ASP.NET background so there might be
something obvious that I'm overlooking.

Thanks,
crjunk
 
L

Lloyd Sheen

Is is possible to dynamically create and add textboxes to a form when
a button is clicked?

I've written and tried to execute the following code:

Dim ctrl As Control
Dim ctlNameCB As ComboBox
Dim FieldName As String

Private Sub brnOverlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles brnOverlay.Click

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then
ctlNameCB = ctrl
FieldName = ctrl.Name

Dim MyTextBox = New TextBox

MyTextBox.text = ctlNameCB.SelectedText

MyTextBox.id = "txt" & ctlNameCB.Name
MyTextBox.location.x = ctlNameCB.Location.X
MyTextBox.location.y = ctlNameCB.Location.Y
MyTextBox.Size.Width = (ctlNameCB.Size.Width - 10)
MyTextBox.Size.Height = ctlNameCB.Size.Height

End If
Next

End Sub
End Class


But I'm receiving the following error when executing it: "Late-bound
assignment to a field of value type 'Point' is not valid when 'Point'
is the result of a late-bound expression"

The error is occurring when the "MyTextBox.location.x =
ctlNameCB.Locaiton.X " is executed.

I'm coming at this from an ASP.NET background so there might be
something obvious that I'm overlooking.

Thanks,
crjunk

First thing to do is set Option Strict On and Option Explicit On

This will make VS show you errors in your code prior to execution (a real
time saver and will help to get rid of those errors which occur after "all"
your testing.

This will highlight the problem and if you are using VS 2005 with refractor
it will most likely give you the code to fix it.

LS
 
C

crjunk

First thing to do is set Option Strict On and Option Explicit On

Thanks for the info. I added this and have "refined" my code. Assuming
I an on the right track, the MyTextBox that I am creating in the For
loop is not being displayed on the form. Any advice on what I'm doing
wrong?

Thanks,
crjunk

Private Sub brnOverlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles brnOverlay.Click

Dim ctrl As System.Windows.Forms.Control

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then

Dim MyTextBox As New TextBox

MyTextBox.Text = ctrl.Text & " This is a Test."
MyTextBox.Name = "txt" & ctrl.Name
MyTextBox.Location = New
System.Drawing.Point(ctrl.Location.X, ctrl.Location.Y)
MyTextBox.Size = New
System.Drawing.Size((ctrl.Size.Width - 10), ctrl.Size.Height)
MyTextBox.Visible = True

MyTextBox.CreateControl()
MsgBox(MyTextBox.Created)

End If
Next

End Sub
End Class
 
L

Lloyd Sheen

Thanks for the info. I added this and have "refined" my code. Assuming
I an on the right track, the MyTextBox that I am creating in the For
loop is not being displayed on the form. Any advice on what I'm doing
wrong?

Thanks,
crjunk

Private Sub brnOverlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles brnOverlay.Click

Dim ctrl As System.Windows.Forms.Control

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then

Dim MyTextBox As New TextBox

MyTextBox.Text = ctrl.Text & " This is a Test."
MyTextBox.Name = "txt" & ctrl.Name
MyTextBox.Location = New
System.Drawing.Point(ctrl.Location.X, ctrl.Location.Y)
MyTextBox.Size = New
System.Drawing.Size((ctrl.Size.Width - 10), ctrl.Size.Height)
MyTextBox.Visible = True

MyTextBox.CreateControl()
MsgBox(MyTextBox.Created)

End If
Next

End Sub
End Class

You have to add the control to something like the form or a panel. I am
assuming you are trying to overlay the comboboxes with textboxes. In that
case since you are gettting the comboboxes from the form's control
collection you should be adding them to the form's control collection.

LS
 
C

crjunk

Thanks LS! The purpose of this code is to create a text box on top of
a combo box in order for the user to be albe to read the text clearly
if the combo box is disabled. I've had no luck changing the color of
the disabled combo box, so I thought I'd give this a shot.

I'm sure there is probably a better way to do this. Here is what I
ended up with:

Private Sub brnOverlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles brnOverlay.Click

Dim ctrl As System.Windows.Forms.Control

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then

Dim MyTextBox As New TextBox
Dim TextControlName As String = "txt" & Mid(ctrl.Name,
4, Len(ctrl.Name))

MyTextBox.Text = ctrl.Text
MyTextBox.Name = TextControlName
MyTextBox.Location = New
System.Drawing.Point(ctrl.Location.X, ctrl.Location.Y)
MyTextBox.Size = New
System.Drawing.Size((ctrl.Size.Width - 18), ctrl.Size.Height)
MyTextBox.Visible = True
MyTextBox.Enabled = False
MyTextBox.BackColor = Color.White

MyTextBox.CreateControl()
MyTextBox.BringToFront()
Me.Controls.Add(MyTextBox)
Me.Controls.Item(TextControlName).BringToFront()


End If
Next

End Sub
 
Top