Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

How to pass values from ActiveX Control to Standard exe

I've just created a simple ActiveX, say, UserControl1 by using VB 6 sp5, and then I registered the ocx file by using regsrv32.

Next, I created a new EXE project and then add that ActiveX with three textfields namely TextBox1, TextBox2 and TextBox3. Finally, I created three textfields on my newly EXE project, namely txtTextBox1, txtTextBox2 and txtTextBox3 and a button.

After clicking the button, the values of the textfields on the ActiveX Control will be copied to those on the EXE one, say,

txtTextBox1.Text = TextBox1.Text
txtTextBox2.Text = TextBox2.Text
txtTextBox3.Text = TextBox3.Text

But, what's the syntaxt then? After I typed "UserControl1", I couldn't see any TextBox1, TextBox2, TextBox3 on the selection.

Thanks for the help!
  • 2
1 Solution
You haven't created any properties that are exposed to the container application. There is an add-in activex control wizard that will automate some of this for you. I would suggest that as you are starting with this you use the wizard to create the properties and link them to the underlying properties of the controls that you have placed in the usercontrol. Then examine the resulting code to understand how properties can be employed to do this. The simplest property method(s) would be something like this:

Public Property Let TB1(ByVal TBText As String)
  Textbox1.Text = TBText
End Property

Public Property Get TB1() As String
  TB1 = Textbox1.Text
End Property

Don't forget to use the Usercontrol_ReadProperties and
Usercontrol_WriteProperties, there you can store and retrieve the values in the PropertyBag, which means you don't lose the values after closing the project, maybe not necessary in this case though.
mscprojAuthor Commented:
Yes, I see. But, if I create the ActiveX control, and want to build a simple exe project that has two that ActiveX Control.

After I enter the values on the three textfields on 1st ActiveX Control, then enter a button, it will update those three textfields on 2nd ActiveX Object.

Also, after I change the valueson the three textfields on the 2nd ActiveX Control, then enther another button, it will update those three textfields on the 1st Active Object.

How to write the coding the those two button events?

I've written something like

Private Sub CommandButton1_Click()
   MainControl2_getText1 (MainControl1.getText1)
   MainControl2_getText2 (MainControl1.getText2)
   MainControl2_getText3 (MainControl1.getText3)
End Sub

Private Sub CommandButton2_Click()
   MainControl1_getText1 (MainControl2.getText1)
   MainControl1_getText2 (MainControl2.getText2)
   MainControl1_getText2 (MainControl2.getText3)
End Sub

My ActiveX Control is something like this:

Public Property Get getText1() As String
   getText1 = TextBox1.Text
End Property

Public Property Let getText1(ByVal vNewValue As String)
   TextBox1.Text = vNewValue
End Property

Thanks again!
Actually it would be:

Private Sub CommandButton1_Click()
  MainControl2.getText1 = MainControl1.getText1
  MainControl2.getText2 = MainControl1.getText2
  MainControl2.getText3 = MainControl1.getText3
End Sub

Private Sub CommandButton2_Click()
  MainControl1.getText1 = MainControl2.getText1
  MainControl1.getText2 = MainControl2.getText2
  MainControl1.getText2 = MainControl2.getText3
End Sub

As a property is assigned using the = operator e.g., PropertyName = PropertyValue as with the intrinsic properties of objects.

You can also simplify this slightly using a with .. end with block:

Private Sub CommandButton1_Click()
  With MainControl2
    .getText1 = MainControl1.getText1
    .getText2 = MainControl1.getText2
    .getText3 = MainControl1.getText3
  End With
End Sub

Private Sub CommandButton2_Click()
    .getText1 = MainControl2.getText1
    .getText2 = MainControl2.getText2
    .getText2 = MainControl2.getText3
  End With
End Sub

This reduces the number of times that VB has to resolve the property of the object as it already gains a reference to the main object in the With statement and can then identify all properties and methods of that object without having to do this in each subsequent line. In some circumstances this can increase processing speed and is a recommended practice where possible.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now