How to enable ctrl + a with TextBox in winform?

I ask the already asked question (and even answered): Why some text fields do not accept Control + shortcut to select all by default?

But this answer does not work for me. I have this code:

public class LoginForm : Form { private TextBox tbUsername; public LoginForm() { tbUsername = new TextBox(); tbUsername.ShortcutsEnabled = true; tbUsername.Multiline = false; Controls.Add(tbUsername); } } 

A text box will appear, I can write on it, I can cut, copy and paste the text on it without any problems. But when I try to press Ctrl + A , I only hear "bling", similar to bling, which you hear if you are trying to erase text from an empty text field (try using the address bar of your browser).

+42
c # winforms keyboard-shortcuts textbox
Apr 24 '13 at 16:59
source share
8 answers

Like other answers, you should call Application.EnableVisualStyles() . Also, the value of TextBox.ShortcutsEnabled should be set to true . But , if your TextBox.Multiline turned on, then Ctrl + A will not work ( see the MSDN Documentation ). Using a RichTextBox instead will be a problem.

+55
Apr 30 '15 at 1:09
source share

Just create a keydown event for this TextBox and include this code:

 private void tbUsername_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) { if (sender != null) ((TextBox)sender).SelectAll(); } } 
+26
Jan 01 '15 at 7:41
source share

You can always redefine process command keys to get the desired result.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { const int WM_KEYDOWN = 0x100; var keyCode = (Keys) (msg.WParam.ToInt32() & Convert.ToInt32(Keys.KeyCode)); if ((msg.Msg == WM_KEYDOWN && keyCode == Keys.A) && (ModifierKeys == Keys.Control) && tbUsername.Focused) { tbUsername.SelectAll(); return true; } return base.ProcessCmdKey(ref msg, keyData); } 
+24
Apr 25 '13 at 12:30
source share

This happened to me once, I assume that you deleted the call to Application.EnableVisualStyles(); from your program? Add it back to the Main() function, and everything should work fine.

+3
Apr 25 '13 at 18:35
source share

The quick answer is that if you use a multi-line true, you must explicitly call select all.

 private void tbUsername_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.A && e.Control) { tbUsername.SelectAll(); } } 
+3
Aug 18 '16 at 21:50
source share

The text box has a SelectAll() method and works fine for me. (.net 4.5)

+1
Dec 08 '14 at 20:40
source share

No need to handle WM_KEYDOWN! I know that most of the examples here (and CodeProject and many others) all say that there is, but it does not cure the audio signal that occurs whenever a WM_CHAR that is not processed occurs.

Instead, try the following:

 LRESULT CALLBACK Edit_Prc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ if(msg==WM_CHAR&&wParam==1){SendMessage(hwnd,EM_SETSEL,0,-1); return 1;} else return CallWindowProc((void*)WPA,hwnd,msg,wParam,lParam); } 

Remember to subclass the EDIT control to this Edit_Prc () using WPA = SetWindowLong (...), where WPA is the address of the window procedure for CallWindowProc (...)

I understood this from an experiment, finding that all the answers I found on the Internet insisted on processing WM_KEYDOWN using GetKeyState (), and ended up with a great code that could not stop this annoying beep!

While this answer does not apply to dotnet, in such cases it is usually better to cut into the pursuit and solve it, and not be tormented by which version of the large code wrapping system may or may not do this for you, especially if you want to avoid the risk of dealing with inline behavior.

0
Aug 18 '14 at 3:38
source share

Drop my two cents. Calling this when you press a key is another option.

 private void TxtBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\x1') { TxtBox.SelectAll(); e.Handled = true; } } 
0
Sep 07 '16 at 7:27
source share



All Articles