Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to dynamically create and position text boxes.

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
US
Can I Dynamically create a textbox from within my program during runtime?

currently i do:
Code:
Friend WithEvents ala35 As System.Windows.Forms.TextBox

But I cannot do this from inside a Private Sub. What are my options?
 
Dim txt As New TextBox()
txt.Size = New System.Drawing.Size(W, H)
txt.Location = New System.Drawing.Point(X, Y)
txt.Name = 'name the control
 
Okay, but I am wondering, can I change txt (the variant itself) or create it dynamically, as in I create the control name based on a result of a database query.
Let's say, I do this:
Code:
Dim testText As New System.Windows.Forms.TextBox()
testText.Location = New System.Drawing.Point(tPosX, posY)
testText.Name = "johnDoe"
johnDoe.Text = "John Doe"

I get nothing with that. VS says that johnDoe is not declared. Makes sense, but I want to know if I can create something like that... Or do I need to declare all 15-20 possible controls, even if I am not going to use them?
 
You will need to add a handler for the events you want to react to:

AddHandler txt.KeyPress, AddressOf MytxtKeyPressEvent

Private Sub MytxtKeyPressEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'your code here
End Sub

when you do a key press for this text box it will fire the MytxtKeyPressEvent
 
No, I'm not making myself clear here. I want to at run time, change the name of txt.
When I go Dim txt As New Textbox I want to be able to dynamically assign txt or anything else as the name of that Textbox.
 
You can name is dynamically, but I dont know how you would refer to it in code. Can I ask why you would want to do this?
 
Well... I have a program, where the user will be entering data to fill a database/xml/comma delimited... but not every customer will use every field. So I wanted to use an xml file with boolean selections to do something like this:
Code:
For Each value in data
 If value Then
  xPos += 24 
  Dim tempName As String = FieldName & "txt"
  Dim tempName As New Textbox()
  tempName.Location = xPos, yPos
 End If
Next

Then I wouldn't have to do basically the same thing for EACH AND EVERY of my fields. I don't want to do a datagrid. Yucky huh?
 
Why don't you define all of the fields that you may need at design time and make then set the visible flag to false if the field is not used.

TxtField.Visible = True ' Field needed
TxtField.Visible = False 'Feild not needed

The field will not recieve focus if it is invisible.

This may not be as elegant as creating only the fields you require at run time but it is far simpler from a programming point of view.

Surely this will work unless you have an infinitely variable number of fields?


 
check out this article here
You can set the name as you did but you can't nessicarily (sp) access the control by that name. When we access controls normally we are using a pointer variable to that control.
The designer puts the control on the form for us. It also declares a variable with a name of the ID property for us. To access that same control when it has been created at run-time we first need to make a variable point to that control. (hopefully I am making sense here).

So, in the above article the control is passed into the sub as a 'sender'. However, you may not be accessing the control in its own button click but you still want to be able to access it right? You need to loop through the control collection on you form and test to see if x control is the one you are looking for. A simple for loop would do this.

dim ctrl as system.windows.forms.control
For Each ctrl In Me.Controls
If myControl.Name = "mycircle" Then
'put code to manipulate control here
exit For
End If
Next


The exit for is to make the code a little more effecient by exiting the loop after the control has been found. Hope this helps you understand how controls and forms interact better. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top