General discussion

Locked

How to create run-time controls in VB ?

By nirman123_doshi ·
I want to create controls at run-time in VB. That is, if i want to add a text box in the form, etc, at run-time. I know the method for the same for control array, if one element of the control-array is placed on form, at design-time. But what to do if the control is not of control array type.
Also, please give me the code if possible.
Also, please let me know if it can be possible in MS-Access project or not.

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by cmiller5400 In reply to How to create run-time co ...

You may create controls at runtime in VB. Which version are you using? .Net? an example for .Net is

Dim txtbox As New TextBox()
txtbox.Location = New System.Drawing.Point (100,100)
Me.Controls.Add(txtbox)

This should be available in a MSAccess project using VBA. Though the code may differ slightley.

Collapse -

by khetan_mudit In reply to How to create run-time co ...

The most easiest way to create a run time control in VB 6 is to described in the following steps:
1) create a single control;
2) set all the necessary properties that you want your run-time controls must inherit from it;
3) Set th 'Index' property to 0;
4) code any events and methods that are necessary for its functioning, make extensive use of the 'Index' parameter that is passed in each of them;
5) If you want you can hide this first control by setting its 'Visible' property to 'False' and wen you require the first control to pop-up simply make it visible;
6) For any subsequent controls give the declaration like- DIM ctlRunTimeControl AS NEW CONTROL. by default the new control is not visible so after setting proper values for its appearance (height, width, color, font) and location (left, top), make it visible.

Collapse -

by Tony Hopkinson In reply to How to create run-time co ...

be careful with DIM as New.
the control isn't actually created at that point, VB's compiler uses a just in time methodology and you can get some strange AV's and even harder to find behaviors.
I always use

DIM MyControl as ...

Set MyControl = ...
Then when I've finished with it
Set MyControl = Nothing

That way the compiler does what you want intead of what it thinks is best, without telling you exactly what it has done.

The last step is not required if you add to the controls class, it depends on whether you want your code to manage the lifetime of the instance, or you are going to let VB's compiler do it for you.
If Controls 'knows' about an instance VB will help you out by creating it so you don't get an AV, however you might not want it to behave this way.

Collapse -

by digi_cool In reply to How to create run-time co ...

Dim n As Integer, i As Integer
Dim left_position As Integer
Private Sub Command1_Click()
Dim c(20) As Control
Static j As Integer
n = Txt_Original.Text 'the number of texboxes you want

j = 1
For i = 0 To n - 1
Set c(i) = Controls.Add("vb.textbox", "text" & j)
c(i).Visible = True
c(i).Text = c(i).Name
left_position = left_position + 1400
c(i).Top = top_position
c(i).Left = left_position
j = j + 1
Next
End Sub

Private Sub form_load()
left_position = Txt_Original.Left
End Sub

Back to Web Development Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums