Monday, March 12, 2012 11:08 PM
Hi all of you,
My project has around thirty winforms. I am thinking how can I avoid to write for every winform the following three subs (I think such code is very auto-descriptive):
Is it possible to define form event handlers in a class?
Public Sub Definir_focos()
For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
AddHandler DirectCast(c, TextBox).GotFocus, AddressOf conFoco
AddHandler DirectCast(c, TextBox).LostFocus, AddressOf sinFoco
Select Case c.HasChildren
For Each c2 As Control In c.Controls
If c2.GetType Is GetType(TextBox) Then
AddHandler DirectCast(c2, TextBox).GotFocus, AddressOf conFoco
AddHandler DirectCast(c2, TextBox).LostFocus, AddressOf sinFoco
Private Sub conFoco(ByVal sender As Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).BackColor = Color.Yellow
Private Sub sinFoco(ByVal sender As Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).BackColor = Color.White
How could I encapsulate that logic in a class?
Thanks for your help,
- Edited by Enric Vives Monday, March 12, 2012 11:26 PM
Tuesday, March 13, 2012 12:12 AM
extend the Form-class. Derive from Form, add your methods, use this custom Form as Forms in your project.
Or add these methods to your mainform, make them public or internal, pass a reference to "other"-forms/controls as parameter to the methods -or make the methods static...
Tuesday, March 13, 2012 1:01 PM
Hi just a silly question.. or not
Following your advices everything is fine but.. could I avoid InitializeComponent method in that class?
Tuesday, March 13, 2012 2:04 PM
I mean, everytime I delete these lines, then the compiler creates them again:
That means that Form class implement an inteface which has two compulsory methods (initializecomponent and Load??
Private Sub FormularioEventos_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadTIA
Private Sub InitializeComponent()
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "FormularioEventos"
Thursday, March 15, 2012 5:30 AMModeratorHi Enric,
The InitializeComponent method has a comment preceding it, warning you not to modify the code. By modifying the code in the InitializeComponent method, you can no longer use the Forms Designer. If you do, your modifications will be lost. You should only perform these optimizations as the final stage of development, after all the design work has been done.
If you edit anything in the designer, the InitializeComponent method will be created again.
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Thursday, March 15, 2012 1:08 PM
Rather than putting this at form level, you could subclass the standard controls themselves - TextBox, ComboBox etc. - and override this behaviour there:
Then just use TextBox2 controls everywhere you would have used TextBox ones and the colouration is done automagically.
Class TextBox2 Inherits TextBox
Public Sub New() MyBase.New() End Sub Protected Overrides Sub OnLeave( _ ByVal sender as Object _ , ByVal e as EventArgs ) Me.BackColor = Color.White End Sub Protected Overrides Sub OnEnter( _ ByVal sender as Object _ , ByVal e as EventArgs ) Me.BackColor = Color.Yellow End Sub End Class
Regards, Phill W.
- Marked As Answer by Enric Vives Thursday, March 15, 2012 4:24 PM
Thursday, March 15, 2012 4:25 PMThanks Pill