Forum guidelines
AuthorQuestion
Enter Keypress and leave the textbox in c#
Posted on: 11 Jan 2011
Hi Friends ,
I am facing with a problem , when i press enter at textbox and then the cursor does not leave the textbox . Instead of it , i must use tab to leave it .

What should i do in this situation ?
Thanks ,



IT - MYF
AuthorReply
Re: Enter Keypress and leave the textbox in c#
Posted on: 11 Jan 2011  
Actually enter/return key will not transfer you rcursor from textbox. When you have some buttons on page that function is normally called by enter key it's not for leaving the focus from control.

Mark as "This Is Correct Answer" if helps you.
Re: Enter Keypress and leave the textbox in c#
Posted on: 11 Jan 2011  
in the keydown event

if(e.KeyCode = Keys.Enter)
   yourTextBox.Focus()   //or any other control that has a focus, like button.Focus()


You could also write a single method that would handle ALL keydown for ALL your controls, you just need to work out how.

theLizard

Minh
  • 0
  • 0
Re: Enter Keypress and leave the textbox in c#
Posted on: 11 Jan 2011  
Any control in application , i see , Cursor is lost focus the control at any tiime the control is keypressed enter , I have tried to use control.focus() to do this but it does not seems to be the good solution .

I think , we can you one event to make that , when press enter , it do as when you  press Tab ?

Thanks for your fast reply .

IT - MYF
Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  
Call ProcessTabKey(true) to get the same behavior as when the tab key is pressed.
Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  

Hi,
Done this in VB6 using "SendKeys" and on looking C# also supporting this. But need to use in a different manner. Below is the way how to call this. Here we programmatically raising a TAB key. Don't forget to set the tab index of text boxes in a sequence order.
 
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter))
{
SendKeys.Send("{TAB}");
}
}


Regards


Jaish Mathews

Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  
What is the advantage of sending a tab using SendKeys instead of using ProcessTabKey?

Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  

Hi,
There's no advantage in my knowledge. But here
SendKeys
.SendWait() is useful when you need some process to be completed and then consider the next tab, like some data validation using background service.
More over for both SendKeys and ProcessTabKey, you need to supress the event raised by the enter event using
e.Handled = true; Otherwise "ding" sound will be generated from background :) So full functionaal code like below

 

private
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
e.Handled = true;
ProcessTabKey(true); // or SendKeys can also use
}
}


Regards


Jaish Mathews

Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  
If my suggestion is not working for you then I suggest you have something wrong with how you are doing things or you are not telling us everything we should know.

If you want to move the cursor to another text box or any other type of text control on your form, then the sample code I gave you does exactly that.

    private void t1_KeyDown(object sender, KeyEventArgs e) //Text Box
      {
      if (e.KeyCode == Keys.Enter)
        {
        t2.Text = sender.GetType().Name;  //put sender type in text box 2
        t2.Focus(); //Text Box
        }
      }
    private void t2_KeyDown(object sender, KeyEventArgs e)  //Text Box
      {
      if (e.KeyCode == Keys.Enter)
        r1.Focus();  //Rich Edit

      }




theLizard

Re: Enter Keypress and leave the textbox in c#
Posted on: 12 Jan 2011  
The advantage of using either ProcessTabKey or sending a tab charcter is that you can set the tab order in the form desginer without a need to modify the code.
Thinking is a feeling; pleasant for some and unpleasant for others.
Re: Enter Keypress and leave the textbox in c#
Posted on: 13 Jan 2011  
Sam, yes that is true but what if?

simple example for generic key down event attached to any control with a keydown event

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int ctrlOrder = -1, next = -1;
        TextBox t = new TextBox();
        ComboBox c = new ComboBox();
        CheckBox ch = new CheckBox();
        public Form1()
        {
            InitializeComponent();
        }

        private void keyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;
            if (sender.GetType().Name == "TextBox")
            {
                t = (TextBox)sender;
                int.TryParse(t.Tag.ToString(), out ctrlOrder);
            }
            if (sender.GetType().Name == "CheckBox")
            {
                ch = (CheckBox)sender;
                int.TryParse(ch.Tag.ToString(), out ctrlOrder);
            }
            if (sender.GetType().Name == "ComboBox")
            {
                c = (ComboBox)sender;
                int.TryParse(c.Tag.ToString(), out ctrlOrder);
            }

            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i].GetType().Name == "TextBox")
                {
                    TextBox te = (TextBox)this.Controls[i];
                    int.TryParse(te.Tag.ToString(), out next);
                    if(next == ctrlOrder+1)
                        this.Controls[i].Focus();
                }
                if (this.Controls[i].GetType().Name == "CheckBox")
                {
                    CheckBox te = (CheckBox)this.Controls[i];
                    int.TryParse(te.Tag.ToString(), out next);
                    if (next == ctrlOrder + 1)
                        this.Controls[i].Focus();
                }
                if (this.Controls[i].GetType().Name == "ComboBox")
                {
                    ComboBox te = (ComboBox)this.Controls[i];
                    int.TryParse(te.Tag.ToString(), out next);
                    if (next == ctrlOrder + 1)
                        this.Controls[i].Focus();
                }
            }
        }
    }
}

this allows you to move to the next control in order of tag property without explicitly setting focus to next control by name.

this of course could be made as a more complex method / function to handle any situation and used on any form in your project, write once use many times...

 

theLizard

Minh
  • 0
  • 0
Re: Enter Keypress and leave the textbox in c#
Posted on: 14 Jan 2011  
Thanks so much , Idea from  Jaish Mathews
is the effect solution .



IT - MYF
Re: Enter Keypress and leave the textbox in c#
Posted on: 14 Jan 2011  
Yet it was my suggestion to use ProcessTabKey(true). I admit that Jaish added a bit of code but I sure hope you are capable of figuring out that part.

Thinking is a feeling; pleasant for some and unpleasant for others.
Minh
  • 0
  • 0
Re: Enter Keypress and leave the textbox in c#
Posted on: 16 Jan 2011  

Thank you all .


IT - MYF
Re: Enter Keypress and leave the textbox in c#
Posted on: 17 Nov 2012  
You answer was really help full for me . i was looking for this kind of solution form last 1 week . dotnetpools.com

v.k.singh

Re: Enter Keypress and leave the textbox in c#
Posted on: 20 Aug 2014  
sir. What to do if i want to visible any lable if i leave textbox empty?
Re: Enter Keypress and leave the textbox in c#
Posted on: 28 Aug 2014  
Great Article sir Thanks

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.