PDA

Click to See Complete Forum and Search --> : trying to get a value out of an Activex control


itmasterw
Jun 7th, 2006, 11:56 AM
Hi, I am trying to learn how to make my own Activex controls. I made this one, and it works great, which multiplies two numbers and sends the result to a hird text box. Now, I would like to do one more thing with it; that I can not seem to get. I woud like in the main program, that I brought this control into, to be able to send the result form the control to a text box on this main programs form. So for example if on the control text1 = 2, text2 = 3, and text3 = 6 I then want to hit a button and have the results form the control ( which = 6) go to a text box on the form which would show 6 in it. What would I have to do to make this happen?
Thank You


Here is my Code for the control:

Public Event CalculatorClick()

Public Property Get Operand1() As Variant
Operand1 = Text1.Text
End Property

Public Property Let Operand1(ByVal vNewValue As Variant)
Text1.Text = vNewValue
PropertyChanged "Operand1"
End Property

Public Property Get Operand2() As Variant
Operand2 = Text2.Text
End Property

Public Property Let Operand2(ByVal vNewValue As Variant)
Text2.Text = vNewValue
PropertyChanged "Operand2"
End Property

Public Sub CalculateResult()
Text3.Text = Text1.Text * Text2.Text
CalculateResult = Text3.Text

End Sub

Private Sub Command1_Click()
CalculateResult
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Operand1", Text1.Text
PropBag.WriteProperty "Operand2", Text2.Text

End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Text1.Text = PropBag.ReadProperty("Operand1")
Text2.Text = PropBag.ReadProperty("Operand2")

End Sub

Public Sub UserControl_Initialize()

End Sub

Keith_VB6
Jun 7th, 2006, 02:03 PM
Hi itmasterw,

I would use an event in your control that would notify your program of the value. In the sub that does the calculation add a Raise Event with the new value. Then in that event on your form, read the value and set your textbox to it.

Here's some example code:

'I don't know what you're value maximum is, so like
'the rest of your code, I left is it as a variant. The variant
'should be changed to the proper format, IE integer, long, double ETC..
'this would also, require the use of val()

'in your UserControl
Event CalcUpdate(Value As Variant)

'add RaiseEvent to this sub
Public Sub CalculateResult()
Text3.Text = Text1.Text * Text2.Text

RaiseEvent CalcUpdate(Text3.Text)
End Sub


'in your form
Private Sub NameOfYourControl_CalcUpdate(Value As Variant)
Text1.Text = Value
End Sub

itmasterw
Jun 7th, 2006, 07:15 PM
Thanks I have been tying to do this for awhile now.
That looks like it would do it, button the of my form how do I call this; I tryed something like this:

Private Sub UserControl1_CalcUpdate(Value As Variant)
Text1.Text = Value
End Sub

Private Sub Command1_Click()
UserControl1_CalcUpdate ()
End Sub

Also, I am not sure if I made myself clear, but this control is being brought into another VB program, and that is where I want to hit the button and have the result form this control go to the text box on that form.
Can you tell me what I need to complete this.
Thank You

Keith_VB6
Jun 7th, 2006, 07:44 PM
Ok, you say the control is being added to another project. Right ?

Did you try my code. You would first have to update the code in the usercontrol . Adding the event. And if the usercontrol project is in a seperate project, re-make the activeX control.

Let me look at your code and I'll reply asap.

Keith_VB6
Jun 7th, 2006, 07:55 PM
Instead of an Event, try...
'In the UserControl
Public Function GetCalc() as long
GetCalc = Val(Text1.Text) * Val(Text2.Text)
End Function

'In the Program
Private Sub Command1_Click()
text1.text=UserControl1.GetCalc()
End Sub
Does the same thing. Just less code.

BTW... What does the program do ? Is it just a test app or something in particular ?

itmasterw
Jun 7th, 2006, 08:44 PM
Yes, I tryed have your code and I now changed it to the your function; but it still is not going. we are probably just missing some small thing. Here is what I have now.

This is in my control:

Public Property Get Operand1() As Variant
Operand1 = Text1.Text
End Property

Public Property Let Operand1(ByVal vNewValue As Variant)
Text1.Text = vNewValue
PropertyChanged "Operand1"
End Property

Public Property Get Operand2() As Variant
Operand2 = Text2.Text
End Property

