Setting focus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BK

    #1

    Setting focus

    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.txtInputValu e.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.txtInputValu e.SelectedText = ""
    Me.txtInputValu e.Focus

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

  • Ken Tucker [MVP]

    #2
    RE: Setting focus

    Hi,

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

    Me.txtInputValu e.Focus
    Me.txtInputValu e.SelectedText = ""
    or
    Me.txtInputValu e.SelectionStar t= txtInputValue.T ext.Length

    Ken
    -----------------

    "BK" wrote:
    [color=blue]
    > 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.txtInputValu e.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.txtInputValu e.SelectedText = ""
    > Me.txtInputValu e.Focus
    >
    > The textbox always has the text selected. Any thoughts on what I'm
    > doing wrong?
    >
    >[/color]

    Comment

    • BK

      #3
      Re: Setting focus

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

      Me.txtInputValu e.SelectionStar t= txtInputValue.T ext.Length

      did the trick though. Many thanks.

      Comment

      • Cerebrus

        #4
        Re: Setting focus

        BK wrote :
        [color=blue][color=green]
        >> However, if the user clicks on a keypad number then tries to type
        >> additional numbers manually, I have a problem.[/color][/color]

        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.

        Comment

        Working...