|
-
Sep 19th, 2006, 11:42 AM
#1
Thread Starter
Hyperactive Member
[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:
' Assume that this user control button is in the file "kControl.ocx"
Private WithEvents foCmdEnd As kButton
' ..Further down..
' Exit/End Program Button
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:
' Error 711: Invalid Class String
Set foCmdEnd = foform.Controls.Add("kControl.kButton", "coCmdEnd")
' Error 711: Invalid Class String
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.
-
Sep 19th, 2006, 11:56 AM
#2
Re: Declaring a User Control At Runtime
VB Code:
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
-
Sep 19th, 2006, 12:04 PM
#3
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
 Originally Posted by bushmobile
VB Code:
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.
-
Sep 19th, 2006, 12:08 PM
#4
Re: Declaring a User Control At Runtime
 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
-
Sep 19th, 2006, 12:14 PM
#5
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
 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.
-
Sep 19th, 2006, 12:38 PM
#6
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
To add what I've discovered thus far:I am loading a form via 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:
Private Sub Show_NewForm(ByRef roForm As Form)
Form1.Hide
SetParent roForm.hWnd, Fra1.hWnd
roForm.Show
Form1.Show vbModal ' <--- If you remove the vbModal option, the user controls behave normally
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.
-
Sep 19th, 2006, 12:54 PM
#7
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
-
Sep 19th, 2006, 01:03 PM
#8
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
 Originally Posted by bushmobile
Using this example, it appears that the WS_CHILD style is applied to the child form:
VB Code:
Public Sub NewAttachSubForm(ByVal NewParent As Frame, ByVal TheForm As Form)
Debug.Print SetWindowLong(TheForm.hwnd, GWL_STYLE, GetWindowLong(TheForm.hwnd, GWL_STYLE) Or WS_CHILD)
Call SetParent(TheForm.hwnd, NewParent.hwnd)
TheForm.Move 0, 0, NewParent.Width, NewParent.Height
TheForm.Visible = True
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?
-
Sep 19th, 2006, 01:07 PM
#9
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.
-
Sep 19th, 2006, 02:15 PM
#10
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
 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:
Private Sub Show_NewForm(ByRef roForm As Form)
Form1.Hide
SetParent roForm.hWnd, Fra1.hWnd
roForm.Show
Form1.Show vbModal ' <--- If you remove the vbModal option, the user controls behave normally
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.
-
Sep 19th, 2006, 03:01 PM
#11
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
-
Sep 19th, 2006, 03:31 PM
#12
Thread Starter
Hyperactive Member
Re: Declaring a User Control At Runtime
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|