Set button as focus vb.net?

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Hello everyone, im trying to add a few features to a program I am working on, and I think I need some help. I have a textbox (call it textbox1) where the user enters text, and then clicks a button to run the script. What I am trying to do, is once the user clicks in the text box to type the info, I want the button (call it Button1) to be highlighted so they can just hit enter after they input the information. How would I go about this in VB.net?

TL;DR
Have a textbox and button in VB.net. When the user clicks into the textbox I want the button to become highlighted so the user only has to press enter to activate it.

Thank you,
Sebastian

EDIT - Here is the final code in case someone else needs this info later on, nettb is the textbox, netuserbut is the button I am using.

Code:
    Private Sub nettb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles nettb.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            netuserbut.PerformClick()
        End If
    End Sub
 
Last edited:

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Set the .Default property to be = true...

I assume you are using Windows Forms? WPF also has the default property. But it may help to know which UI framework you are using.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Set the .Default property to be = true...

I assume you are using Windows Forms? WPF also has the default property. But it may help to know which UI framework you are using.

Forgive my ignorance, but is that the same as setting the button as the accept button for the form? I have another button that I want to be the default, I only want this particular button highlighted once the user activates the text box.

I am using windows forms, framework 3.5. This is VB 2012.
 

cabri

Diamond Member
Nov 3, 2012
3,616
1
81
Forgive my ignorance, but is that the same as setting the button as the accept button for the form? I have another button that I want to be the default, I only want this particular button highlighted once the user activates the text box.

I am using windows forms, framework 3.5. This is VB 2012.

Only one item can have focus. You can always paint the button a different shade once a entry has been made in the text box (string length > 0)

In the default handler; check to see which field has focus and operate from that.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Does your code have an OnOK function? I assume that gets called when you press enter. If so then you could stick some code in there that says "if my edit box has focus then I dont really want to call OnOK. I actually want to call OnButtonRunMyScript"
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Only one item can have focus. You can always paint the button a different shade once a entry has been made in the text box (string length > 0)

In the default handler; check to see which field has focus and operate from that.

In the default handler, a different button has focus. But I want that button to retain default focus, I only want my textbox button to have focus once text has been entered.

Does your code have an OnOK function? I assume that gets called when you press enter. If so then you could stick some code in there that says "if my edit box has focus then I dont really want to call OnOK. I actually want to call OnButtonRunMyScript"

I do not know about OnOK, how would I implement that? I tried setting the button to focus once the text was changed in the textbox, but every time I type somthing it removes my cursor from the textbox and highlights the button. This is the code I used:

Code:
    Private Sub nettb_TextChanged(sender As Object, e As EventArgs) Handles nettb.TextChanged
        netuserbut.Select()
    End Sub
 

cabri

Diamond Member
Nov 3, 2012
3,616
1
81
Only one button can have focus. that is where the keyboard entries go to. By assigning the focus to the button, you are taking it away from the text box.

you may have a function available labeled OnOK.
The intention of this handler is to perform final "positive" processing from within the form/screen/etc and then revert control to the parent.

There may be a default (hidden) OnOK handler that will be called unless you define your own. When you define your own handler, you can check to see what control has focus and then implement you own logic.

your only other option would be to implement a keyboard handler that replaces the existing one and then figure out what to do with a given character based on where the focus is.

A lot of work that replaces code that already is in place and works.

you just need to understand how the controls work, interact with each other and the ability to change the properties of a control.
 
Last edited:

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Given the behavior you want, seems it would be easiest to check in the keydown event for the text box, and simply invoke whatever the button would be doing anyway if the key pressed is enter.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
your only other option would be to implement a keyboard handler that replaces the existing one and then figure out what to do with a given character based on where the focus is.

This is the route I'd take. Look at the PreviewKeyDown or PreviewKeyPress. It lets you see if the enter key has been pressed and then trigger a button click (or at least call the button click function from this function.) It may also have the ability to remove the item from the queue so the enter doesn't trigger the default button. But I'm not sure if this ability is in WindowsForms. I know you can do that in WPF.
 

cabri

Diamond Member
Nov 3, 2012
3,616
1
81
This is the route I'd take. Look at the PreviewKeyDown or PreviewKeyPress. It lets you see if the enter key has been pressed and then trigger a button click (or at least call the button click function from this function.) It may also have the ability to remove the item from the queue so the enter doesn't trigger the default button. But I'm not sure if this ability is in WindowsForms. I know you can do that in WPF.

