468,512 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Windows.Form: Event on active control change

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
Nov 20 '05 #1
4 7249
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

///
Nov 20 '05 #2
"Volker Jobst" <bu****@gmx.de> schrieb
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

Nov 20 '05 #3
* "Volker Jobst" <bu****@gmx.de> scripsit:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uO**************@TK2MSFTNGP12.phx.gbl...
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!!
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dave Wurtz | last post: by
6 posts views Thread by Fred Morrison | last post: by
3 posts views Thread by Jason Huang | last post: by
reply views Thread by hazz | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.