Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Global Event for UserControl

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
robertlees
Asked:
robertlees
1 Solution
 
stretch71Commented:
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
 
robertleesAuthor Commented:
Thanks, stretch71, but the instances aren't in a control array.
0
 
TigerZhaoCommented:
a simply way is call global procedure only when Ambient.UserMode = True
0
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!

 
robertleesAuthor Commented:
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
 
TigerZhaoCommented:
pass you control as parameter
    Call FieldHasChanged(txt)
0
 
robertleesAuthor Commented:
...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
 
TigerZhaoCommented:
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
 
robertleesAuthor Commented:
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
 
TigerZhaoCommented:
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
 
robertleesAuthor Commented:
That is brilliant, TigerZhao. Thank you very much.
0
 
aeklundCommented:
TigerZhao, good job... I think that is called polymorphism.
0
 
TigerZhaoCommented:
just about skill, no originality.
study by study...
work by work...
experience by experience...
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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