Focus on button when textBox get clicked on or have focus

Hello everybody, new here and new to coding overall.
I have a stupid question.

Im building a simple Windows form in Visual Studio with ironman pro tools.
I have 2 text box and 2 buttons.

When the form load, i can set the focus to button1 with the AcceptButton properties.
But i cant seem to set the focus to button2 when a user click on textBox2 so when he press the Enter key, it send it to button2 and not button1.

Ive tried the following;

$textBox2_Click = {

$button2.Focus()
$button2.Focused
$button2 = $button2.Focus()
$button2 = $button2.Focused
$button2.Select()
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)

}

None of these seem to work, when i click on textBox2, nothing happens.
Will love to get help on this.

Thank you guys

Tool: Visual Studio - Windows Form
Version: Latest

Hi @cheyou,

Thank you for coming here to post about this. I’m not exactly crystal clear on your intentions but I am assuming that you want the button2 to be invoked after the user presses enter on the textbox2. Below is one possible solution that may help. Invoking the Select method on the button ensures it will be on focus when the form is drawn.

Let’s assume that you’ve created a new project called Forum7582

$textbox2_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    if ($_.KeyCode -eq 'Enter')
    {
       $button2.PerformClick()
    }
}
. (Join-Path $PSScriptRoot 'Forum7582.designer.ps1')

$button2.Select()
$Forum7582.ShowDialog()

You would also need for the KeyDown event to be defined (pictured).

image

Extra

This solution presents a new problem where the user will hear the Asterisk beep (!) when they press ENTER in the textbox. Changing the textbox to a multiline will suppress the beep but that may not be ideal.

What ive managed to do without the KeyPress Properties is to put focus on Button2 when TextBox2_TextChanged but as soon as you press 1 thing on textbox2 it goes on Button2. Also i cant just put Button2 when form load since i have button1 that i want to be Default of the form.

oh…ooohhh… i had to go on the Event tab of textbox2 and not properties.
Still… it doesnt work sadly

Hi @cheyou, please note that we are seeking the KeyDown event of the TextBox. If you do not have this event with a System.Windows.Forms.TextBox then we are somehow out of alignment.

Can you please advise if you have the KeyDown event available?

I see that you did find the event. I’m afraid we’re still out of alignment here, though. Can you post your minimal code to reproduce the issue with a clear explanation of what you are trying to achieve? I am fairly certain we can either make progress on this or answer the questions with certainty. If all else fails, we can escalate to @adamdriscoll for hints.

Thank a lot for your time and patience @DataTraveler
Basically i have 1 form with 2 textbox and 2 button.
Everything works fine except for the focus on button2.

Ive managed to put button1 as the AcceptButton for form1, so everytime it launch, the focus is on Button1 and when you type in textbox1 and press Enter, button1 activate.

Now, i need the button2 to get the focus when somebody type in textbox2 and press Enter.
Right now, when you press Enter on textbox2, the button1 activate.

Ive did everything you said, but when i click on textbox2, nothing happen.

Hi @cheyou,

I reviewed this again and I understand now that you only want the button2 to be selected and not actually invoked (clicked, pressed). I can achieve this by making one small change to the code.

Replacing $button2.PerformClick() with $button2.Select() solved the problem. After entering text into textbox2 and pressing Enter, the focus switches to button2.

$textbox2_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    if ($_.KeyCode -eq 'Enter')
    {
       $button2.Select()
    }
}
. (Join-Path $PSScriptRoot 'Forum7582.designer.ps1')

$Forum7582.ShowDialog()

I have (hopefully) provided working code that solves the problem. If this doesn’t work for you, we need to know why because it works for me. Maybe there is something wrong with your system or there is missing information that you have not provided. Please:

  1. Explain why my above code does not work for you
  2. Share your code more clearly here so I (and possibly others) can help to recreate the problem locally.

More

We can’t help if we can’t recreate the problem. Help us to do that. I am sure we can resolve this if we persist :heavy_check_mark:.

1 Like

It works ! Thank you !!! What i did wrong i think was that before you told me to do this, i had a lot of stuff in the Event tab of my textbox, went there removed everything and now it works perfectly, thanks a lot. Im having other issue now but its about powershell specifically.

Thanks !!!

1 Like