A VB.Net "LostFocus" event's bug ?

blumonde

Well-known member
Joined
Jul 26, 2005
Messages
68
Programming Experience
Beginner
Hi folks,

I don't know if anyone has seen this bug.

If you place a "Messagebox.show" statement for a message in the lostfocus event of a combobox/textbox, the damn message will popup FOREVER when exiting out of it. It won't let you exit the combobox/textbox. An infinite loop!

Please try it and see it for yourself. Please let me know if there a fix for this. I gave up. Spent 2 hours debugging it but no luck.

In case you are curious, below is my code. It is simple. Nothing fancy.

Cheers,

blumonde

PrivateSub txDID_LostFocus(ByVal sender AsObject, ByVal e As System.EventArgs) Handles txDID.LostFocus

If txDID.Text.Trim = "" Then Exit Sub

If Microsoft.VisualBasic.Left(txDID.Text.Trim, 1) = """" Then' Strip out quotes
txDID.Text = txDID.Text.Trim.Substring(1)
EndIf

If Microsoft.VisualBasic.Right(txDID.Text.Trim, 1) = """" Then' Strip out quotes
txDID.Text = txDID.Text.Trim.Substring(0, 5)
EndIf

If Not IsNumeric(txDID.Text.Trim) And txDID.Text <> "" Then
MessageBox.Show("DID should be numeric. Please re-enter", "New DID Info.", MessageBoxButtons.OK, MessageBoxIcon.Information)
ExitSub
EndIf


sStoreDid = Me.txDID.Text.Trim ' Store it so it can be used for the default

EndSub

 
Last edited:
I use lost focus events on two combo boxes and I don't have a problem.

Mine basically throws a messagebox when the value of one combo box equals the value of another, i.e. you can't have the same person as the primary and secondary contact.

Not as complex code as yours;

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] cboSecAccountManager_LostFocus([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] cboSecAccountManager.LostFocus
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] cboSecAccountManager.SelectedValue = cboAccountManager.SelectedValue [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]MessageBox.Show("You cannot have the same primary and secondary Account Manager. Please change accordingly.", "Secondary Account Manager Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
cboSecAccountManager.SelectedIndex = -1
 
cboSecAccountManager.Focus()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]

[size=2][color=#0000ff]
[/color][/size]

..as I say, I have no problems. If I select the same person, an error box pops up, the selectedIndex of that cbo is set to -1 so it's blank, and then the user can select someone else, in which case they can successfully tab to the next field.

It would suggest something in your code is obviously causing the loop.
 
Hi all,

I can't try "Leave" because the LostFocus event occurs before the Leave event. I need things to execute at the right time.

I am reexamining my code. I don't know what is causing the loop.

Thanks for helping me out, guys.

Cheers,

blumonde
 
Gentlemen, I have found the problem.

In the GotFocus even, I have the txDID.SelectAll statement. And that caused the infinite loop in the LostFocus event. So becareful gentlemen. Watch out.

That SelectAll was there to highlight the value in that textbox only. Is it a bug ? Hell, I don't care. I deleted it.

Cheers,

blumonde
 
Back
Top