468,557 Members | 2,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,557 developers. It's quick & easy.

Create textbox at runtime

How to create textbox randomly by written code in a command button?
Nov 16 '07 #1
4 25942
AHMEDYO
112 100+
HI...

what you mean by randomaly , you mean dynamically???
Nov 16 '07 #2
AHMEDYO
112 100+
Hey..

You have 3 ways to do that I will descript you begin from simple method and terminate by complex one.

Method 1:

You can create your run-time controls first by placing Textbox control in your form and then change index property to 0 to create array of textbox control, then you can use it to load new controls at runtime, and you can receive events from all of controls by Text1 control events and use Index parameter to identify your control.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim TextControl As TextBox
  3.     ControlID = ControlID + 1
  4.     Load Text1(ControlID)
  5.     Set TextControl = Text1(ControlID)
  6.     With TextControl
  7.         .Left = (Text1(ControlID - 1).Left + Text1(ControlID - 1).Width) + 10
  8.         .Top = 20
  9.         .Width = 100
  10.         .Height = 20
  11.         .Visible = True
  12.     End With
  13. End Sub
'================================================= =======
Method 2:

You can create new controls using Form.Controls object, but you can't receive events from VB IDE about these controls, to handle events you must work a lot using API functions.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim TextControl As TextBox
  3.     Dim PrevTextControl As TextBox
  4.     ControlID = ControlID + 1
  5.     Set TextControl = Form1.Controls.Add("Vb.TextBox", "Text" & ControlID)
  6.     If (ControlID > 1) Then
  7.         Set PrevTextControl = Form1.Controls("Text" & (ControlID - 1))
  8.     Else
  9.         Set PrevTextControl = TextControl
  10.     End If
  11.     With TextControl
  12.         .Left = (PrevTextControl.Left + PrevTextControl.Width) + 10
  13.         .Top = 20
  14.         .Width = 50
  15.         .Height = 20
  16.         .Visible = True
  17.     End With
  18. End Sub
'================================================= =======
Method 3:

Using API only to create new textbox controls at run-time and sure you will work a lot to receive events and just to change the text in the textbox controls, you can use CreateWindows API Function to do that.

GOOD LUCK
Nov 17 '07 #3
@AHMEDYO
Hello sir i have used ur method 1 which is excellent. it lets me add a text box on runtime but i do not know a particular way to add the textboxes besides each other. I mean that if i have to add three text boxes using a command button ADD at runtime they get added behind each other. How can i resolve this using method 1.
Apr 13 '10 #4
@AHMEDYO
I got it!!.Theres absoluetly no problem with the code thank u.
Apr 13 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Tom Rathbun | last post: by
2 posts views Thread by Patrick Marti | last post: by
3 posts views Thread by Don Lee | last post: by
1 post views Thread by pepsi330ml | last post: by
3 posts views Thread by Ricardo Corsi P. Cesar | last post: by
3 posts views Thread by RSB | last post: by
1 post views Thread by Bruno Pimentel Machado | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.