Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UserForm_KeyPress

Status
Not open for further replies.

thatjayguy

Technical User
Jan 27, 2003
4
CA
I am building a simple calculator program to use when working with feet and inches and I'm having trouble with sending a cooresponding value to the UserForm_KeyPress event.

This is what I have so far:

Private Sub cmd0_Click()

UserForm_KeyPress (48)'this is where it is failing

End Sub

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
txtEntry.Text = txtEntry.Text & Chr(KeyAscii)
End If

If KeyAscii = vbKeyBack Then
cmdDel_Click
End If

End Sub

What do I need to change to correctly call the UserForm_KeyPress event?

Thanks in advance for you assistance.
 
I don't think it is appropriate to call an event handler. The idea is that when an event occurs, the appropriate action should be taken.

There may be an easier way, but here is what I think is necessary: For each button on the form, code an event handler something like this:
Code:
Private Sub CommandButton1_Click()
  TextBox1.Text = TextBox1.Text + &quot;1&quot;
End Sub
Private Sub CommandButton2_Click()
 TextBox1.Text = TextBox1.Text + &quot;2&quot;
End Sub
etc. for all 10 number buttons.

To allow user to key the numbers, you need to have an event handler for every object that can receive focus:
Code:
Private Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  VetKeystroke (KeyAscii)
End Sub
Private Sub CommandButton2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  VetKeystroke (KeyAscii)
End Sub
Then in a separate module, have a public function to process the key-strokes (add approprate code for plus, minus, equals, etc.)
Code:
Sub VetKeystroke(KeyAscii As Integer)
  With UserForm1.TextBox1
    If KeyAscii >= 48 And KeyAscii <= 57 Then
      .Text = .Text + Chr(KeyAscii)
    End If
Code:
    ' Add code here to process other keys
Code:
  End With
End Sub
You have selected a very ambitious project. You will learn a lot!


 
Here is an idea which will simplify the programming end. It's not fancy but it seems to work.

Create UserForm1

Add three TextBoxes, four OptionButtons and a CommandButton with the caption &quot;CRUNCH&quot;.

TextBox1 and TextBox2 accept values in the format &quot;feet [space] inches&quot;:

123 6
0 11.25

Which is 123 feet 6 inches and
0 feet 11 and a quarter inches.

The four OptionButtons get captions:

PLUS
MINUS
DIVIDED BY
and
TIMES

The last textbox is for the answer.

Here is the code:
Code:
Private Sub CommandButton1_Click()


L = Len(TextBox1)

For X = 1 To L
  If Mid(TextBox1, X, 1) = Chr(32) Then
    D = X
  End If
Next X

For X = D - 1 To 1 Step -1
  V = V + Val(Mid(TextBox1, X, 1)) * 10 ^ (D - X - 1) * 12
Next X

R = L - D

W = Val(Mid(TextBox1, D + 1, R))

T = V + W



L = Len(TextBox2)

For X = 1 To L
  If Mid(TextBox2, X, 1) = Chr(32) Then
    D = X
  End If
Next X

For X = D - 1 To 1 Step -1
  V2 = V2 + Val(Mid(TextBox2, X, 1)) * 10 ^ (D - X - 1) * 12
Next X

R = L - D

W = Val(Mid(TextBox2, D + 1, R))

T2 = V2 + W



If OptionButton1 = True Then ANSWER = T + T2
If OptionButton2 = True Then ANSWER = T - T2
If OptionButton3 = True Then ANSWER = T / T2
If OptionButton4 = True Then ANSWER = T * T2



If OptionButton1 = True Or OptionButton2 = True Then
I = Int(ANSWER / 12)
J = (ANSWER / 12 - I) * 12
TextBox3 = I & &quot; Feet  &quot; & J & &quot; Inches&quot;
End If

If OptionButton3 = True Then
TextBox3 = ANSWER & &quot; Times&quot;
End If

If OptionButton4 = True Then
  If ANSWER <= 1000 Then
    TextBox3 = ANSWER & &quot; Square Inches&quot;
  Else
    TextBox3 = ANSWER / 144 & &quot; Square Feet&quot;
  End If
End If

End Sub

A word of warning: if you have any more than one space between the feet and the inches in the input textboxes, you will get some interesting non-answers.

You might want to create a macro called 'showform' with UserForm1.Show in it.
 
A little further play testing shows that the above code has a minor flaw in the MINUS routine. Seems to be OK as long as TextBox1 is larger than TextBox2, but not the other way around. Somebody can make troubleshooting that their homework assignment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top