Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyEvent in a Leave Sub

Status
Not open for further replies.

Ragnarox

Programmer
Oct 2, 2003
141
US
Hello all,

Here is the situation.

I have a textbox that takes a number as input. Upon leaving the textbox it uses that number to determine what are allowable entries in other parts of the form. What I need though is to only allow numbers to be entered. The following is the code that I have tried:

Private Sub txtAmount_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAmount.Leave
Dim back As System.Windows.Forms.KeyEventArgs

Select Case back.KeyCode
Case Keys.Shift, Keys.Tab, Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.Home, Keys.End, Keys.A, Keys.B, Keys.C, Keys.D
txtAmount.Text = ""
Return
End Select

The error that I receive on my select case line is :

Object reference not set to an instance of an object.

What am I doing wrong here? Any ideas?

Any help, as always, is greatly appreciated.

Brian
 
You're not in the right event, I suspect. By the lok of it, you are trying to trap various keys when you leave a control. But leaving a control can also be done by a mouse. So Leave doesn't have the right args for you. You need to try KeyDown, KeyPress and KeyUp.

Craig
 
Craig,

I can understand using one of the other events, the problem is though that I will still need to have the leave event firing and it will fire upon leaving no matter how I leave. What I need to do is only allow it to fire if it has numeric values in it. I can do that with a key event by not allowing alpha values to be entered. But what about when it is left using a tab or a shift-tab keycombo or a mouse click? I need to stop it from firing in those instances.

Is there any way to store what keystrokes or mouse clicks when I am in that textbox so that if they cause the person to leave the event that it will not fire?[bigsmile]

Or am I just out of luck in this regard?[hairpull3][curse]

Any help, as always, is greatly appreciated.

Brian
 
Why dont u try this...
this is a textbox that only accepts #s

Private Sub txtvalfrec_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtvalfrec.KeyPress
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
e.Handled = True
End If
End Sub

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top