CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Textboxes

  1. #1
    Guest

    Textboxes

    I have problems concerning textboxes. Thanks to anyone who can give me some tips on these:

    1) I have 2 textboxes, Text1 & Text2, for instance. I wrote a code that after inputting something in Text1 and pressing the Enter key, Text1 will be disabled so that no changes can be made to it, and Text2 will have the focus. But when I try using the Tab key, Text2 has the next focus, but Text1 is still enabled. How can I make the 2 keys perform similarly? I tried using SendKeys but it's still the same.

    2) Is there a way of displaying numeric values in textboxes right-aligned?



  2. #2
    Join Date
    Feb 2000
    Location
    garden grove, california
    Posts
    64

    Re: Textboxes

    'hope this helps, put this in the forms declarations
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    'put this in timer1's code
    Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyReturn) <> 0 Then
    Text1.Enabled = False
    Text2.SetFocus
    End If
    End Sub



  3. #3
    Guest

    Re: Textboxes

    Mr. Matrix,

    I don't know if I still lack something, but I copied your code and pasted it in a sample form w/ 2 textboxes, and I still don't achieve my desired result. What I did was Text1 will change its background color (to set a more obvious indication that the code works), and set the focus to Text2 once Enter/Tab is pressed. Enter key works, Tab key sets the focus to Text2 but Text1's background color doesn't change.


  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Textboxes

    Here you go :

    place the following code in Form1 for your TextBoxes - I'm assuming here that they are called Text1 and Text2 by default (you'll need to change this if they're called anything else)


    '
    private Sub Text1_KeyUp(KeyCode as Integer, Shift as Integer)
    '
    ' If Enter is pressed, then disable text1 and move to text2
    '
    If KeyCode = Asc(vbCr) then
    EnableText1 false
    End If
    End Sub
    '
    private Sub Text1_LostFocus()
    '
    ' Catch the tab / click away
    '
    EnableText1 false
    End Sub
    '
    private Sub EnableText1(byval bEnable as Boolean)
    Text1.Enabled = bEnable
    '
    If bEnable then
    on error resume next ' just in case
    Text2.SetFocus
    Err.Clear
    End If
    End Sub




    The 'EnableText1' procedure allows you to disable and re-enable it by passing false/true.

    As for the right aligned textbox for numbers, this is possible, but only at design time - set the Alignment property of the text box to '1 - Right Justify'.


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  5. #5
    Join Date
    Feb 2000
    Location
    garden grove, california
    Posts
    64

    Re: Textboxes

    whoops, set that timer interval to 1. thats the problem right there


  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Textboxes

    That code will catch the 'Return' key for any event on the form and system wide, (eg. editing in notepad) not very useful for checking in Text1.

    Checkout my earlier answer for a better / correct method.



    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured