|
-
Aug 27th, 2015, 06:06 AM
#1
Thread Starter
Addicted Member
[RESOLVED] UserControl with form
Hello,
I have a user control that consisting of UserControl, and one Form.
When UserControl Initialize it opens that Form.
In that form I would like to call an event that is fired from user control.
Here is a example:
Code:
'Code In UserControl1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Public Event FormEvent(ByVal FormEventName As String)
Public Sub OnFormEvent(ByVal localFormEventName As String)
RaiseEvent FormEvent(localFormEventName)
End Sub
Private Sub UserControl_Initialize()
Form1.Show
End Sub
Now when the usercontrol opens the Form1 in Form1 load event I want to call a public sub inside the usercontrol:
Code:
'Code In Form1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private Sub Form_Load()
' Call the sub inside UserControl1
End Sub
Is anyone having some ideas?
Here is a simple example.
UserControl_with_Form.zip
My projects:
Virtual Forms
VBA Telemetry
-
Aug 27th, 2015, 06:47 AM
#2
Re: UserControl with form
Both your UserControl and your Form are just objects (instances of a particular class) that you can see.
If you want to communicate from one object to another, then one needs a reference to the other.
Code:
[Form1.frm]
Private form1Instance as Form1
Private Sub UserControl_Initialize()
' Create a new instance of Form1 ...
Set form1Instance = new Form1
' ... Tell that instance that we're "hosting" it ...
Set form1Instance.Host = Me ' or, possibly, UserControl; memory's a bit vague
' ... and then show it.
form1Instance.Show
End Sub
[UserControl1.uc]
Private hostUC as UserControl1
Public Property Set Host( host as UserControl1 )
Set hostUC = host
End Property
Private Sub RaiseFormEvent_123()
hostUC.OnFormEvent "123"
End Sub
Regards, Phill W.
-
Aug 27th, 2015, 07:17 AM
#3
Re: UserControl with form
Hmmmmm.... this just sounds like a bad idea... I'm wondering what's the goal here... why does the form need to call a sub in the UC? The form should be isolated enough to stand on its own. Phill's solution works, but just feels wrong to me.
-tg
-
Aug 27th, 2015, 09:56 AM
#4
Thread Starter
Addicted Member
Re: UserControl with form
I'm having some old application written in VB6.
This app is having some forms.
Now I'm trying to convert it to ActiveX ocx so that I can use it in VB.NET
The goal is to place this ocx on a form in .net and when a user for example clicks on this form in ocx a button I want to trigger an event and send result from this ocx to .net in an event.
My projects:
Virtual Forms
VBA Telemetry
-
Aug 27th, 2015, 10:06 AM
#5
Re: UserControl with form
I'm still not sure I followed... but if you're writing someting to use in .NET with the intent of replacing it with .NET code anyways... why not just write it in .NET? by bother writing some new VB6 OCX control from scratch that's just going to be used by a .NET application.
Personally I think you're setting up for a world of hurt by going this route. Even still, even if this component was going to be used by VB6, I still don't see why the form needs the UC it was spawned from any way. The UC should create an instance of the form, load it, pass it any data it needs and then shows it. The form should simply consume the data passed to it... if it needs to pass databasck, it should close with a value of 0 (or some kind of success value) and then let the UC determine that success/failure and then get the data back from the form... the form shouldn't care if it was created from a UC, or another form or what ever. It should simply accept data for input, and package data for output.
-tg
-
Aug 27th, 2015, 10:20 AM
#6
Thread Starter
Addicted Member
Re: UserControl with form
The app in vb6 is having approximately half a million lines of code. It works well as an ocx. But can't comunicate with the parant application.
So I was thinking to communicate through events .OCX <> .NET
My projects:
Virtual Forms
VBA Telemetry
-
Aug 27th, 2015, 03:10 PM
#7
Thread Starter
Addicted Member
Re: UserControl with form
Or to rephrase the question. How to raise an event outside the user control. For example inside OCX, but from module, form or class?
My projects:
Virtual Forms
VBA Telemetry
-
Aug 27th, 2015, 04:18 PM
#8
Re: UserControl with form
If you will only have one OCX sited on the parent project, couldn't you just create a public variable in your module: g_theUserControl declared as your UC type? Then within the UC's initialize event: Set g_theUserControl = Me. Now anything in your ocx project can call any public or friend methods on the UC by referencing the public g_theUserControl variable
If you can have multiple instances of the ocx loaded in the same parent project, design a method where your forms, classes, usercontrols can talk to each other. Friend declared methods are available within your ocx like public methods are. But Friend declarations are not visible outside the compiled ocx.
Million lines of code? There are numerous issues to be addressed when auto-convert from VB6 to .Net and you can find several related articles on MSDN
-
Aug 28th, 2015, 05:46 AM
#9
Re: UserControl with form
@Davor Geci
You have to think if you like a solution for one usercontrol or many, and if the form is common for all users controls or private for each (let say that is a memo form, so every user control open a memo until you choose to close).
The easiest way is to inform form when loading, calling a public sub on it with the name of usercontrol in the main form.
***Using any form we can know the main form, where the user controls exist. So the name is the key to find the right control and to interact with it. Events can raised in a subroutine that we can call by outside...I think you have to use ambient object to find the parent and to open form just front of the main form...
look the one only usercontrol in M2000 interpreter code (one control with many classes to make it behavior as text box, edit box, list box etc), the code is in sign..
Last edited by georgekar; Aug 28th, 2015 at 07:51 AM.
Reason: addition
-
Aug 28th, 2015, 11:20 AM
#10
Thread Starter
Addicted Member
Re: UserControl with form
Thank you people for helping me out.
But I still can't figure it out.
I have attache a Group witch is having 2 pojects.
1. the ActiveX (ocx) control
2. the test project with one form and command button
When I click on a button,
1. it should open a form in OCX (this is ok)
2. in form load (in OCX) it calls a sub in a module (in OCX) and pass a Text "This is a test." (this is ok)
3. in module a class is initilize and a Friend Sub inside this class is called and Text is passed (this is ok)
4. in the class the Text is recived, and a event is fired (Hm, ok or not)
5. in UserControl we have WithEvents which should recive this event fired from class, but it never get fired
The Project Group:
UserControl_with_Form.zip
My projects:
Virtual Forms
VBA Telemetry
-
Aug 28th, 2015, 11:53 AM
#11
Re: UserControl with form
Your UC is declaring a clsAX instance as m_clsax
Your form is calling the ClassSomeEvent on a different instance from modAX
So, the instance in modAX is raising event, but no objects have that instance referenced; hence the raised event is falling on deaf ears. You need to call ClassSomeEvent on the m_clsax instance to have it received in m_clsax
A quick solution is rather simple but may not be appropriate in your case:
In the module, remove your sub & replace with this: Public clsTest As clsAX
In your UC, during it's initialize event:
If clsTest Is Nothing Then Set clsTest = New clsAX
Set m_clsax = clsTest You should release it during the Terminate event: Set m_clsax = Nothing
In your form change your call to: modAX.clsTest "This is a test."
How now? The problem there is that if you have more than 1 UC loaded on the form, every one of them will get the event.
-
Aug 28th, 2015, 12:30 PM
#12
Thread Starter
Addicted Member
Re: UserControl with form
LaVolpe this a great!!!
Now is the full circle closed.
Hehe, but with some side effects.
This is a good new start for a new challenge.
How to identify the caller.
For others, I've attached the Group project in the current state.
UserControl_with_Form_1.zip
My projects:
Virtual Forms
VBA Telemetry
-
Aug 28th, 2015, 12:48 PM
#13
Re: UserControl with form
Identifying the caller? You can pass the caller as a parameter in the event.
Identifying the callee? That's a bit more complex but depends on one's needs. Call any of the loaded UCs or call just the UC that loaded the form, or some other scenario
-
Aug 28th, 2015, 01:50 PM
#14
Thread Starter
Addicted Member
Re: UserControl with form
In some situations, this behavior would be the solution. But as you said, in this case it is not. Here, the control that triggered the event, only she should receive the response.
My projects:
Virtual Forms
VBA Telemetry
-
Aug 28th, 2015, 02:01 PM
#15
Re: UserControl with form
Then the solution was discussed earlier.
1. in the form declare a variable of your usercontrol, i.e., Dim m_Owner As UserControlXYZ
2. Create a Friend method on the form that receives a UC instance, i.e.,
Friend Set Owner(theOwner As UserControlXYZ)
3. Cache the passed UC instance, i.e., Set m_Owner = theOwner
4. Within the UC, when loading the form...
a. Pass the form the instance of itself, i.e., Set myForm.Owner = Me
b. Show the form as needed
5. The form should release the instance of m_Owner in its unload or terminate event
Now the form can call any public or friend methods on the usercontrol, i.e., m_Owner.[method name]
Note: Should verify your project closes normally when the above is done. A circular reference may have been created by the form and UC if they have hard references to each other. If the project does not completely unload when closed normally, then a bit of creativity is needed to avoid the circular references
Edited: Oops. Para 2 should've read: Friend Property Set Owner
Last edited by LaVolpe; Aug 28th, 2015 at 02:36 PM.
-
Aug 29th, 2015, 02:42 AM
#16
Thread Starter
Addicted Member
Re: UserControl with form
This is the solution.
It works!
Thnank you people.
My projects:
Virtual Forms
VBA Telemetry
-
Jul 20th, 2018, 02:32 PM
#17
New Member
Re: [RESOLVED] UserControl with form
Hi Davor,
I have the same problem with you. I tried to apply what says LaVolpe in his last post but my problem is that i don't be able to execute metheds in the usercontrol. I try to run the methods from le parent window and i try to run from a class that is in the same ocx of the usercontrol. But in both cases the message is: Property or method not supported by the object. It seems that it is not possible to run methods (sub or function) sites in the usercontrol. Can you help me, please? Can you explain how did you resolve the problem?
Thanks a lot.
-
Jul 20th, 2018, 02:41 PM
#18
Thread Starter
Addicted Member
Re: [RESOLVED] UserControl with form
Hello Rudy_Soft,
this is a 3 years old post. Maybe you should try with Public and not Friend.
Davor
My projects:
Virtual Forms
VBA Telemetry
-
Jul 20th, 2018, 02:43 PM
#19
Thread Starter
Addicted Member
Re: [RESOLVED] UserControl with form
Hello Rudy_Soft,
this is a 3 years old post. Maybe you should try with Public and not Friend.
Davor
My projects:
Virtual Forms
VBA Telemetry
-
Jul 20th, 2018, 04:14 PM
#20
Re: [RESOLVED] UserControl with form
 Originally Posted by Davor Geci
