Solved

Leaving a text box

Posted on 2011-03-14
5
166 Views
Last Modified: 2012-06-27
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
Comment
Question by:mpdillon
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:enkor
ID: 35128860
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
 

Author Comment

by:mpdillon
ID: 35128886
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
 
LVL 2

Accepted Solution

by:
enkor earned 250 total points
ID: 35128928
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 35129278
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
 

Author Closing Comment

by:mpdillon
ID: 35129547
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now