VB.NET - How to enable cursor n default textbox and make it accept Enter

Hi!

I have a winform with textbox and Accept button.
Application is to be used for scanning barcodes. Now, when I click on "Enter" keyboard button nothing happens, and form waits until I click on Accept button.
I need to do 2 things:

1) Enable Enter keyboard action (same as when user clicks Accept button).
2) When form loads, I want the cursor be positioned automatically in the textbox.

Thanks!
LVL 1
ZaurbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
Hi Zaurb,

1) Set the DialogResult poroperty for the button to 'OK'.
2) Go to View > Tab Order, and ensure the textbox's TabIndex is set to 0.

Regards,

Wayne
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZaurbAuthor Commented:
Hi,

1) I can't find the dialogResult property for the textbox.

2) I used txtBox.Select() in a forms load event. Is it OK or better View->tab Order?
0
BinuthCommented:
try this
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Focus()
    End Sub
 
 
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If AscW(e.KeyChar) = 13 Then
            Button1_Click(Nothing, Nothing)
        End If
    End Sub

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Wayne Taylor (webtubbs)Commented:
1) There is no DialogResult for a Textbox. I double checked, and I did indeed say "button".

2) It's better to set the TabOrder to zero. Using the Select() command only creates an additional unnecessary step.

Wayne
0
ZaurbAuthor Commented:
And what is the event for a TextBox? KeyPress? Enter? Nothing of this both worked.
This code:
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If AscW(e.KeyChar) = 13 Then
            Button1_Click(Nothing, Nothing)
        End If
    End Sub

worked, but I thought its better to use DialogResult if it works.
And also, under the TextBox properties in forms designer I selected AcceptsReturn to true, but it changes nothing. What is this property for?
0
Wayne Taylor (webtubbs)Commented:
Zaurb

In addition to setting the DialogResult for the *button*, set the 'AcceptButton" property of your form to your accept button, and use ShowDialog() instead of Show() when displaying the form.

Wayne
0
Wayne Taylor (webtubbs)Commented:
>>I selected AcceptsReturn to true, but it changes nothing. What is this property for?

This lets the Textbox know to add a newline character when the textboxes Multiline property is set to true.

Wayne
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree with Wayne.  (POINTS TO HIM)

For the TEXTBOX, set the TabIndex() Property to 0 (zero).
For the BUTTON, set the DialogResult() Property to OK.
For the FORM, set the AcceptButton() Property to your button.

Now when Enter is pressed, the Button will be clicked, the DialogResult will be set to OK, and the Form will close itself automatically.

NO CODE IS NECESSARY AT ALL...IT CAN ALL BE DONE AT DESIGN-TIME.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.