473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamically Add TextBox On Button Click

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

Nov 12 '07 #1
3 4246

<cr****@earthlink.netwrote in message
news:11**********************@v65g2000hsc.googlegr oups.com...
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

Nov 12 '07 #2
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
Nov 12 '07 #3

<cr****@earthlink.netwrote in message
news:11*********************@v3g2000hsg.googlegrou ps.com...
>
>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

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

Nov 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gelios | last post by:
Hello all! I have a Windows.Forms code where I dynamically created controls. How can I access to properties from event handler? For example: createSomeControls() { TextBox textBox = new...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
6
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
2
by: Radu | last post by:
Hi. I have a page with the following html: __________________________________________________________ <h1>Dynamic Controls Test</h1> <hr /> <asp:TextBox ID="txtProduct1" Text="txtProduct 1"...
0
by: ahmadbaseet | last post by:
Hello all I am new to VB.NET. I am trying to create dynamically objects and giving them the event handles. Something like this Dim NewTab As New TabPage Dim NewTextBox As New TextBox Dim...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.