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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you add forms to ActiveX controls? 2

Status
Not open for further replies.

dazlogan

Programmer
Aug 1, 2003
6
0
0
GB
Hello,

Is it possible to add forms to activeX controls (using VB6)?

What im thinking about is having a main control, but being able to invoke tool boxes/dialogs etc. from the control.

The main control and toolboxes/dialogs must be able to communicate back and forth.

I would appreciate a quick response.

Thanks.

Regards,
Darren
 
Yes you can. Start a new ActiveX project with VB. Add a command button to your ActiveX control. Next add a form to the project. Then just use the command buttons click event to call the form's "Show" method. You will probably want to open the form as modal. Hope this helps. [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Hi,

Thanks for the quick response.

I have actually been able to invoke a form BUT...

How do I communicate with the main control from that form?

For example, lets say the form has a button and when this button is pressed I want to display some text on the main control.
How do I do this?

I've tried:

MyControlName.label1.caption="some text" but I get an error (object required).

In other words, the form can't talk back to the control. I guess I am not addressing the control properly?

Thanks.

Regards,
Darren
 
The ActiveX control's form will have no way of knowing that the container(main) form has a specic control, much less a way to identify it. You may want to determine a different way to accomplish the task at hand. BTW, why would you want to perform such a task?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Hi!,

I should imagine there are many occasions when one would want to do this.

Lets say you design a chart contol. The chart may have hundreds of properties which would not be possible to fit into one area - so what you'd do is have several supporting toolboxes (forms) - one with say colour selection, another with x-axis parameters etc.

What would you do in this situation?

Regards,
Darren
 
If I understand you correctly, you would need to make them properties of the ActiveX control. You could then "Read" the properties of the control in the main for by using and event the would be triggered by the ActiveX control when the property changed. Is that what you are looking for? [flip]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Hi!,

Yes I know where you are coming from regarding the properties, but my original
question still remains.

I still would like to add forms to my control - forms which can communicate with one-another and
to the master control.

This must be possible - perhaps via API or some other more in-depth programming techniques.

Yes, I could just add properties but ideally I'd like forms with my control.

If you can find out how to do this I'll give you a gold star!

:cool:

Regards,
Darren
 
The two forms of the ActiveX control are no problem because they are a part of the control and you can regulate them. To pass info to them you just use their name. Given 2 forms, Form1 and Form2, each containing a textbox called Text1, then text can be set like this

Form1.Show
Form1.Text1.Text = "My text for form 1"

and

Form2.Show
Form2.Text2.Text = "My text for form 2"


