Solved

Leaving a text box

Posted on 2011-03-14
5
168 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

10 Experts available now in Live!

Get 1:1 Help Now