[RESOLVED] Preventing an ocx control to be placed on the form
Hi,
I am working on an activeX control. I want the control to check the registry and exit if no values found.
Details:
Here is what I require.
1. I start a new project and add myControl from the Project>Components... to the project.
2. Now I place an instance of the control on the form.
3. The control should check the registry for some specific values. And if it fails, it should display a message and quit...i.e. No instance should be placed on the form... (I want the same behavior as what can be obtained by Project>Properties and checking the "Require License Key". But I want my own code to check the registry.)
The problem I have when I write the following code in the initialize section of the control, it gives error.
Dim verified As Boolean
If Not verified Then End
How can I stop the placement of the control on the form.
Thanks in advance.
[937 byte] By [
jawadSahil] at [2007-11-20 10:03:19]

# 1 Re: [RESOLVED] Preventing an ocx control to be placed on the form
First tell me for what purpose you want to add this kind of feature. Also you if you are going to use this ActiveX in only one software, I would suggest you to add the ActiveX in your project ONLY rather than making seperate ActiveX and adding OCX reference, like this you need not distribute your OCX file with your software and also will be hidden from the end-user.
# 2 Re: [RESOLVED] Preventing an ocx control to be placed on the form
I haven't found a way to unload a control silently, but there are a few things you could do in order to prevent its use.
One way might be to set a flag once the proper registry data has been found. So if this flag is not set, other code within the control can use that to determine whether or not to execute. So essentially the control would remain on the form, but not function.
Another way is to use Unload Me if the required data is not found. This will produce an error message, and no control will be placed. I doubt the message can be replaced with your custom message however. It will say something like "Can't load or unload this object". I suppose you could show your message before the unload statement.
# 3 Re: [RESOLVED] Preventing an ocx control to be placed on the form
Well, I have encapsulated some functionality in the OCX and I want to deliver this ocx to my co-developers. I want them not to be able to see the code. Also if the ocx is not registered (The ocx will check the registry for certain values and if the values are not found, it will give a message and exit) , they should not even be able to use it.
I am using this activeX in almost every software I develop...
# 4 Re: [RESOLVED] Preventing an ocx control to be placed on the form
Thanks Wizbang
Private Sub UserControl_Initialize()
dim verified as boolean
'verified=getVerification
If Not verified Then Err.Raise 999, , "Not Licenced"
End Sub
This works for me. Now The problem is that I am not able to display the "Not Licensed" message. The attachment shows the message box I get.
If I set verified=true, everything is fine.
??
# 5 Re: [RESOLVED] Preventing an ocx control to be placed on the form
A blank error message? I've never heard of this one. I just tried your code in a dummy control, and it works. I did not try compiling it though, so the message box had the four buttons, including End and Debug.
Anyway, I do have an additional idea, shown below:
Private Sub UserControl_Resize()
Dim verified As Boolean
'verified=getVerification
If verified Then
'Do the rest of whatever the control normally does
Else
UserControl.Cls
UserControl.AutoRedraw = True
UserControl.Print "Not Licensed"
UserControl.AutoRedraw = False
End If
End Sub
This will show the "Not Licensed" message on the window for the control itself. If the control is designed to be visible, this may work depending on what your control does. I have often used the resize event to "launch" a control's functionality, because it fires after the control is seated on the form. You could also have a module level flag, and exit any functions (or show a message) for code which shouldn't execute.
I'm not suggesting you give up on disallowing the control to be placed. Only that there are other ways which might potentially be useful, depending on the circumstances.
# 6 Re: [RESOLVED] Preventing an ocx control to be placed on the form
Thanks wizbang.
The problem occurs only when I compile the ocx... But that is not a big deal as this ocx is only for in house usage.
Thanks again for help