Link to home
Start Free TrialLog in
Avatar of chaos_59
chaos_59

asked on

How can I programmatically attach controls to a container?

I want to be able to attach controls to a container, like a frame or a tab control, at run time.

For instance, if I have a control array of command buttons, I can load or unload more or less buttons. I'd like to be able to attach those buttons to a frame or tab control at run time.

Is it possible?
ASKER CERTIFIED SOLUTION
Avatar of JonFish85
JonFish85

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I dont think, you could change the Container property. It is read-only, if I am right.

If you wish to attach to a frame, you could create the control in frame or tab control in design time, and then create the control arrays.

Could I know why you wish to do so, anyhow?

Cheers
Avatar of glass_cookie
glass_cookie

I agree with valli_an : )

By the way, do remember to create the controls with an index of 0.

That's it!

glass cookie : )
the Container property can be set as far as I am aware. I have used it before and it worked ok for me!
I agree with JonFish: you can set the container property at runtime (but no the parent Property)

ok, got it. I too agree with you. Just now, found in MS site.

Look at this link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbprostandardcontainerx.asp

But in MSDN, library, i dont know why, it is put Container property returns the container name, in one place. In another place it is put it returns/sets. But the search value is "Container Property" in both. :>

Cheers.
I tried with textbox, and with textbox control array, it works. :)
the Container property can be set as far as I am aware. I have used it before and it worked ok for me!
hmm sorry bout that. I did the unforgivable - refreshed :-(
You can use the forms controls collection to add controls dynamically at run time.  For example if you wanted to add a textbox at runtime dynamically to a frame control name "Frame1" use the following code.

'========================================================
Private m_TextBox As TextBox ' Reference created textbox

Private Sub Form_Load()    
    ' Create the control and set its parent to Frame1.
    Set m_TextBox = Me.Controls.Add("vb.Textbox", "m_textbox", Frame1)
    ' Ensure that the new text box is visible.
    m_TextBox.Visible = True
End Sub
DavidLester:

welcome to EE! It is common courtesy here to only comment on questions, rather than proposing an answer. Proposing an answer locks the question, and moves it to the "Answered Questions" section. You should not post "as answer" unless you are 300% sure your answer is 1) correct and 2) unique from what other users have commented. Please read the "Tips on comments and answers" at the bottom of this page.

I look forward to seeing you around EE!

To chaos_59:
Please reject the proposed answer and select a more appropriate one from the other comments...
JonFish85

My answer does exactly want chaos_59 has required.
Also I don't think you should tell him what he should/shouln't except, who else does what he wants but him.

However, your point on "comments as opposed to answers" is gracefully taken.


David Lester
yes, your answer has done what is required, but so does mine, no? I apologize for the comment about rejecting/accepting answers, and will be more careful in the future!
Avatar of chaos_59

ASKER

My appologies, I should have been more specific.
I am trying to add controls to a tabbed dialog control (SSTAB). 1 control for each tab.
I can control the number of tabs during run time. But I can't figure out how to add controls to each tab at run time.
I tried your suggestions, and although it adds the command buttons to the tab control, they are not placed on individual tabs. They are all placed on tab1.

This is the code that I am using.
Private Sub Form_Load()
SSTab1.Tabs = 27
SSTab1.TabsPerRow = 9

For x = 1 To SSTab1.Tabs - 1
    Load command1(x)   'load control
    command1(x).Visible = True
    SSTab1.Tab = x     'set current tab
    Set command1(x).Container = SSTab1
Next
End Sub
My appologies, I should have been more specific.
I am trying to add controls to a tabbed dialog control (SSTAB). 1 control for each tab.
I can control the number of tabs during run time. But I can't figure out how to add controls to each tab at run time.
I tried your suggestions, and although it adds the command buttons to the tab control, they are not placed on individual tabs. They are all placed on tab1.

This is the code that I am using.
Private Sub Form_Load()
SSTab1.Tabs = 27
SSTab1.TabsPerRow = 9

For x = 1 To SSTab1.Tabs - 1
    Load command1(x)   'load control
    command1(x).Visible = True
    SSTab1.Tab = x     'set current tab
    Set command1(x).Container = SSTab1
Next
End Sub
My appologies, I should have been more specific.
I am trying to add controls to a tabbed dialog control (SSTAB). 1 control for each tab.
I can control the number of tabs during run time. But I can't figure out how to add controls to each tab at run time.
I tried your suggestions, and although it adds the command buttons to the tab control, they are not placed on individual tabs. They are all placed on tab1.

This is the code that I am using.
Private Sub Form_Load()
SSTab1.Tabs = 27
SSTab1.TabsPerRow = 9

For x = 1 To SSTab1.Tabs - 1
    Load command1(x)   'load control
    command1(x).Visible = True
    SSTab1.Tab = x     'set current tab
    Set command1(x).Container = SSTab1
Next
End Sub
how about adding a frame in each tab, and changing the container to that frame?
I suggest you use my previous answer to add the controls to the tab control. Or as valli an suggested add the contols to a frame in each tab and then show/hide the frames depending on the visible tab.

WHAT MORE DO YOU WANT.
Valli an,
the problem with this solution is that it requires a preset upper limit and requires having all tabs, frames and controls to be loaded first at design time. I want to be able to add and remove tabs and load and unload controls as needed. For example, if I add a tab at run time, I want to load the controls for it, and have them placed on the new tab.

DavidLester,
I won't accept your answer because it essentially does the same thing as JonFish85's answer. If I am going to accept an incomplete answer the points will go to the first person that gives that answer.
WHAT MORE DO I WANT? I want an answer to my question!
It seems that I need to be more precise with my question. And in fairness JonFish85 answered this question correctly first. I'll post a new, more precise, question later. Hopefully someone can give me an answer that does what I want.