Solved

Leaving a text box

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

16 Experts available now in Live!

Get 1:1 Help Now