Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
 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
Updated 30-Aug-12 22:51pm
v4
Comments
[no name] 31-Aug-12 4:21am    
Post your code first for both
Keypress and TextChanged events
Kenneth Haugland 31-Aug-12 4:44am    
I assume that this applies to WinForms or?
choudhary.sumit 31-Aug-12 4:46am    
yes i m working on winforms
Sunny_Kumar_ 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?

hi friend this example enough for you!!!


C#
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
 
Share this answer
 
v2
Comments
choudhary.sumit 31-Aug-12 5:22am    
no effect :(
nothing happening when i hit enter... none of the events are firing
Sarrrva 31-Aug-12 5:25am    
check this code in separate application then implement in your Project. It should Work
Sarrrva 31-Aug-12 5:37am    
See my Improved Code
regards
sarva
C#
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;
}
 
Share this answer
 
v2
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
 
Share this answer
 
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900