Who said it couldn't be done... Oh yeah, everyone...
I dealt with a textbox here, but its possible to do wth with any control
Drop this code onto a custom control... Add nothing
else to the control...
'-- Begin Control Code --
Option Explicit
Private WithEvents MyTextBox As TextBox
Private pbVisible As Boolean
Private psTop As Single
Private psLeft As Single
Private psWidth As Single
Private psHeight As Single
Private WithEvents zzzTextBoxEvents As TextBox
Public Event Change()
Public Event Click()
Public Event DblClick()
Private Sub MyTextBox_Change()
SetParentControl
RaiseEvent Change
End Sub
Private Sub MyTextBox_DblClick()
SetParentControl
RaiseEvent Change
End Sub
Private Sub UserControl_Initialize()
UserControl.BackColor = vbRed
Set MyTextBox = Controls.Add("VB.TextBox", "MyTextBox")
MyTextBox.Visible = True
End Sub
Private Sub UserControl_Resize()
MyTextBox.Move 20, 20, Extender.Width - 40, Extender.Height - 40
SetParentControl
End Sub
Private Sub MyTextBox_Click()
'MyTextBox.SelStart = 0
'MyTextBox.SelLength = Len(MyTextBox.Text)
SetParentControl
RaiseEvent Click
End Sub
Public Property Get Visible() As Boolean
Visible = pbVisible
End Property
Public Property Let Visible(ByVal vNewValue As Boolean)
pbVisible = vNewValue
Extender.Visible = vNewValue
SetParentControl
End Property
Public Sub Move(xLeft, xTop, xWidth, xHeight)
psLeft = xLeft
psTop = xTop
psWidth = xWidth
psHeight = xHeight
Extender.Move xLeft, xTop, xWidth, xHeight
SetParentControl
End Sub
Public Property Get Top() As Variant
Top = psTop
End Property
Public Property Let Top(ByVal vNewValue As Variant)
psTop = vNewValue
Extender.Top = vNewValue
SetParentControl
End Property
Public Property Get Left() As Variant
Left = psLeft
End Property
Public Property Let Left(ByVal vNewValue As Variant)
psLeft = vNewValue
Extender.Top = vNewValue
SetParentControl
End Property
Public Property Get Width() As Variant
Width = psWidth
End Property
Public Property Let Width(ByVal vNewValue As Variant)
psWidth = vNewValue
Extender.Top = vNewValue
SetParentControl
End Property
Public Property Get Height() As Variant
Height = psHeight
End Property
Public Property Let Height(ByVal vNewValue As Variant)
psHeight = vNewValue
Extender.Top = vNewValue
SetParentControl
End Property
Private Sub SetParentControl()
On Error Resume Next
Set Extender.Parent.xTextBox = Me
End Sub
Public Property Get Index() As Variant
On Error GoTo errorSpot
Index = Extender.Index
Exit Property
errorSpot:
Index = vbEmpty
End Property
Public Property Get Name() As String
Name = Extender.Name
End Property
'-- End Control Code --
Then Drop this code into a form...
...and drop as many controls as you need to,
creating a control array as you do this...
Oh, yeah, add some that are not in the control
array as well, just to prove that this handles
every control of this type on the form reguardless
of control array or not...
Run the code and watch the debug window...
'-- Begin Form Code --
Public WithEvents xTextBox As ctlTextBox
Private Sub xTextBox_Change()
Debug.Print xTextBox.Name,
Debug.Print xTextBox.Index,
Debug.Print "Change"
End Sub
Private Sub xTextBox_Click()
Debug.Print xTextBox.Name,
Debug.Print xTextBox.Index,
Debug.Print "Click"
End Sub
Private Sub xTextBox_DblClick()
Debug.Print xTextBox.Name,
Debug.Print xTextBox.Index,
Debug.Print "DblClick"
End Sub
'-- Begin Form Code --
Thats all there is to using it.
You'll want to continue to fill out the controls
events and properties, but this code is a good
model to start with.
D.
Quote:
> The best I could come up with is simply using a control array. That
> would mean you name all your datafield controls with the same name,
> which will cause VB to call one set of event handlers for all the fields.
> (VB passes in an Index value to the handlers to indicate which one fired
the
> event)
> In that scenareo, you can use Select Case in the change event to perform
> field specific duties and call your sub (outside of the Select Case
structure)
> just before leaving the change event.
> I'd have to think hard to find a good reason for not using control arrays
> in every program, for every type of control you have more than one of....
> HTH
> LFS
> What is the KB?:
http://support.microsoft.com/default.aspx?ln=EN-US&pr=kbinfo&
Quote:
> RTFM?
http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbstart...
m
Quote:
> Where's the archive?:
http://groups.google.com/advanced_group_search?q=group:microsoft.publ...
&hl=en&safe=off&num=50