2

How to SendKeys to the last active input TextBox after clicking a button in winforms (C#)? I'm new to C# and I'm trying to create a winforms application with an on-screen keyboard and I have multiple textboxes.

I have searched and tried the guides I found, here's sample that's working so far.

below code is working when I focused the cursor outside (notepad, etc) my winforms app, but when I click on my created TextBox within the winforms app and then click a button to SendKeys to the TextBox, the cursor is being removed and focus is on the button I clicked, that is making the TextBox unfocused.

const uint WS_EX_NOACTIVATE = 0x08000000;
const uint WS_EX_TOPMOST = 0x00000008;

protected override CreateParams CreateParams
{
    get
    {
       CreateParams param = base.CreateParams;
       param.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOPMOST);
       return param;
    }
}

private void btnA_Click(object sender, EventArgs e)
{
    SendKeys.Send("A");
}

How can I return the cursor focus to the last active TextBox when I click a button to SendKeys to the TextBox?.

Richard
  • 439
  • 1
  • 6
  • 25

2 Answers2

2

Call

_recentTextbox.Select();

before you do your sendkeys. There exists another method that will work similarly ( Focus() ) but that is primarily intended for people creating custom controls

If you have many textboxes and you need to know which one recently lost the focus to your button, attach the same Leave (or LostFocus) event handler to all the textboxes:

private void Leave(object sender, EventArgs e){
  _recentTextbox = (TextBox)sender; //_recentTextbox is a class wide TextBox variable
}

private void btnA_Click(object sender, EventArgs e)
{
   if(_recentTextbox == null)
     return;
   _recentTextbox.Select();
   SendKeys.Send("A");
   _recentTextbox = null; //will be set again when a textbox loses focus
}
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Hi Caius, does this will work on my multiple textbox? – Richard May 15 '19 at 06:46
  • I'm not really sure what you mean by multiple textbox. I would avoid using sendkeys all together and instead modify the .Text property of the textbox, but there isn't a lot of context to your post to know how you're intending things to work – Caius Jard May 15 '19 at 06:48
  • Hi Caius, I'm trying to make my own on-screen keyboard. I have one keyboard field that can be used on my multiple textboxes. – Richard May 15 '19 at 06:49
  • If you mean you have many textboxes and you want to know which one most recently lost the focus, you can attach the same event handler to all of their Leave events, and the `sender` argument will be the box that lost the focus – Caius Jard May 15 '19 at 06:50
  • Have you considered using the windows defaul on screen keyboard – Caius Jard May 15 '19 at 06:50
  • Yes, but they asked me to create my own. Since I only need numpad with other extended buttons. – Richard May 15 '19 at 06:52
  • How can I attach the Leave on my textboxes? – Richard May 15 '19 at 07:10
  • Same as you would wire up a button clcik or any other event: Use the forms designer - go to design view - multi select all the textboxes - go to properties grid, click lightning bolt at the top to switch to events - find Leave event, write a name in the empty cell next to it. you will be transported to the code, with an empty method that is the handler code. It just needs filling in – Caius Jard May 15 '19 at 08:14
  • Hi Caius, I've tried your code and it's now working. – Richard May 15 '19 at 09:00
0

// Optionally go to other field:

SomeOtherField.Focus();

// Add string at insertion point / over selected text:

SendKeys.Send("This is a test...");

The Send() method processes the string you passed immediately; to wait for a response from the program (like auto-populated database records matching input), use SendWait() instead.

  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.focus?redirectedfrom=MSDN&view=netframework-4.8#System_Windows_Forms_Control_Focus says "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms." – Caius Jard May 15 '19 at 06:56
  • Select() method.Its a higher level. Iterates from top in the control's parent hierarchy until it finds a container control. Then it sets that container's ActiveControl property. – Shiva_Kishore May 15 '19 at 07:03
  • is it a reason not to use it? – Caius Jard May 15 '19 at 07:05
  • The Focus() function actually performs the focussing on the control. The Select() function is higher-level, and tells the parent to select through the control's hierarchy.If all you want to do is literally call focus on the element, there is no advantage on using Select... it makes me wonder why Focus isn't kept as a private/protected function. – Shiva_Kishore May 15 '19 at 07:12
  • If you're going to copy paste advice from elsewhere on SO you really should attribute the original author. Passing it off as your own (by omission of reference) is called plagiarism, and is a big no-no here https://stackoverflow.com/questions/4277754/c-sharp-what-is-the-difference-between-control-focus-and-control-select – Caius Jard May 15 '19 at 08:20