Maybe you should try with Public and not Friend.
Yes, or in the Form's code declare the reference to the UserControl not "As Object" but "As TheUserControlType".
-
Jul 23rd, 2018, 04:07 PM
#21
New Member
Re: [RESOLVED] UserControl with form
Thanks to Davor and Eduardo for replying to me.
Davor: I just used Public insted of Friend.
Eduardo: what do you mean with TheUserControlType ?
I post my code.
In the main Exe I have a blank form:
Private WithEvents objCtrExt As VBControlExtender
Private m_Owner As Object
'Private m_Owner As MyControl
Private Sub Form_Load()
Set objCtrExt = Controls.Add("MyOcx.MyControl", "MyControl")
objCtrExt.Visible = True
objCtrExt.Tag = Me.Tag
End Sub
'Public Property Let Owner(objCtrl As MyControl)
Public Property Let Owner(objCtrl As Object)
Set m_Owner = objCtrl
End Property
Private Sub Cmd_Click(Index As Integer)
Select Case Index
Case 1 ' Go to previous record
objCtrExt.PrevRec
Case 2 ' Go to next record
m_Owner.NextRec
End Select
End Sub
-------------------------------------------------------------------------
In the UserControl (MyControl in MyOcx) I have:
Private FrmParent As Object
Private Sub UserControl_Paint()
Set FrmParent = Extender.Parent
FrmParent.Owner = UserControl.Extender
End Sub
Public Sub PrevRec()
PrevRec_UC
End Sub
Public Sub NextRec()
NextRec_UC
End Sub
When I click in some buttons of the form (in Exe) the Cmd_Click runs the Sub in the OCX (PrevRec and NextRec) but I have the same message: Property or method not supported by the object
If I use MyControl insted of Object in the PropertyLet of the form I get the error: Type not corresponding in the line:
FrmParent.Owner = UserControl.Extender of the OCX.
Can you give me some help, please? Many thanks.
-
Jul 23rd, 2018, 04:55 PM
#22
Re: [RESOLVED] UserControl with form
 Originally Posted by Rudy_Soft
