Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
hello
here is the problem
 
i have a textbox and i want to use key_press event for it only when i press "Enter" key..
but i want to use text_changed event also. problem is that text_changed fire itself before key_press
i mean it takes "Enter " as a text_changed not as a key_press.
how can i resolve this problem
 
 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
 
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.KeyChar = (char)Keys.Tab;
                SendKeys.Send(e.KeyChar.ToString());
            }
        }
 
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            AutoCompleteStringCollection srsa = new AutoCompleteStringCollection();
            foreach (DataRow drsa in ob.dsss2("select accountn from partymaster", "dss").Tables[0].Rows)
            {
                srsa.Add(drsa[0].ToString());
            }
            textBox1.AutoCompleteCustomSource = srsa;
}
 
i want to move the cursor to next control when hit "Enter" but its not moving while if i dont use text_changed then it works fine.
Posted 30-Aug-12 23:17pm
Edited 30-Aug-12 23:51pm
v4
Comments
Sourav Sarkar-SS at 31-Aug-12 4:21am
   
Post your code first for both
Keypress and TextChanged events
Kenneth Haugland at 31-Aug-12 4:44am
   
I assume that this applies to WinForms or?
Indiana.Net at 31-Aug-12 4:46am
   
yes i m working on winforms
Sunnykumar08 at 31-Aug-12 4:46am
   
Elaborate on what you wanna do. Do you want some code to get executed when Enter key is pressed or else?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi friend this example enough for you!!!
 

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("hello");
        }
 
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                textBox1_TextChanged(this.textBox1, EventArgs.Empty);
            }            
            else
            {
                this.textBox1.TextChanged -= new EventHandler(textBox1_TextChanged);            
            }
        }
 

regards
sarva
  Permalink  
v2
Comments
Indiana.Net at 31-Aug-12 5:22am
   
no effect :(
nothing happening when i hit enter... none of the events are firing
Sarrrva at 31-Aug-12 5:25am
   
check this code in separate application then implement in your Project. It should Work
Sarrrva at 31-Aug-12 5:37am
   
See my Improved Code
regards
sarva
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
 
            if (e.KeyChar == 13)
            {
               
                SendKeys.Send("{TAB}");// or textBox2.Focus();
            }
        }
 
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            AutoCompleteStringCollection srsa = new AutoCompleteStringCollection();
            foreach (DataRow drsa in ob.dsss2("select accountn from partymaster", "dss").Tables[0].Rows)
            {
                srsa.Add(drsa[0].ToString());
            }
            textBox1.AutoCompleteCustomSource = srsa;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I wonder why you are implementing such an unusual keypress behavior:
 
Windows users are socialized to, conditioned to, expect certain regular actions in terms of navigation within windows (WinForm windows), and inside standard controls in response to key-events ... with some variations.
 
Normally, the tab key, or arrow keys (or even page up/down and home/end) should be used for navigation.
 
The tab key is specially reserved for moving focus within a WinForm from control to control based on TabIndex level, and whether or not the 'TabStop property of a control is set to 'true, or 'false; and, likewise in dialogs, or input forms, etc.
 
But, even that behavior can vary: for example: within a TextBox, the 'AcceptsTab and 'AcceptsEnter properties affect what happens in response to those keys, while the default behavior, or course, is to insert a cr/lf (linefeed), and move the insertion cursor to the start of the next line (in a MultiLine TextBox, of course).
 
If you open a WinForms property browser at design-time, and set the 'AcceptButton to a button selected from the drop-down list of all Buttons: that means that the 'enter key will trigger a button-click on that selected button: but only if it has focus.
 
And last, but not least, you set a WinForm's 'KeyPreview property to 'true: you can handle key-events first at the Form level. But, even that gets "tricky:" if you a bunch of Buttons and one TextBox on a Form, even if you set the focus to one of the Buttons and hit a key: the object that triggers the Form KeyDown event will be the TextBox, even though it does not have focus.
 
If you really want advice about using the tab key in an unusual way, then I think you need to describe exactly:
 
1. in what type of control you wish to use it: WinForm standard, 3rd. party, custom UserControl ?
 
2. why you wish to use it in this way: what's the real benefit to the end-user, or how does it add value to the control you using this over-ridden key behavior in ?
 
best, Bill
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Finally...
 
key_press does not work with textboxes with autocompelet on.
i used here key_down and it works as i wanted..
 
thanks to all
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 449
1 Maciej Los 430
2 OriginalGriff 320
3 CHill60 240
4 CPallini 210
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100