Is that what you are looking to do? [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Hello!,

Nearly !!!

Adding two forms and communicating between them is not a problem, the problem is communicating back to the control.

What i've got now is a project group consisting of an activeX control and two forms.

The activeX can show the forms no problem (form1.show, form2.show etc.) - this works.
Form1 and Form2 talk to each other no problem.

What I want to be able to do now is pass values etc. from form1 or form2 back to the control. When I try to do this, I get an error "object required" (i.e. there doesn't seem to be any backward link from form to control but passing stuff from control to form is fine!!!!!!)

Most frustrating.

Any clues?

Regards,
Darren
 
Dazlogan, I noticed you never did get an answer to your question. I just posted a new version of the same question. Did you ever figure out how to communicate from the forms back to the usercontrol? If so, please share the trick with me.
 
I don't understand why you would not use Properties of the ActiveX control but you could also add a Bas module to the the control and pass data back to the control via variables in the module.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Thanks for responding, Foada. I agree that it is no problem to change variable values between forms and the usercontrol using properties. However, what I am unable to do is call a sub procedure in the usercontrol from a form
(that is also within the OCX). I need to do this in order to update the information displayed in the usercontrol. If I could call a sub in the container program which would in turn call the appropriate sub in the usercontrol, this would work also. But I have been unable to find an object reference for a call from the form that will do either. If you have any suggestions for solving this problem, I would be most grateful to hear them.

Regards,

Orgdoctor
 
Give this a try. Start a new VB ActiveX control project. Add a command button to the control and rename the control as MyCtrl. Rename the ActiveX project as MyCtrlPrj. Add a form to the ActiveX project and rename the form as MyCtrlForm and add two command buttons to this form. Then add a standard exe VB project and set it as the startup project. Add the following code to the usercontrol MyCtrl.ctl file

Option Explicit
Private WithEvents m_frmCallBack As MyCtrlForm

Private Sub Command1_Click()
m_frmCallBack.Show
End Sub

Private Sub m_frmCallBack_MyCallBack(ByVal sData As String)
Command1.Caption = sData
End Sub

Private Sub UserControl_Initialize()
Set m_frmCallBack = New MyCtrlForm
End Sub

Add this following code to the MyCtrlForm.frm

Option Explicit
Event MyCallBack(ByVal sData As String)

Private Sub Command1_Click()
Unload Me
End Sub

Private Sub Command2_Click()
RaiseEvent MyCallBack("My Data")
End Sub


Finally drop your new control on the form that was created when you added the standard exe project(by default it should be called Form1) and run the project and click on the command button on the form1(which is the usercontrol). It should bring up the Usercontrol form which has 2 command buttons on it. If you click on Command2 it should change the caption on the Command1 button on Form1. Does that make sense?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Foada, thanks so much for spending the time to come up with a proposed solution. I followed your instructions exactly, but immediately ran into a problem when I tried to compile the OCX to prepare to drop it into the container program. I got the error message "User type not defined" for the statement, "Private WithEvents m_frmCallBack As MyCtrlForm". I tried preceding it with "Private MyCtrlForm as Form", but this led to the same error. So, somehow we need to get MyCtrlForm recognized as an object or user type before we can reference it. Any suggestions?

Orgdoctor
 
It sounds to me like the you did not name the form as "MyCtrlForm". You should be able to use Intelisense to see the form name in a droplist when you declare the "m_frmCallBack As" statement. Basically check the name of the form that you added to the ActiveX control. Does that make sense? [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Foada,

Despite ensuring the accuracy of my spelling, for some reason when I used the Intellisense dropdown to insert "MyCtrlfrm" in the As clause of the "Private WithEvents" statement, it WORKED! How can I thank you enough? You are one hell of a resource and a great asset to the programming community. Please contact me at
jkane@alliant.edu. I may have some paid work opportunities for you in the not too distant future.

Orgdoctor
 
Along these same lines. I have my ActiveX control initialized in the web browser and display my forms and take input form the user.

Is there a way when i close the form i opened to close the web browser window which initialized my control also?

My users are really annoyed at having the initialize browser window hanging around after they finished.

(The more ASP.NET I do, the more Javascript I find myself using)
 
Let me understand what exactly you are trying to do. You run the control from within the browser, which is typically done by opening another browser window which has the <object>...</object> tags containing the .ocx control
specification. Now, are you asking how you can close the
the browser window from which you initially launched the
window containing the ActiveX control? If so, here are the asp commands that write a set of VBScript commands to do what you want (which you can easily convert to javascript, if that's your preference):

Response.Write "<SCRIPT LANGUAGE = " & Chr(34) &
"VBScript"& Chr(34) & ">" & vbCrLF
Response.Write "NewWin = Window.Open( .... "
Response.Write "Window.Opener = Window" & VbCrLf
Response.Write "Window.Close" & vbCrLF
Response.Write "</SCRIPT>" & vbCrLF

I hope this helps. Contact me if you need clarification or if I misunderstood what you're trying to do.

Orgdoctor
 
This works fine, and is really quite simple:

Create a user control. Add a label, label1. Add a command button, command1. Add a form to the project, form1. On form1, add a command button.

In Form1's general declarations, add:
Public x as Control
Of course, it's more robust to set this up as a property procedure pair.

In the usercontrol_initialize event handler, add:
Set Form1.x = Label1

In the Command1_click event handler, add:
Form1.show

In Form1's command button click event handler, add:
x.BackColor = vbRed
x.Caption = "Oh, Boy, it works!"
Me.Hide

Repeat for each control that you want to have the form see. That's the bare bones of the technique, you should be able to put your requirements together from there.

HTH

Bob


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top