Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Leaving a text box

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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