How to create textbox randomly by written code in a command button?
Create textbox at runtime
Collapse
X
-
-
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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
ControlID = ControlID + 1
Load Text1(ControlID )
Set TextControl = Text1(ControlID )
With TextControl
.Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
.Top = 20
.Width = 100
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
Dim PrevTextControl As TextBox
ControlID = ControlID + 1
Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
If (ControlID > 1) Then
Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
Else
Set PrevTextControl = TextControl
End If
With TextControl
.Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
.Top = 20
.Width = 50
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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 LUCKLast edited by Killer42; Nov 27 '07, 06:04 AM.Comment
-
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.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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
ControlID = ControlID + 1
Load Text1(ControlID )
Set TextControl = Text1(ControlID )
With TextControl
.Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
.Top = 20
.Width = 100
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
Dim PrevTextControl As TextBox
ControlID = ControlID + 1
Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
If (ControlID > 1) Then
Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
Else
Set PrevTextControl = TextControl
End If
With TextControl
.Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
.Top = 20
.Width = 50
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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 LUCKComment
-
I got it!!.Theres absoluetly no problem with the code thank u.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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
ControlID = ControlID + 1
Load Text1(ControlID )
Set TextControl = Text1(ControlID )
With TextControl
.Left = (Text1(ControlI D - 1).Left + Text1(ControlID - 1).Width) + 10
.Top = 20
.Width = 100
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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.
[CODE=vb]Private Sub Command1_Click( )
Dim TextControl As TextBox
Dim PrevTextControl As TextBox
ControlID = ControlID + 1
Set TextControl = Form1.Controls. Add("Vb.TextBox ", "Text" & ControlID)
If (ControlID > 1) Then
Set PrevTextControl = Form1.Controls( "Text" & (ControlID - 1))
Else
Set PrevTextControl = TextControl
End If
With TextControl
.Left = (PrevTextContro l.Left + PrevTextControl .Width) + 10
.Top = 20
.Width = 50
.Height = 20
.Visible = True
End With
End Sub[/CODE]
'============== =============== =============== ============
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 LUCKComment
Comment