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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Control at Run Time...?

Status
Not open for further replies.

ColDeamon

Programmer
Feb 15, 2000
1
US
Does anyone here know how exactly one would create a new control at runtime in VB? I am trying to create a new image/picture at a location that the user clicked and I thought the best way would be to make a new image box or picture box control with the picture each time. Is this the best way to do it? If so than how is this done? I have queried the MSDN Knowledge Base and I can't find a damn thing about this. I appreciate any help I can find. Thanks.
 
You have to create a control array to load controls at runtime. Something like this:<br>
<br>
Create a picture box on the form and give 'Index' property the value of 0 (zero). Then in the code, you have to keep track of which control index you want to load. Like this:<br>
<br>
Load picGraphic(intMaxImage)<br>
<br>
Typically in this application, intMaxImage would be a module level variable. When you create a new control, increment the value by one. When you unload the control, decrement the counter by one. You can only unload the controls you create at run-time and can't unload the one you placed on the form at design-time or you'll get an error. To unload the control use this code:<br>
<br>
Unload picGrapic(intMaxImage)<br>
<br>
There are 2 things you have to do when you create a control at run-time. First you have to move the control to the location you want it to appear on the form. The second thing you have to do is make the control visible by setting its 'Visible' property to TRUE.<br>
<br>
Cheers,<br>
<br>
<p>Steve Meier<br><a href=mailto:sdmeier@jcn1.com>sdmeier@jcn1.com</a><br><a href= > </a><br>
 
Option Explicit<br>
<br>
Dim WithEvents txtMain As TextBox<br>
Dim WithEvents lstMain As ListBox<br>
<br>
<br>
Private Sub Form_Load()<br>
'Create and Set Up Controls<br>
Set txtMain = Me.Controls.Add(&quot;vb.textbox&quot;, &quot;txtMain&quot;, Me)<br>
txtMain.Text = &quot;&quot;<br>
txtMain.Visible = True<br>
<br>
'Setting the OLEDragMode to automatic makes _<br>
detecting a drag operation much easier than _<br>
monitoring the mouse position<br>
txtMain.OLEDragMode = vbOLEDragAutomatic<br>
Set lstMain = Me.Controls.Add(&quot;vb.listbox&quot;, &quot;lstMain&quot;, Me)<br>
lstMain.Clear<br>
lstMain.Visible = True<br>
<br>
'We'll handle all the Drop logic<br>
lstMain.OLEDropMode = vbOLEDropManual<br>
End Sub<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Center : Visual Basic Center</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top