Results 1 to 18 of 18

Thread: Set the parent of a control at runtime

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Set the parent of a control at runtime

    The Parent property on every control is only read-only at runtime. What other way would I set the parent of any control at runtime? I Have tried "SetParent" but it won't work for label control (doesn't have hWnd property) and don't know why it doesn't respond to any event when it's showed up on it's new parent form.
    Baaaaaaaaah

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    try
    VB Code:
    1. Set Label1.Container = Frame1

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Stiletto
    Guest

    Re: Set the parent of a control at runtime

    Originally posted by abdul
    The Parent property on every control is only read-only at runtime. What other way would I set the parent of any control at runtime? I Have tried "SetParent" but it won't work for label control (doesn't have hWnd property) and don't know why it doesn't respond to any event when it's showed up on it's new parent form.
    Sorry for this question, but *** is Parent?, and what blade's code does?

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by crptcblade
    try
    VB Code:
    1. Set Label1.Container = Frame1

    Hmmm, it's not working. Shows me "Invalid Object Use" error.
    My code is like:
    VB Code:
    1. Set frmExtra.Command1.Container = whichform

    So the code is in the module, and it takes the control from frmExtra and shows that on whichform.
    Baaaaaaaaah

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Sorry, that method won't work between forms.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Re: Re: Set the parent of a control at runtime

    Originally posted by Stiletto

    Sorry for this question, but *** is Parent?, and what blade's code does?
    Parent property of a specific control contains the object that this control in placed on. Blade's code doesn't work yet.
    BTW, are you drunk?
    Baaaaaaaaah

  7. #7

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by crptcblade
    Sorry, that method won't work between forms.
    Awww, any other way you can think of to switch the control among different forms?
    Baaaaaaaaah

  8. #8
    Stiletto
    Guest

    Re: Re: Re: Set the parent of a control at runtime

    Originally posted by abdul


    Parent property of a specific control contains the object that this control in placed on. Blade's code doesn't work yet.
    BTW, are you drunk?
    Me, no...
    So you mean like this:
    VB Code:
    1. Form1.Command1.Container = Form1
    ?

  9. #9

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Re: Re: Re: Re: Set the parent of a control at runtime

    Originally posted by Stiletto

    Me, no...
    So you mean like this:
    VB Code:
    1. Form1.Command1.Container = Form1
    ?
    Maybe this will work but I want to go from one form to another like this:
    VB Code:
    1. Form1.Command1.Container = Form2
    Baaaaaaaaah

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Can I ask why you need to do this? Maybe there is a better way.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I want the user to be able to add different controls on the form at runtime. Obviously, I'll have more than 1 form and each form should be loaded with no control (empty like in vb) then you can choose a control from the toolbar and draw it on the form you have chosen. Right now, I have places all the common controls on one form, frmExtra, and when the user wants to draw a control on a specific form, I just increase the dimension of the array of that specific control and then move it to the form where the user wants to draw it.
    Maybe there is another way but I can't think of that right now...
    Baaaaaaaaah

  12. #12
    Stiletto
    Guest
    Lemme get this. If the control is on Form1, so the Container property of that control will be Form1, is that correct?
    If so, as i understand it, you cant do this Abdul:
    VB Code:
    1. Form1.Command1.Container = Form2
    Since the control is on Form1.
    Or this is possible, and it will remove the control from form1 to form2?

  13. #13

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    This method will give you an error message that's why I am looking for another way to do this.
    Baaaaaaaaah

  14. #14
    just make a control array on the form u want 2 add 2 then use:

    load command(x)
    command(x).top=command(x-1).top+120
    command(x).visible=true

  15. #15

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Yes, but I'll have to do that on every single form. I am trying another way right now but I think this method will also work with more ease.
    Baaaaaaaaah

  16. #16
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    try using the SetParent API
    VB Code:
    1. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    2.  
    3. SetParent Form1.Command1.hWnd, Form2.hWnd
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  17. #17

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by BuggyProgrammer
    try using the SetParent API
    VB Code:
    1. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    2.  
    3. SetParent Form1.Command1.hWnd, Form2.hWnd
    1) Somehow, it just doesn't respond to any event on the command button quite right.
    2) It won't work for a label control because it doesn't have a 'Hwnd" property.
    Baaaaaaaaah

  18. #18
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    well then you're out of solutions now.........
    Remember, if someone's post was not helpful, you can always rate their post negatively .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width