Results 1 to 11 of 11

Thread: Control Array KeyPress

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318

    Control Array KeyPress

    I did this code to check for the value in a set of textbox control array with the value in another set of textbox control array. But this works only for the Enter key in the keyboard. Can this be working when we use the mouse to move to the next textbox?

    Private Sub txtnewHr_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim TempVal As Long
    TempVal = 0

    If KeyAscii = 13 Then
    If Index + 1 <= txtnewHr.Count Then
    If T > Val(txtnewHr(Index).Text) Then
    txtoldhr(Index).ForeColor = &H80000008
    txtoldhr(Index).Text = Val(txtoldhr(Index).Text) + (T - Val(txtnewHr(Index).Text))
    txtnewHr(Index + 1).SetFocus
    ElseIf T <= Val(txtnewHr(Index).Text) Then
    If Sgn(Val(txtoldhr(Index).Text)) = 1 Or Sgn(Val(txtoldhr(Index).Text)) = 0 Then
    txtoldhr(Index).Text = txtoldhr(Index).Text - (Val(txtnewHr(Index).Text) - T)
    txtoldhr(Index).ForeColor = &H80000008
    If Index + 1 < txtnewHr.Count Then
    txtnewHr(Index + 1).SetFocus
    Else
    cmdAdd.SetFocus
    End If
    End If
    If Sgn(Val(txtoldhr(Index).Text)) = -1 Then
    txtoldhr(Index).ForeColor = &HFF&
    MsgBox "Exceeded Available Hours!", vbExclamation, "Alert!"
    txtnewHr(Index).SetFocus
    SendKeys "{Home}+{End}"
    Exit Sub
    End If
    End If
    End If
    End If
    End Sub

    Please help.

    Thanks

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    For that, you should use the MouseUp event of the textbox, not the keypress.


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    VB Code:
    1. Private Sub Text1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    2. MsgBox Text1(Index).Text
    3. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    I tried all the ways before coz this made my life miserable. Finally, the code above gave me what I wanted.

    I even have code for other actions in the LostFocus and GotFocus events. See the code below.


    Private Sub txtnewHr_LostFocus(Index As Integer)
    Dim I As Integer
    Dim TempVal As Long
    TempVal = 0

    For I = 0 To txtnewHr.Count - 1
    TempVal = TempVal + Val(txtnewHr(I).Text)
    Next I
    txtTotHrs.Text = TempVal

    End Sub

    Private Sub txtnewHr_GotFocus(Index As Integer)
    T = 0
    T = Val(txtnewHr(Index).Text)
    End Sub


    Thanks

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Control Array KeyPress

    let me format that...
    VB Code:
    1. Private Sub txtnewHr_KeyPress(Index As Integer, KeyAscii As Integer)
    2. Dim TempVal As Long
    3. TempVal = 0
    4.  
    5. If KeyAscii = 13 Then
    6.     If Index + 1 <= txtnewHr.Count Then
    7.         If T > Val(txtnewHr(Index).Text) Then
    8.             txtoldhr(Index).ForeColor = &H80000008
    9.             txtoldhr(Index).Text = Val(txtoldhr(Index).Text) + (T - Val(txtnewHr(Index).Text))
    10.             txtnewHr(Index + 1).SetFocus
    11.         ElseIf T <= Val(txtnewHr(Index).Text) Then
    12.             If Sgn(Val(txtoldhr(Index).Text)) = 1 Or Sgn(Val(txtoldhr(Index).Text)) = 0 Then
    13.                 txtoldhr(Index).Text = txtoldhr(Index).Text - (Val(txtnewHr(Index).Text) - T)
    14.                 txtoldhr(Index).ForeColor = &H80000008
    15.                 If Index + 1 < txtnewHr.Count Then
    16.                     txtnewHr(Index + 1).SetFocus
    17.                 Else
    18.                     cmdAdd.SetFocus
    19.                 End If
    20.             End If
    21.             If Sgn(Val(txtoldhr(Index).Text)) = -1 Then
    22.                 txtoldhr(Index).ForeColor = &HFF&
    23.                 MsgBox "Exceeded Available Hours!", vbExclamation, "Alert!"
    24.                 txtnewHr(Index).SetFocus
    25.                 SendKeys "{Home}+{End}"
    26.                 Exit Sub
    27.             End If
    28.         End If
    29.     End If
    30. End If
    31. End Sub

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Thumbs down

    So is this resolved?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    No.

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Having difficulties understanding how the textboxes are processed... one is to one for txtnew and txtold? And what's the data they are handling? A time range?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    Yes, one is to one. The data entered is hours. But the data type is number. So the issue is checking for the exceed in number between the txtnew and txtold.

    Thanks.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    How are the values initialized? What's the range or values for T? Given particular values, whats the required result?

    It's difficult to do an analysis on your code since we don't have data to test against, and we don't know what output is considered correct.

    Better yet also add the zip of the project.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    T is a variable that stores the value that was there in the textbox when it gets the focus, that is T gets the value when the GotFocus event is triggered. The purpose is as follows:

    There are 2 sets of textboxes called the txtnewhr and txtoldhr. There will always be values in the txtoldhr which is read from a table. When the user enters a value in the txtnewhr for the first time, it should always be lesser than the value in the txtoldhr. The next time the user picks that record, the values are shown in the txtnewhr and txtoldhr. Now, when the user wants to modify the value in the txtnewhr, it checks for T and if the value is reduced, the equivalent value is added to the txtoldhr and if the value is increased, the equivalent value is reduced from the txtoldhr. This is how it should work. So once the user enters a value and presses the Enter Key, the above code is executed. I hope it is clear now.
    I will add the zip of the project.
    Attached Files Attached Files

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