Public Property Let Operand2(ByVal vNewValue As Variant)
Text2.Text = vNewValue
PropertyChanged "Operand2"
End Property
Public Property Get Result() As Variant
Result = Text1.Text * Text2.Text
End Property

'Public Function CalculateResult()
'Text3.Text = Text1.Text * Text2.Text
'CalculateResult = Text3.Text
'Gla = Text3.Text
'End Function

Public Function GetCalc() As Long
GetCalc = Val(Text1.Text) * Val(Text2.Text)
End Function

Private Sub Calculator_Click()
Text3.Text = Text1.Text * Text2.Text
Gla = Text3.Text
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Operand1", Text1.Text
PropBag.WriteProperty "Operand2", Text2.Text

End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Text1.Text = PropBag.ReadProperty("Operand1")
Text2.Text = PropBag.ReadProperty("Operand2")

End Sub

Public Sub CalculateResult()
Text3.Text = Text1.Text * Text2.Text

RaiseEvent CalcUpdate(Text3.Text)
End Sub

Public Sub UserControl_Initialize()

End Sub


And this si in my Mian form on the other program:


Private Sub Command1_Click()
Text1.Text = UserControl1.GetCalc()

End Sub

Keith_VB6
Jun 8th, 2006, 09:40 AM
I copied your code into a project and it worked.

Are your new project and usercontrol projects separate or are they in one project.

If they're separate, then you might not have re-made the usercontrol.

I'm attaching the project that I made from your code. Only slightly modified.

The simplest way to build and test usercontrols is by putting both in one project. Create or load a basic form project, then insert a usercontrol to the same project. [project] -> [add usercontrol]

Also, do you know how to use the ActiveX Control Interface Wizard Addin ? It's a great time saver and a learning tool.

itmasterw
Jun 8th, 2006, 11:48 AM
Thanks for your help,and this works great here. The problem is that I can not use this same code in another program. that is:

Text4 = usercontrol1.GetCalc()

I jsut tried it and it blowes up on the above code because, even though I added the control to the project it does not have some connection that it needs. I even tryed looking in refernces but I not sure what to do here. Buit nothing I do works.

Let me explain a minute, because I know its me I just am not explaining it right. I am trying to learn how to make my own controls and use them in an program that I make. Where I work other programers do this all the time, the problem is they are not so willing to show how. In the above it woud seem to me that in another program you woud have to create an usercontrol object (some how?) and then you should be able to access the its member methods; like a class, but I can not figure out how to do that.
Thank You

Keith_VB6
Jun 8th, 2006, 02:19 PM
Ok, I'll admit it's difficult to explain. Giving advice on code is a lot simpler than how to set something up.

When you have a stand alone activeX project, you do all the code and interface for the control. And then under the "Project" menu you click "make projectname.ocx" , if there's no errors then that makes the control and registers it with the system. It doesn't matter where you save the file, the system registers it.

Then, to use it in in another project, you click [projects] menu --> [components] and from the list check the box before the file.

The project I upload was probably a bad example, if you're using it else where.

Take a look how I have the attached file. Open the "UC Group.vbg" file.
I don't know if this is how you were making yours. To compile the activeX control so, other programs can use, select the usercontrol group from the "project explorer" from the "File" menu, click "Make UC_TextActivex.ocx" item. Choose a filename.

Now to use it in another program just select it from the [project] menu -> [components] screen.

If I'm not explaining this clearly, I apologize. It's difficult to explain.

itmasterw
Jun 8th, 2006, 07:31 PM
yes I did not mention it but I do make the OCX like you did, nad to draw it on the screen I go through components. but hten I also add it to the projject. But un fortunently it still will not reconize the function in the button. I did notice that you have a group file as well. How did you make it? Also I was woundering if you used it in another program?
Again thanks for your help.

Keith_VB6
Jun 8th, 2006, 08:01 PM
It sounds like either a code or maybe the control isn't being rebuilt. Did you try to use the control in the 2nd project I posted in another project. ( use the make .ocx function ?

If your program still isn't working, then zip the activeX project and the Form projects and I'll take a look.

I did notice that you have a group file as well. How did you make it?

Everything I did was in VB 6 ( professional edition )

Group files are easy to make. Just load or create a project and then click the [File] menu and [add project]. Then either add a [new] [existing] or [recent] project. In the "project explorer" hightlight the project name and right click to make a project the "Startup Project". So, If you had a form in one and an activeX in the other, you would set the form to be the startup.