Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pressing Enter Key

Status
Not open for further replies.

rskiko

Programmer
Mar 17, 2004
10
TN
Hi,
I have a form that contains 2 textBoxs one for the user name and the other for password. what i want is to execute the code of the button Ok when I press Enter Key and the cursor is inside one of the textBoxs. I used the Keypress Event handler but it doesn't work. Can you find me a solution for that.
Thanks.
 
I'm using this code and works
Code:
private void txtPassword_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
	if(e.KeyValue==13) //action here; 
}

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Simplest approach is make the OK button your forms acceptButton. You can do this programmatically or you can do it at the design time.

1. Open your form in designer
2. Go to Properties window. (Make sure your form has focus)
3. Look for the attribute AcceptButton and select the OK button from the dropdown there.

-Kris
 
Explain the difference between Sequential search and binary search the main advantage and the main disadvantage
 
hodo12,
I'm not sure what the connection is between binary/sequential search and pressing the Enter key but here's what the difference between binary and sequential search is:

Sequential search is the most logical way to search a list of values (usually an array). As its name suggests, you search for a target value analyzing each value in the list, one by one, in a sequence.

Binary search, however, is a bit different. First off, it requires the list of values to be sorted. When searching, the list is split in half and the middle value is compared to the target. If the middle value is the target, the search is over. If not, then half the list of values is "thrown away". That is, the target is compared to the middle value. If the target is bigger, then (since the list is sorted) the first half of values doesn't contain the target and thus is thrown away. Otherwise the opposite is true and the second half is thrown away, and the search continues with half the values. This process is repeated again until the target is found. This type of search is very fast for long lists, because in for every pass of the loop, half the list is values is eliminated.

Binary search is usually better than sequential search. The only drawback of binary search that I know of are:

1 - You must sort the list before performing the search.
2 - Searching for a value that is at the second position in a long list (let's say of a million values), will require many loop passes, whereas with sequential search, the value would be found at the second loop pass. In general, values that are close to the beginning of the list will take longer loop passes in binary search than in sequential search.

Sequential search will only be a good candidate when the lists are short. Searching for a value that is at the second-to-last position of a million value list will take a substantial number of loop passes.

Hope this helps!

JC


Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
I use the ProcessCmdKey event because I've problems with keypress and keydown.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch(keyData)
{
case Keys.Enter:
if(this._TabOnEnter)
return base.ProcessCmdKey(ref msg, Keys.Tab);
return true;
.
.
.
default:
return base.ProcessCmdKey (ref msg, keyData);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top