?
Solved

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

Posted on 2008-11-06
8
Medium Priority
?
726 Views
Last Modified: 2013-11-26
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!
0
Comment
Question by:Zaurb
8 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 22893126
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
 
LVL 1

Author Comment

by:Zaurb
ID: 22893144
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
 
LVL 14

Expert Comment

by:Binuth
ID: 22893147
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22893279
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
 
LVL 1

Author Comment

by:Zaurb
ID: 22893307
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
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22893315
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
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22893324
>>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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22896399
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question