|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Content of the ZIP fileThe ZIP file contains a demonstration application (source code and binary) which was created with Visual Studio 2003 as well as an additional copy of the file CtrlActivator.vb which contains the complete source code of the IntroductionI recently read a post by a fellow programmer who wanted to have a disabled First, I tried to generalize the problem a little bit. What the asker wanted to do was to have a disabled control on a container that should be activated on a double click. And that's what my How to use it: A simple exampleJust add the file CtrlActivator.vb to your project. Now, let's assume you have a form Public Class MainForm
Inherits System.Windows.Forms.Form
Private TextboxActivator As CtrlActivator = New CtrlActivator(Me)
[Designer generated code]
End Class
To let the TextboxActivator.RegisterControl(DisabledButton)
That's all. When you execute the code, you can enable the TextboxActivator.UnRegisterControl(DisabledButton)
There is just one thing you should keep in mind: if you continue reading, you will find out that the How to use it (public members): Sub New(ByVal Parent As Control)The constructor of the class has one parameter which is used to pass a reference to the parent of the controls you want to enable. If you want to handle multiple parent controls, you need to create one instance of the Sub New(ByVal Parent As Control)
How to use it (public members): Public Sub RegisterControl(ByVal Ctrl As Control)
Public Sub RegisterControl(ByVal Ctrl As Control)
How to use it (public members): Public Sub UnRegisterControl(ByVal Ctrl As Object)
Public Sub UnRegisterControl(ByVal Ctrl As Object)
How to use it (public members): Public Function Exists(ByVal Ctrl As Control) As Boolean
Public Function Exists(ByVal Ctrl As Control) As Boolean
How to use it (public members): Public Overloads Sub Dispose()
Public Overloads Sub Dispose() Implements IDisposable.Dispose
How to use it (public members): Public ReadOnly Property Count() As IntegerThe property Public ReadOnly Property Count() As Integer
How it works: Global declarationsThe class implements the Implements IDisposable
Private p_CallingCtrl As Control = Nothing
Private p_CtrlCollection As Collection = New Collection
Private p_MousePos As Point = New Point(-1, -1)
Private p_Disposed As Boolean = False
How it works: Sub New(ByVal Parent As Control)The constructor is responsible for copying a parent control reference to Sub New(ByVal Parent As Control)
If Parent Is Nothing Then
Throw New NullReferenceException
End If
p_CallingCtrl = Parent
AddHandler p_CallingCtrl.DoubleClick, AddressOf p_CallingCtrl_DoubleClick
AddHandler p_CallingCtrl.MouseMove, AddressOf p_CallingCtrl_MouseMove
End Sub
How it works: Public Sub RegisterControl(ByVal Ctrl As Control)
Public Sub RegisterControl(ByVal Ctrl As Control)
If Me.p_Disposed Then
Throw New ObjectDisposedException(Me.ToString())
End If
If Not (Ctrl Is Nothing) Then
If Not Exists(Ctrl) Then
p_CtrlCollection.Add(Ctrl)
End If
End If
End Sub
How it works: Public Sub UnRegisterControl(ByVal Ctrl As Object)
Public Sub UnRegisterControl(ByVal Ctrl As Object)
Dim Counter As Integer
If Me.p_Disposed Then
Throw New ObjectDisposedException(Me.ToString())
End If
For Counter = 1 To p_CtrlCollection.Count
If p_CtrlCollection.Item(Counter) Is Ctrl Then
p_CtrlCollection.Remove(Counter)
Exit For
End If
Next Counter
End Sub
How it works: Public ReadOnly Property Count() As IntegerAfter ensuring that Public ReadOnly Property Count() As Integer
Get
If Me.p_Disposed Then
Throw New ObjectDisposedException(Me.ToString())
End If
Return p_CtrlCollection.Count
End Get
End Property
How it works: The Deconstructor LogicThe methods Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not (Me.p_Disposed) Then
If disposing Then
RemoveHandler p_CallingCtrl.DoubleClick, AddressOf p_CallingCtrl_DoubleClick
RemoveHandler p_CallingCtrl.MouseMove, AddressOf p_CallingCtrl_MouseMove
p_CtrlCollection = Nothing
p_CallingCtrl = Nothing
End If
Me.p_Disposed = True
End If
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
How it works: Private Sub p_CallingCtrl_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)This event handler is triggered when the Private Sub p_CallingCtrl_DoubleClick(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim Counter As Integer
Dim Ctrl As Control = Nothing
For Counter = 1 To p_CtrlCollection.Count
Ctrl = CType(p_CtrlCollection.Item(Counter), Control)
If Ctrl.Bounds.Contains(p_MousePos) Then
Ctrl.Enabled = True
End If
Next Counter
End Sub
How it works: Private Sub p_CallingCtrl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)This event handler is triggered when the Private Sub p_CallingCtrl_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
p_MousePos.X = e.X
p_MousePos.Y = e.Y
End Sub
How it works: Public Function Exists(ByVal Ctrl As Control) As BooleanLast but not least, there is Public Function Exists(ByVal Ctrl As Control) As Boolean
Dim Counter As Integer
If Me.p_Disposed Then
Throw New ObjectDisposedException(Me.ToString())
End If
Exists = False
For Counter = 1 To p_CtrlCollection.Count
If p_CtrlCollection.Item(Counter) Is Ctrl Then
Exists = True
Exit For
End If
Next Counter
End Function
|
|||||||||||||||||||||||||||||||||||