Eduardo: what do you mean with TheUserControlType ?
I mean 'As MyControl'.
Something like this...
Code:
Private WithEvents objCtrExt As VBControlExtender
Private m_Owner As MyControl
Private Sub Form_Load()
Set objCtrExt = Controls.Add("MyOcx.MyControl", "MyControl")
Set m_Owner = objCtrExt
objCtrExt.Visible = True
objCtrExt.Tag = Me.Tag
End Sub
Private Sub Cmd_Click(Index As Integer)
Select Case Index
Case 1 ' Go to previous record
objCtrExt.PrevRec
Case 2 ' Go to next record
m_Owner.NextRec
End Select
End Sub
-------------------------------------------------------------------------
In the UserControl (MyControl in MyOcx) I have:
Code:
Friend Sub PrevRec()
PrevRec_UC
End Sub
Friend Sub NextRec()
NextRec_UC
End Sub
-
Jul 27th, 2018, 10:01 AM
#23
New Member
Re: [RESOLVED] UserControl with form
Hi Eduardo
I have the solution:
In the main Exe I have a blank form:
Private WithEvents objCtrExt As VBControlExtender
Private Sub Form_Load()
Set objCtrExt = Controls.Add("MyOcx.MyControl", "MyControl")
objCtrExt.Visible = True
objCtrExt.Tag = Me.Tag
End Sub
Private Sub Cmd_Click(Index As Integer)
Select Case Index
Case 1 ' Go to previous record
objCtrExt.object.PrevRec
Case 2 ' Go to next record
objCtrExt.object.NextRec
End Select
End Sub
-------------------------------------------------------------------------
In the UserControl (MyControl in MyOcx) I have:
Public Sub PrevRec()
PrevRec_UC
End Sub
Public Sub NextRec()
NextRec_UC
End Sub
I resolved the problem using the keyword object: objCtrExt.object.PrevRec
In this way from the form in the Exe is possible to call the sub in the OCX. Now it works very well.
Anyway thank you for your interest and for your help.
Bye.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|