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!

User Controls added to form at runtime

Status
Not open for further replies.

abaldwin

Programmer
Mar 27, 2001
798
US
After Googling myself to death, I have turned to the one place that can usually provide an answer. Why I did not start here I don't know.

Requirements / Desires

VB6

I have a project with
1 picture box named picture1
1 File list box named fileObjects
1 command button
1 usercontrol defined in the project. This control has a pitcture box in it at the moment. I was trying not to use a picture box but to set the picture of the control and thought this might help with the visibility problem after the control is inserted.

Here is what I am trying to accomplish
After choosing a file (File list is a list of gifs in a directory). I want to click the button and have an instance of the user control placed in the forms picture box and assign the user controls picture to be the picture from the list box.

Currently I have tried the controls.add method and the code runs fine but the control is not visible or actually on the form.

I can place the control on the form at design and changes its picture at run time no problem. Getting the control to be visible and moveable after on the form.

Eventually I will be adding an unknown number of these user controls to the form to display what is effectively a layout of objects in a field or in a room. The gifs are pictures of the actuall items.

I need the following example code or help
1. Actually adding the controls to the form at runtime and having them have the ability to get control simply for the purpose of moving them around.

2. I may need some help with the coding of the user control to allow for its ability to be dragged after on the form.

This is my first jump into user controls of this nature. Spent all my programming time in the DB world and business logic/problems solving coding.

So far my coding on my button is this

Dim myControl As VehItem
Set myControl = Controls.Add("project1.VehItem", "VehItem1", Me) 'This will be changed in the future to name it incrementally after I get the first one to work right

Picture1.Picture = LoadPicture(App.Path & "\images\Objects\" & fileObjects.FileName)
Set myControl.Picture = Picture1.Picture



So far my coding in my first user contorl is
Option Explicit


Public Property Set Picture(ByRef newpic As IPictureDisp)
Picture1.Picture = Form1.Picture1.Picture
End Property

Private Sub UserControl_Initialize()
Picture1.Picture = LoadPicture(App.Path & "\images\Vehicles\Truck.gif")

End Sub


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
I did a gig for a place where the main form of their app had hit the VB limit of 250 controls per form. This was only possible because they had a tabbed dialog control so it was sort of 6 pages in one.

What I ended up doing to add more controls to the form was to load up a hidden form and then use the SetParent windows API function to move them from the hidden form to the "full" one. There was a slight focus problem that I fixed by subclassing the controls to catch windows focus messages.
 
Thanks for the input. My problem is that there is only one picture box and trying to add one control at this time. The first instance of a control will not become available.


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
The way to add one control at a time to a "blank" form is simply to have one instance of the control at design time on the form. Make it invisible. The first time the user wants to add the control, just make it visible. Next time, use Load to load another instance of it, and so on. You'll usually want to limit the number of controls the user can add in this way, of course.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top