473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

TextBox.SelectAll() doesn't work - sometimes

Using VS2008, Windows Forms Application

In a TextBox::KeyDown event I check to see if the Enter Key has been pressed, and if so, I perform some action then attempt to select all of the text using SelectAll(), as follows:
Expand|Select|Wrap|Line Numbers
  1. private void SelectAllTextBox_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.     if (Keys.Enter == e.KeyCode)
  4.     {
  5.     PerformSomeAction();
  6.         SelectAllTextBox.SelectAll();
  7.     }
  8. }    
  9.  
It works as expected if I manually enter text into the TextBox then press enter, but if I use a barcode reader to enter the text (with a <CR>), the PerformSomeAction() method executes but the SelectAll() method appears to not select the text. I'm guessing the difference is the timing between characters.
To get passed the issue I changed to code to use a Timer as follows:
Expand|Select|Wrap|Line Numbers
  1. private void SelectAllTextBox_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.     if (Keys.Enter == e.KeyCode)
  4.     {
  5.     PerformSomeAction();
  6.         HackTimer.Start();
  7.     }
  8. }
  9. private void HackTimer_Tick(object sender, EventArgs e)
  10. {
  11.     HackTimer.Stop();
  12.     SelectAllTextBox.SelectAll();
  13. }
  14.  
This works if the Timer Interval is set to a value greater than 10ms.
Note: the PerformSomeAction() method is irrelevant.

Can someone explain the timing involved and why, without the Timer, it doesn't work?
Oct 19 '18 #1
2 2051
Luk3r
300 256MB
I believe this is a case of needing to suppress the event because you're not actually wanting the ENTER key to do work, simply trigger work. Add the following code after your ".SelectAll()" code.

Expand|Select|Wrap|Line Numbers
  1. e.SuppressKeyPess  = true;
Oct 22 '18 #2
Thanks for the reply Luk3r. Unfortunately adding e.SuppressKeyPress = true; doesn't make a difference. I actually have that in the code already to suppress the beep that occurs on a non-multiline TextBox. What's odd about my problem is that if I use the keyboard to enter text into the TextBox, then press the Enter key, then it works - all of the text gets selected. But if I use a barcode reader to enter text into the TextBox (with an automatic Cr), then it doesn't work - none of the text get selected. This seems to be a timing issue since the barcode reader is entering text far quicker than I can possibly type. Thanks again.
Oct 22 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
11
by: C. Sengstock | last post by:
Hi, i want to save the keywords of an ini file in a struct, together with a fpos_t. I think i´m right with the concept, but the access through fsetpos() doesn´t work. The position is always wrong...
3
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am...
9
by: nichas | last post by:
I tried to use #include<iostream> in visual C++ compiler but then it didnt work but i see this is the way it is being mentioned in C++ primer by lippman and lajoie.. where is the problem.. Do...
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
1
Digital Don
by: Digital Don | last post by:
I am writing a program for Peg solitaire... To check for no repetition of previous states I use a Set for storage of Board states.. The pronblem is when I declare the set as type char i.e. set...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for...
20
by: Hush | last post by:
Hi, The following code works fine in IE7 but FF returns with an error: Access denied to achieve the property Element.firstChild. In this line: nodes = xmlDoc.documentElement.childNodes; My...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.