Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActiveX example not working 1

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I am trying to learn how to make an ActiveX control. I followed a tutorial but all it does is make a messagebox come up with its titlebar having the name of the control. What is it supposed to teach me? I am pasting the code unless its wrong.Thanks

Dim TextVariable As String
Event Click()
Event KeyPress(Keyascii As Integer)

Private Sub Command1_Click()
MsgBox (TextVariable)
RaiseEvent Click
End Sub

Private Sub Command1_KeyPress(Keyascii As Integer)
RaiseEvent KeyPress(KeyAsci)
End Sub

Private Sub MyControl_ReadProperties(PropertyBag)
TextVariable = PropBag.ReadProperty("Text", "There is no message")
End Sub

Private Sub MyControl_Resize()
Command1.Width = MyControl.Width
Command1.Height = MyControl.Height
End Sub

Public Property Get Text() As String
Text = TextVariable
End Property

Private Sub MyControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Text", TextVariable, "There is no message")
End Sub

Public Property Let Text(ByVal New_Text As String)
TextVariable = New_Text
PropertyChanged = "Text"
End Property
 
I compiled the control, it showed two errors Ascii in raise event having only one I, and PropertyChanged having a wrong = sign. I compiled it okay. I started a new project, put the control on a form. The tutorial then said enter "Hello" into the control text property, run the program and a hello message will appear - Huh Nothing??? Thanks
 
I cannot move on! I did a debug and nothing happens after the RaiseEvent? Anybody passing by that knows the answer? Thanks
 
Domino2 said:
The tutorial then said enter "Hello" into the control text property, run the program and a hello message will appear - Huh Nothing???
Are you sure it didn't also say you need to click on the control for the messagebox to appear?

I did a debug and nothing happens after the RaiseEvent?
You need to write code in your form to handle the event. Its exactly the same as if you dragged a button on to your form, nothing will happen when you click on the button until you write some code in the button's _Click event.

You are probably getting ahead of your tutorial - don't assume something is wrong until it says something is supposed to happen and it doesn't.
 
>run the program and a hello message will appear

The tutorial says

Run the program and click the button.
A "hello" message box
 
It says more than that:

Insert it to your form, resize it to your preferred size,
and insert "hello" to the control Text property.
Run the program and click the button.
A "hello" message box is popping.

I went into the text property and put Hello in. I ran it and all thet happened was a messagebox opened, with okay on its button, and the name of the control in its title bar. When I went back into the text property again it was blank.

Never having ever made an ActiveX control before I don't understand this line:

TextVariable = PropBag.ReadProperty("Text", "There is no message")

Cannot see the reason of typing in There is no message as again that does not do anything.

Maybe my VB6 is corrupted although everything else seems to work. Thanks again
 
>I don't think I have skipped anything from the tutorial?

At what point did the tutorial tell you to change [tt]UserControl[/tt] to [tt]MyControl[/tt]? Change them back, and all should work exactly as the tutorial says.
 
That's the public name of the control, which is used as the default root for instances of the control used in projects. It is not the internal name used by all the internal references of the user control (events, properties and methods), which remains UserControl.

 
Thanks. I rebuilt everything from scratch following everything. I selected a project of an ActiveX control. I right clicked the form, changed the name to MyControl. Added the command button, put in all the code. Saved the ocx. I then opened a new exe project, added the usercontrol MyControl and then tried running it. It gave a message:

MyControl can not be public in this type of project. The item has been changed to private

I opened the command buttons properties and changed the changed the text to Hello, and what do you know it worked.

Thanks very much for sorting it out. Have a star

 
Few more questions now this bit is working. I dont see how the command buttons text is being passed through to the messagebox.

I am also hunting to see how I can affect something on the activeX control from the form its on. Basically I am trying to make an activeX control with the MSComm6 control on. The activeX will have all the code on it together with other command buttons on, so I am trying to see how I can get things to go into the control from the form, vice versa. There seems so few simplified examples out there, thanks
 
>the command buttons text

It isn't the command button's text. It is the usercontrol's text property. In this example, when you set the usercontrol's text property it firstly sets an internasl variable (TextVariable) to that value and also flags that a property called Text has been changed which causes the control's PropertyBag to be updated with that value (although frankly you can safely ignore this latter bit for now)

When you click the usercontrol commandbutton it passes the value of the internal, privae variable called TextVariable to the messagebox.



Ok, an example of something affecting the control from your (hosting form) might be as follows

1) Chnage the Command1_Click event in the usercontrol from

Private Sub Command1_Click()
MsgBox (TextVariable)
RaiseEvent Click
End Sub

to

Private Sub Command1_Click()
RaiseEvent Click
MsgBox (TextVariable)
End Sub

and recompile.

2) Add a textbox to your hosting form.
3) Paste in the following code:

Private Sub myControl1_Click()
myControl1.Text = Text1
End Sub

Run your program. Now enter any text that you like into Text1, and click the usercontrol ...
 
Many thanks for running through that, very appreciated. Is the text property the only in/out "communication" between a form/usercontrol usercontrol/form or can you put variables in the usercontrol for runtime in/out use. Should be my last issue. Regards
 
>Is the text property the only in/out "communication"

No. It is the only one specifically set up for this example/tutorial but you can add whatever properties, methods and events you like to a user control.

We don't really have the time to provide a full suite of tutorials concerning the creation and use of ActiveX user controls in VB. There are plenty of on-line resources already available, however. ActiveX usercontrols are really just a part of ActiveX components, so a starting point can be found here.
 
Many thanks for the link. Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top