• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Leaving a text box

I want my code to do something when the cursor leaves a text box by pressing TAB. However, if a user clicks on a form button, I do not want to execute the same code. How do I determine how the user left the text box?
0
mpdillon
Asked:
mpdillon
  • 2
  • 2
2 Solutions
 
enkorCommented:
Try to use the event below when textbox.Blur() occures:
(here is example with some key codes)



<script type="text/javascript">
document.onkeyup = KeyCheck;      

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 16:
      document.Form1.KeyName.value = "Shift";
      break;       case 17:
      document.Form1.KeyName.value = "Ctrl";
      break;      case 18:
      document.Form1.KeyName.value = "Alt";
      break;      case 19:
      document.Form1.KeyName.value = "Pause";
      break;      case 37:
      document.Form1.KeyName.value = "Arrow Left";
      break;      case 38:
      document.Form1.KeyName.value = "Arrow Up";
      break;      case 39:
      document.Form1.KeyName.value = "Arrow Right";
      break;      case 40:
      document.Form1.KeyName.value = "Arrow Down";
      break;   }
}</script>
<script type="text/javascript">
document.onkeyup = KeyCheck;       

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 16:
      document.Form1.KeyName.value = "Shift";
      break;       case 17:
      document.Form1.KeyName.value = "Ctrl";
      break;      case 18:
      document.Form1.KeyName.value = "Alt";
      break;      case 19:
      document.Form1.KeyName.value = "Pause";
      break;      case 37:
      document.Form1.KeyName.value = "Arrow Left";
      break;      case 38:
      document.Form1.KeyName.value = "Arrow Up";
      break;      case 39:
      document.Form1.KeyName.value = "Arrow Right";
      break;      case 40:
      document.Form1.KeyName.value = "Arrow Down";
      break;   }
}</script>

Open in new window

0
 
mpdillonAuthor Commented:
enkor,
I made a Monday morning mistake. I am using Visual Basic 2008. THis is s Windows Forms project not a web project. Sorry.
But I do work on the occasional web project. I will save your code. thanks,
0
 
enkorCommented:
Oh, OK :)
So this could be helpful too, I hope. Just try to call the method below:


using System;
using System.Drawing;
using System.Windows.Forms;
  
class ExitOnX: Form
{
     public static void Main()
     {
          Application.Run(new ExitOnX());
     }
     public ExitOnX()
     {
          Text = "Exit on X";
     }
     protected override void OnKeyDown(KeyEventArgs kea)
     {
          if (kea.KeyCode == Keys.X)
               Close();
     }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can do something like this:
Public Class Form1

    Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
        If Me.ActiveControl Is TextBox2 Then
            ' ... put code in here ...
            Debug.Print("Tabbed from TextBox2")
        End If
        Return MyBase.ProcessTabKey(forward)
    End Function

End Class

Open in new window

0
 
mpdillonAuthor Commented:
Thank you both,
Enkor, your solution is a little over my head. But I will keep looking at it to determine how to implement it.
Idle_mind. Yours will work also.
While you were responding I found the PreviewKeyDown event. This allows me to test for the TAB key (e.keyCode = 9) .
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now