Results 1 to 12 of 12

Thread: [RESOLVED] Declaring a User Control At Runtime

  1. #1

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Resolved [RESOLVED] Declaring a User Control At Runtime

    I'm wondering if there is a way to declare a user control at runtime. If the user control is compiled into a separate OCX file, you could use the following syntax to declare it:
    VB Code:
    1. ' Assume that this user control button is in the file "kControl.ocx"
    2. Private WithEvents foCmdEnd          As kButton
    3.  
    4. ' ..Further down..
    5.  
    6. ' Exit/End Program Button
    7. Set foCmdEnd = Me.Controls.Add("kControl.kButton", "coCmdEnd")
    However, assuming that the User Control isn't in a separate OCX file, but it is in the main Project as a User Control, the following lines won't work:
    VB Code:
    1. ' Error 711:  Invalid Class String
    2. Set foCmdEnd = foform.Controls.Add("kControl.kButton", "coCmdEnd")
    3.  
    4. ' Error 711:  Invalid Class String
    5. Set foCmdEnd = foform.Controls.Add("VB.kButton", "coCmdEnd")

    Do I have to have it in a separate OCX file if I want to declare it at run time or do I have other options? I can add the object at design time without any hitch, but I can't say the same for adding it at runtime. I don't mind it being in a separate OCX file, but when you have some buggy issues with a user control in a big project file--and not the small one you created to test out the control--it becomes rather annoying to test & correct.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Declaring a User Control At Runtime

    VB Code:
    1. Set foCmdEnd = Me.Controls.Add("[I]ProjectName[/I].kButton", "coCmdEnd")
    if it's not used anywhere in your project (at design time) then you'll have to uncheck the "Remove information about unused ActiveX controls" checkbox under the Make tab of the project properties

  3. #3

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by bushmobile
    VB Code:
    1. Set foCmdEnd = Me.Controls.Add("[I]ProjectName[/I].kButton", "coCmdEnd")
    if it's not used anywhere in your project (at design time) then you'll have to uncheck the "Remove information about unused ActiveX controls" checkbox under the Make tab of the project properties
    That checkbox isn't checked.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by Fedhax
    That checkbox isn't checked.
    okay... was that a comment on whether it's working or not? or were just keeping me informed

  5. #5

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by bushmobile
    okay... was that a comment on whether it's working or not? or were just keeping me informed
    A statement to keep you informed.

  6. #6

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    To add what I've discovered thus far:I am loading a form via
    VB Code:
    1. Form1.Show vbModal
    On this form, I have my 2 user control buttons (coCmdNo & coCmdYes) and a frame (Fra1). I'm using the frame to load different forms into Form1. You click on coCmdYes, it will move you onto the next form. If you click on coCmdNo, it will move you back onto the previous form. From what I've learned, this function is causing the buttons to behave questionably:
    VB Code:
    1. Private Sub Show_NewForm(ByRef roForm As Form)
    2.     Form1.Hide
    3.    
    4.     SetParent roForm.hWnd, Fra1.hWnd
    5.     roForm.Show
    6.  
    7.     Form1.Show vbModal  ' <--- If you remove the vbModal option, the user controls behave normally
    8. End Sub
    This discovery makes me think that my user controls' timers/values are getting messed up in this transition function, and I want to troubleshoot the controls to resolve this issue if it is fixable.

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Declaring a User Control At Runtime

    you'll have to set the WS_CHILD style of the form if you're placing it in a modal form: http://www.vbforums.com/showthread.php?t=427380

  8. #8

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by bushmobile
    you'll have to set the WS_CHILD style of the form if you're placing it in a modal form: http://www.vbforums.com/showthread.php?t=427380
    Using this example, it appears that the WS_CHILD style is applied to the child form:
    VB Code:
    1. Public Sub NewAttachSubForm(ByVal NewParent As Frame, ByVal TheForm As Form)
    2.  
    3.   Debug.Print SetWindowLong(TheForm.hwnd, GWL_STYLE, GetWindowLong(TheForm.hwnd, GWL_STYLE) Or WS_CHILD)
    4.   Call SetParent(TheForm.hwnd, NewParent.hwnd)
    5.   TheForm.Move 0, 0, NewParent.Width, NewParent.Height
    6.   TheForm.Visible = True
    7.  
    8. End Sub
    However, my user controls are on the parent form. Do I need to set the WS_CHILD style on the parent form for my problem?

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Declaring a User Control At Runtime

    oh right, i didn't really understand your post (#6)

    without knowing either a) your problem or, b) the code you're using, i'm not really sure that it's possible to give an answer - were you expecting one?

    and in answer to your setting WS_CHILD style on the parent form question - no, don't do that.

  10. #10

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by bushmobile
    oh right, i didn't really understand your post (#6)

    without knowing either a) your problem or, b) the code you're using, i'm not really sure that it's possible to give an answer - were you expecting one?
    a) I am loading a form (Form1) using the vbModal option. On this form, I have my 2 user control buttons (coCmdNo & coCmdYes) and a frame (Fra1). I'm using Fra1 to load different forms. You click on coCmdYes, it will move you onto the next form. If you click on coCmdNo, it will move you back onto the previous form.

    b)
    VB Code:
    1. Private Sub Show_NewForm(ByRef roForm As Form)
    2.     Form1.Hide
    3.    
    4.     SetParent roForm.hWnd, Fra1.hWnd
    5.     roForm.Show
    6.  
    7.     Form1.Show vbModal  ' <--- If you remove the vbModal option, the user controls behave normally
    8. End Sub
    Form1.coCmdNo & Form1.coCmdYes start behaving irradically once I call Form1.Show. If I don't pass the vbModal option, then everything (Form1, Fra1, coCmdNo, & coCmdYes) behave as expected.

    Am I expecting an answer? Well, I can't load a UserControl at runtime--if I use a UserControl file instead of an OCX--because I keep getting an error 711. If I load the controls via OCX, I can load the controls, but I can't debug the bloody things when they go all mucked after my Form1.Show vbModal call.

    You tell me if I have a question that can be answered 'cause I have a VB project that won't work as it is currently designed, and I'd like to continue working on it.

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Declaring a User Control At Runtime

    ok

    what you wrote for a) isn't the problem, it's a description of what you expect it to do - the problem is that "Form1.coCmdNo & Form1.coCmdYes start behaving irradically" but that could mean just about anything.

    and regarding b), if problem is occuring with the two usercontrols then there error must be there - not with that bit of code - that doesn't tell us anything.

    lastly i told you how to load a usercontrol at runtime with post #2

  12. #12

    Thread Starter
    Hyperactive Member Fedhax's Avatar
    Join Date
    Aug 2006
    Posts
    293

    Re: Declaring a User Control At Runtime

    Quote Originally Posted by bushmobile
    lastly i told you how to load a usercontrol at runtime with post #2
    I just decided to attack the problem from a different angle to avoid my issues--however poorly documented they were. By not using the vbModal option when I show Form1 and rewriting some of the code that followed the Form1.Show call, I am able to load the controls as you instructed, and they behave as expected.

    I'm not sure what to make of it exactly, but I do know that sometimes it is better to stop, think about alternative solutions, weigh those solutions against your time spent attacking a problem & your desired outcome, and chart a different course if necessary. Now, I'm getting back up to speed and the rest of my application is performing as it was before I took my class hacks and placed them in a separate OCX file.

    Finally, you are correct in that the code I posted was not the source of the strange behavior, but why would my controls work:

    1) Before the Form1.Hide
    2) After the Form1.Show (no vbModal)

    but not

    3) Form1.Show vbModal

    ? Does vbModal do a lot more behind the scenes than I give it credit?

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