Setting focus

B

BK

I've created a small calculator that will pop up on demand within a
form. The calculator is working fine, but I'm having a focus problem.
The calculator looks almost identical to the Windows calc.exe, I wrote
a replacement so that I could pass values in and out as needed.

The calculator has a text box for the numeric values that are either
typed in or entered by clicking on the visual keypad. Clicking on the
visual keypad works perfectly and entering the numbers manually works
fine if I manually set focus to the text box by clicking on it.
However, if the user clicks on a keypad number then tries to type
additional numbers manually, I have a problem.

All operations and all numeric keypad button clicks point to a common
method. In that method, I entered:

Me.txtInputValue.Focus

The problem is that the text is highlighted (selected), so if the user
combines clicks and types for their data entry (Yeah I know, but there
are users that do this sort of thing) they wipe out text. I changed
this to:

Me.txtInputValue.SelectedText = ""
Me.txtInputValue.Focus

The textbox always has the text selected. Any thoughts on what I'm
doing wrong?
 
G

Guest

Hi,

Set the focus to the textbox before you set its selected text.

Me.txtInputValue.Focus
Me.txtInputValue.SelectedText = ""
or
Me.txtInputValue.SelectionStart= txtInputValue.Text.Length

Ken
 
B

BK

Thanks. I tried both suggestions, oddly enough the first one didn't
work. The line:

Me.txtInputValue.SelectionStart= txtInputValue.Text.Length

did the trick though. Many thanks.
 
C

Cerebrus

BK wrote :

Why not set the KeyPreview property of the form to True and intercept
Keystrokes. Then you could simply append the key that was pressed to
the text in the textbox. And use Ken's method to return focus to the
textbox.

HTH,

Regards,

Cerebrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top