Good day all

Is it possible to type an exponent in a textbox or a label using VB6? I am currenltly using two textboxes, one for the base and the other for the exponent, by using the tabkey to go from textbox 1 to textbox 2.

Recommended Answers

All 4 Replies

I hope that this is what you are looking for....

Add a command button to your form. Set its tabindex property to 1 more than your textbox where the number to be exponened will be entered, and set its default property to "True".

Function Power2(ByVal exponent As Long) As Long
    Static res(0 To 31) As Long
    Dim i As Long
    ' Raise 2 to a power
    ' the exponent must be in the range [0,31] or change the Static Res above.
    ' rule out errors
    If exponent < 0 Or exponent > 31 Then Err.Raise 5
    ' initialize the array at the first call
    If res(0) = 0 Then
        res(0) = 1
        For i = 1 To 30
            res(i) = res(i - 1) * 2
        ' this is a special case
        res(31) = &H80000000
    End If
    ' return the result
    Power2 = res(exponent)
    Text2.Text = Power2
End Function

Private Sub Command1_Click()

'First check to see if an inteher has been added to textbox.
If Text1.Text = "" Then
    MsgBox "Please add a number to compute exponent.", vbOKOnly + vbExclamation, "Add Integer"
    Exit Sub
'Check if data entered is an integer.
ElseIf Not IsNumeric(Text1.Text) Then
    MsgBox "Only numbers accepted as an entry.", vbOKOnly + vbExclamation, "Add Integer"
    Call Power2(Text1.Text)
End If
End Sub

Good day

Thank you. Will try it and let u know if it works for me.


Hi AndreRet I tried it, but did not work for me. i will for the time being stick to my old method. Ta. Nicky

By using the tab key, try the following with the code above -

If KeyAscii = 13 Then
    Call Power2(Text1.Text)
    SendKeys "{tab}"
End If

Is this what you had in mind?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.