Windows.Form: Event on active control change

  • Thread starter Thread starter Volker Jobst
  • Start date Start date
V

Volker Jobst

Hi,

Is there an event of windows.forms.form which informs me that the active
control will be changed before it will be changed? Something like:

Private Sub Form_Validating(ByVal sender As Object, ByVal e As ...) Handles
Form."EVENT HERE"
dim Ok as Boolean = False
...
If Not (ok = True) Then
e.Cancel = True
End If
End Sub

I don't want to add an event handler for each control on the form, because
there is one base class which should be able to validate the user input
independently by special criteria not mentioned here.

thanks volker jobst
 
Volker Jobst said:
Hi,

Is there an event of windows.forms.form which informs me that the
active control will be changed before it will be changed? Something
like:

Private Sub Form_Validating(ByVal sender As Object, ByVal e As ...)
Handles Form."EVENT HERE"
dim Ok as Boolean = False
...
If Not (ok = True) Then
e.Cancel = True
End If
End Sub

I don't want to add an event handler for each control on the form,
because there is one base class which should be able to validate the
user input independently by special criteria not mentioned here.


You can use the same event handler for all controls. You could add them in a
loop, also in a base class.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi Volker,

I thought not, however when you use this sample code you can create them. I
use the lost focus but you can take whatever you like. (look to the right
events then)

I hope this helps?

Cor
\\\
Dim last As String
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
last = DirectCast(sender, Control).Name
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).Text = last
End Sub

///
 
* "Volker Jobst said:
Is there an event of windows.forms.form which informs me that the active
control will be changed before it will be changed? Something like:

Private Sub Form_Validating(ByVal sender As Object, ByVal e As ...) Handles
Form."EVENT HERE"
dim Ok as Boolean = False
...
If Not (ok = True) Then
e.Cancel = True
End If
End Sub

I don't want to add an event handler for each control on the form, because
there is one base class which should be able to validate the user input
independently by special criteria not mentioned here.

I didn't test it, but try to listen in the form's 'WndProc' for
'WM_KILLFOCUS'. The 'wParam' parameter will store the handle of the
control that receives focus, or 'NULL'.
 
Cor Ligthert said:
Hi Volker,

I thought not, however when you use this sample code you can create them. I
use the lost focus but you can take whatever you like. (look to the right
events then)

I hope this helps?

Cor
\\\
Dim last As String
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
last = DirectCast(sender, Control).Name
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).Text = last
End Sub

///

Thank you, this solution works fine!!
 
Back
Top