The windows API handlers will return a TRUE/FALSE condition depending on if the function has handled the message or does it need further processing.

Like you stated, all his handler will need to do is to:
Detect that the key is the return key and
Check if the designated text box is in focus.

If so, do what is needed and return TRUE.
Otherwise call the internal default and return that result
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Given the behavior you want, seems it would be easiest to check in the keydown event for the text box, and simply invoke whatever the button would be doing anyway if the key pressed is enter.

This is what I would do. The alternative is to have a function that determines if the format of the text meets a certain criteria (I.E don't search until the length of the string is 5).
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Only one button can have focus. that is where the keyboard entries go to. By assigning the focus to the button, you are taking it away from the text box.

you may have a function available labeled OnOK.
The intention of this handler is to perform final "positive" processing from within the form/screen/etc and then revert control to the parent.

There may be a default (hidden) OnOK handler that will be called unless you define your own. When you define your own handler, you can check to see what control has focus and then implement you own logic.

your only other option would be to implement a keyboard handler that replaces the existing one and then figure out what to do with a given character based on where the focus is.

A lot of work that replaces code that already is in place and works.

you just need to understand how the controls work, interact with each other a and the ability to change the properties of a control.

How would I interact/modify the onOK handler? I am pretty inexperienced with VB and its inner workings.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Given the behavior you want, seems it would be easiest to check in the keydown event for the text box, and simply invoke whatever the button would be doing anyway if the key pressed is enter.

This is the route I'd take. Look at the PreviewKeyDown or PreviewKeyPress. It lets you see if the enter key has been pressed and then trigger a button click (or at least call the button click function from this function.) It may also have the ability to remove the item from the queue so the enter doesn't trigger the default button. But I'm not sure if this ability is in WindowsForms. I know you can do that in WPF.

The windows API handlers will return a TRUE/FALSE condition depending on if the function has handled the message or does it need further processing.

Like you stated, all his handler will need to do is to:
Detect that the key is the return key and
Check if the designated text box is in focus.

If so, do what is needed and return TRUE.
Otherwise call the internal default and return that result


Ohh boy, I think we have a winner. I will try to implement this now.

Thanks!
 

cabri

Diamond Member
Nov 3, 2012
3,616
1
81
How would I interact/modify the onOK handler? I am pretty inexperienced with VB and its inner workings.

Your IDE (Integrated Development Environment) should allow you to add existing methods to your code. Use the method(s) described above as a starting point. One should work. Look at the documentation as to what the key codes are and check in your handler for the code. If the keycode is Enter/Return then check the focus.

No match - call the actual default handler of the same name and pass on the return code.

If match, do what ever code that you would have the button press do and the return TRUE.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
BLAMO, after a lot of research and playing with different commands, I managed to get it working perfectly. Here is the final code:

Code:
    Private Sub nettb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles nettb.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            netuserbut.PerformClick()
        End If
    End Sub

Thank you to everyone for the great help!
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
BLAMO, after a lot of research and playing with different commands, I managed to get it working perfectly. Here is the final code:

Code:
    Private Sub nettb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles nettb.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            netuserbut.PerformClick()
        End If
    End Sub

Thank you to everyone for the great help!

I spent at least several seconds trying to figure out what acronym BLAMO represents since you capitalized all the letters.

Also, post 9,000 for me. Soon I'll be OVAR 9000 and then on to the real life of being a Lifer.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
I spent at least several seconds trying to figure out what acronym BLAMO represents since you capitalized all the letters.

Also, post 9,000 for me. Soon I'll be OVAR 9000 and then on to the real life of being a Lifer.

Lol, no acronym, just me expressing excitment :p

Also, congrats, soon your power level will be off the charts.
 

cabri

Diamond Member
Nov 3, 2012
3,616
1
81
BLAMO, after a lot of research and playing with different commands, I managed to get it working perfectly. Here is the final code:

Code:
    Private Sub nettb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles nettb.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            netuserbut.PerformClick()
        End If
    End Sub

Thank you to everyone for the great help!

Before you fully celebrate, create a second text box and see what happens when the enter Key is pressed while that box has focus.

You do not want the code for Text1 to be executed
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Before you fully celebrate, create a second text box and see what happens when the enter Key is pressed while that box has focus.

You do not want the code for Text1 to be executed

Good call, just did this and everything appears to be working fine.