Click here to Skip to main content
16,020,622 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 8:26
Andraw Tang17-Aug-10 8:26 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Eddy Vluggen17-Aug-10 9:02
professionalEddy Vluggen17-Aug-10 9:02 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 9:17
Andraw Tang17-Aug-10 9:17 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Eddy Vluggen17-Aug-10 9:38
professionalEddy Vluggen17-Aug-10 9:38 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 9:55
Andraw Tang17-Aug-10 9:55 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Eddy Vluggen17-Aug-10 10:20
professionalEddy Vluggen17-Aug-10 10:20 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 10:29
Andraw Tang17-Aug-10 10:29 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Eddy Vluggen17-Aug-10 11:00
professionalEddy Vluggen17-Aug-10 11:00 
Andraw Tang wrote:
Normally how you handle this case of case?


I'd go for the Validating event, with code similar to below;
VB.NET
  1  Public Partial Class MainForm
  2  	Inherits Form
  3  	Private t As New TextBox()
  4  	Private t2 As New TextBox()
  5  
  6  	Public Sub New()
  7  		' t2 will be disabled if t fails to validate
  8  		t2.Parent = Me
  9  		t2.Location = New Point(0, 0)
 10  
 11  		' this is the control that we're validating
 12  		t.Parent = Me
 13  		t.Location = New Point(0, 0 + t2.Height)
 14  
 15  		Controls.AddRange(New Control() {t, t2})
 16  
 17  		' Hook up any control that doesn't allow zero's;
 18  		AddHandler t.Validating, New CancelEventHandler(AddressOf t_Validating)
 19  	End Sub
 20  
 21  	Private Sub t_Validating(sender As Object, e As CancelEventArgs)
 22  		' identify what control raised the event
 23  		Dim senderAsTextBox As TextBox = TryCast(sender, TextBox)
 24  
 25  		' verify that the content is valid
 26  		Dim IsContentValid As Boolean = senderAsTextBox.Text.Contains("-")
 27  
 28  		' if it ain't, cancel the moving of the focus;
 29  		e.Cancel = IsContentValid
 30  
 31  		' your custom action goes here, we disable t2 if 
 32  		' the content isn't valid
 33  		t2.Enabled = Not IsContentValid
 34  
 35  		' included for fun :)
 36  		If IsContentValid Then
 37  			System.Media.SystemSounds.Beep.Play()
 38  		End If
 39  	End Sub
 40  End Class

That will allow the user to switch to another program (to lookup the correct value on the internetz, for example), but it won't allow the user to do anything (like clicking a button in your program or closing it) until the textbox has a valid value.

To achieve what you requested, remove line #29.
I are Troll Suspicious | :suss:

GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 11:15
Andraw Tang17-Aug-10 11:15 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Eddy Vluggen17-Aug-10 11:47
professionalEddy Vluggen17-Aug-10 11:47 
AnswerRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
nikhilhegde00717-Aug-10 21:40
nikhilhegde00717-Aug-10 21:40 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang18-Aug-10 4:27
Andraw Tang18-Aug-10 4:27 
QuestionFile.Delete Method Pin
98fireblade17-Aug-10 5:08
98fireblade17-Aug-10 5:08 
AnswerRe: File.Delete Method Pin
David Mujica17-Aug-10 5:22
David Mujica17-Aug-10 5:22 
GeneralRe: File.Delete Method Pin
98fireblade17-Aug-10 5:29
98fireblade17-Aug-10 5:29 
AnswerRe: File.Delete Method Pin
dan!sh 17-Aug-10 5:39
professional dan!sh 17-Aug-10 5:39 
GeneralRe: File.Delete Method Pin
98fireblade17-Aug-10 5:42
98fireblade17-Aug-10 5:42 
AnswerRe: File.Delete Method Pin
Luc Pattyn17-Aug-10 5:52
sitebuilderLuc Pattyn17-Aug-10 5:52 
GeneralRe: File.Delete Method Pin
98fireblade17-Aug-10 6:34
98fireblade17-Aug-10 6:34 
GeneralRe: File.Delete Method Pin
Eddy Vluggen17-Aug-10 8:55
professionalEddy Vluggen17-Aug-10 8:55 
AnswerRe: File.Delete Method Pin
Dave Kreskowiak17-Aug-10 6:22
mveDave Kreskowiak17-Aug-10 6:22 
GeneralRe: File.Delete Method Pin
98fireblade17-Aug-10 7:11
98fireblade17-Aug-10 7:11 
GeneralRe: File.Delete Method Pin
98fireblade17-Aug-10 8:30
98fireblade17-Aug-10 8:30 
GeneralRe: File.Delete Method Pin
ChandraRam18-Aug-10 2:48
ChandraRam18-Aug-10 2:48 
QuestionMathmL control for math operations [modified] Pin
selpra17-Aug-10 1:40
selpra17-Aug-10 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.