?
Solved

Global Event for UserControl

Posted on 2003-03-24
12
Medium Priority
?
253 Views
Last Modified: 2013-11-25
How can I get many instances of a UserControl to activate the same 'event'. I want the one procedure to be executed whenever the user makes a change to any of many instances of my UserControl. At the moment, I am having my UserControl execute a Global procedure in a Module, however this causes an annoying error - the instances of the UserControl get hatched out when I add to the form in design mode, or during some run-time errors.
0
Comment
Question by:robertlees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 2

Expert Comment

by:stretch71
ID: 8198508
I'm not quite sure if this helps, however if all the instances of your UserControl are in a control array then you can have one common event.  The event declaration then contains an index value to indicate which specific control fired the event.
0
 
LVL 1

Author Comment

by:robertlees
ID: 8198632
Thanks, stretch71, but the instances aren't in a control array.
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8199705
a simply way is call global procedure only when Ambient.UserMode = True
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:robertlees
ID: 8200203
Thanks, TigerZhao, but that didn't fix it.

I tried the following in my UserControl...

Private Sub txt_Change()
   If Ambient.UserMode Then
      Call FieldHasChanged
   End If
End Sub

...where FieldHasChanged is defined in a module as a Global Sub.

However this still doesn't fix the problem. If I add or remove something from the form, all instances of my UserControl get hashed out.
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8201538
pass you control as parameter
    Call FieldHasChanged(txt)
0
 
LVL 1

Author Comment

by:robertlees
ID: 8206277
...but the problem remains - if I call a Global procedure from a UserControl, then I get this hatching symptom. If I comment out the call to FieldHasChanged, then the problem goes away.

What I think I need is some ability to raise an event which is responded to by the container of the instance of my control, rather than by the instance itself.

I wonder if I could use the SendMessage API to generate a message to the container of the instance. Is there an Ambient property that will give me the handle of the container, to which I would send the message. A minor problem is that some of these instances are in Frames, so the Frame rather than the form would be the container.
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8207495
I did not understand what's "hatching symptom", but if you want create a event for usercontrol, code like this:

'in user control
Public Event FieldHasChanged()

Private Sub txt_Change()
  If Ambient.UserMode Then
     RaiseEvent FieldHasChanged()
  End If
End Sub

'in Form
Private Sub UserControl11_FieldHasChanged()
    'do something you want
End Sub
0
 
LVL 1

Author Comment

by:robertlees
ID: 8207727
Thanks for your interest TigerZhao.

To restate the problem, I have a UserControl that encapsulates a number of functions that I like to have for all textboxes. For example, that the text becomes selected when it gets the focus, and that keypresses might be restricted according to the field type (ie, date or currency, etc).

Another thing I want to be able to do is enable the SAVE button on the form if any of the textboxes is changed.

Using the RaiseEvent method, I can activate an event in a particular instance of my control, but that would mean putting cmdSave.Enabled = True into this event of every instance of my control.

I want to be able to code this just once, and have every instance of the control utilise it.

The way I have done this so far is to put it as a Global procedure in a module, and the change event in the UserControl calls it.

This works, except that in design-mode, if I change anything (for example I might add or remove a label control), then every instance of my UserControl gets hatched - ie, oblique lines are drawn across the instance. I know it is due to my referencing the Global Procedure because if I comment out this call, the problem goes away.
0
 
LVL 4

Accepted Solution

by:
TigerZhao earned 1000 total points
ID: 8214793
use callback is fit you needs.
even if you put usercontrol on a frame control, it still works.

'== Class IForm ==
Option Explicit

Public Sub FieldHasChanged()
End Sub

'== UserControl ==
Option Explicit
Private m_IForm As IForm

Private Sub Text1_Change()
    If Not m_IForm Is Nothing Then
        Call m_IForm.FieldHasChanged
    End If
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    If Ambient.UserMode Then
        Set m_IForm = Extender.Parent
    End If
End Sub

'== Form ==
Option Explicit
Implements IForm

Private Sub cmdSave_Click()
    'do save
    cmdSave.Enabled = False
End Sub

Private Sub IForm_FieldHasChanged()
    cmdSave.Enabled = True
End Sub
0
 
LVL 1

Author Comment

by:robertlees
ID: 8220539
That is brilliant, TigerZhao. Thank you very much.
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8220787
TigerZhao, good job... I think that is called polymorphism.
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8242954
just about skill, no originality.
study by study...
work by work...
experience by experience...
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question