Click to See Complete Forum and Search --> : Creating new TextBoxes at runtime


TheBlackSmith
September 12th, 2005, 07:05 PM
How can i create new textboxes at runtime when a user clicks on a "New Item" button?

Thxn

Ne tutorial will be nice..!

jmcilhinney
September 12th, 2005, 11:24 PM
Dim myTextBox As New TextBox

'Set properties here, e.g. Location, Size, etc.

Me.Controls.Add(myTextBox)

TheBlackSmith
September 14th, 2005, 12:34 AM
Cool, it works but it only creates one textbox with this code:

Dim a As Integer = 0
Dim b As Integer = 0
Dim BoxName As String

Private Sub CreateBox(ByVal a, ByVal b)
Dim myTextbox As New TextBox
BoxName = "txt_" & a & "_" & b
a = a + 20
b = b + 20
myTextbox.Location = New Point(a, b)
myTextbox.Name = BoxName
myTextbox.Text = a & " " & b
Me.Controls.Add(myTextbox)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CreateBox(a, b)
End Sub

How it should look to create as many as i need/want?

TheBlackSmith
September 14th, 2005, 12:39 AM
LOL

Got it working..!

Dim x As Integer = 0
Dim y As Integer = 0
Dim BoxName As String

Private Sub CreateBox(ByVal x, ByVal y)
Dim myTextbox As New TextBox
BoxName = "txt_" & x & "_" & y
myTextbox.Location = New Point(x, y)
myTextbox.Name = BoxName
myTextbox.Text = myTextbox.Name
Me.Controls.Add(myTextbox)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If y <> 0 Then
y = y + 25
Else
y = y + 10
End If
CreateBox(x, y)
End Sub

If there is any "emprovement" please tell me..! :D

Thnx!

DeepButi
September 14th, 2005, 03:29 AM
TheBlackSmith,
not exactly an improvement, but your use of x,y is confusing ... if you have class scope variables there is no need to pass them as parameters to your functions:

Private Sub CreateBox()

and the corresponding

CreateBox()

call will work as well. Or you can define x,y inside Button1_Click and pass them as it is now.

Also, don't you need events in you TextBoxes?

Look for AddHandler statement ...

Hope it helps

TheBlackSmith
September 14th, 2005, 10:36 AM
Just for you to know... it's a little hard for me this way of coding, i'm a Coldfusion Developer, so, this is "new" for me.. that's why sometimes my codes looks "silly", so i apologize for all the basic/newb questions :D.

Ne way, thank you for the tip, i'll try that today... :)

stelmerfudd
September 14th, 2005, 07:57 PM
Hello,

Newbie here.

Thanks for this thread. I'm trying to do basically the same thing and have tried using what you guys have posted here but I'm getting a couple of errors on:

myTextbox.Location
myTextbox.Name

from your code example here.

The errors:
'Location' is not a member of System.Web.UI.WebControls.Textbox
'Name' is not a member of System.Web.UI.WebControls.Textbox

What am I missing?

Thanks,
Fudd

jmcilhinney
September 14th, 2005, 08:17 PM
Hello,

Newbie here.

Thanks for this thread. I'm trying to do basically the same thing and have tried using what you guys have posted here but I'm getting a couple of errors on:

myTextbox.Location
myTextbox.Name

from your code example here.

The errors:
'Location' is not a member of System.Web.UI.WebControls.Textbox
'Name' is not a member of System.Web.UI.WebControls.Textbox

What am I missing?

Thanks,
FuddThe code used above is for a System.Windows.Forms.TextBox. The WebForms TextBox is a different control and has different properties. You'll have to adapt the code to the properties exposed by that control.