Current location: Hot Scripts Forums » Programming Languages » Visual Basic » Numeric textbox


Numeric textbox

Reply
  #1 (permalink)  
Old 02-16-04, 07:22 AM
R.Amin R.Amin is offline
New Member
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Numeric textbox

Hi there i'm really stuck! I have created two text boxes which store numeric values, e.g 1000! The value of the text of textbox1 must be less than the value of text2 or a error message occurs when i click the save button The user though should be able to press "k", "m" or "b" after a number so to turn it to the corresponding value e.g 1k = 1000! The format must also change so that 1000 = 1,000 0r 1000000 = 1,000,000 etc! I've been able to sort these problems out, although now when i click from one textbox to the other, the number in the textbox i have just left changes. so for example if the user enters 23455 the format then changes to 23,455 when the texbox loses focus which iscorrect, but then if the user clicks back in and then out of the same textbox the value changes to 23! What can i do?? Please help anyone!!

the coding is below!!

Option Explicit

Const billion As Double = 1000000000
Const million As Double = 1000000
Const thousand As Double = 1000

Dim bnumber As Double
Dim mnumber As Double
Dim knumber As Double
Dim bbnumber As Double


Private Sub Command1_Click()

Dim dtext1 As Double
Dim dtext2 As Double

dtext1 = Text1.Text
dtext2 = Text2.Text

If Val(dtext1) > Val(dtext2) Then
MsgBox "Error"
End If
End Sub

Private Sub Text1_LostFocus()

bnumber = Val(Text1.Text)
bnumber = bnumber * billion

mnumber = Val(Text1.Text)
mnumber = mnumber * million

knumber = Val(Text1.Text)
knumber = knumber * thousand

If Right$(Text1.Text, 1) = "b" Then
Text1.Text = Format(Val(bnumber), "#,###,###,###")
Else
If Len(Text1.Text) >= 10 Then
Text1.Text = Format(Val(Text1.Text), "#,###,###,###")
ElseIf Right$(Text1.Text, 1) = "m" Then
Text1.Text = Format(Val(mnumber), "#,###,###")
ElseIf Len(Text1.Text) <= 9 And Len(Text1.Text) >= 7 Then
Text1.Text = Format(Val(Text1.Text), "#,###,###")
ElseIf Right$(Text1.Text, 1) = "k" Then
Text1.Text = Format(Val(knumber), "#,###")
ElseIf Len(Text1.Text) <= 6 And Len(Text1.Text) >= 4 Then
Text1.Text = Format(Val(Text1.Text), "#,###")
End If
End If



End Sub

Private Sub Text2_LostFocus()

bnumber = Val(Text2.Text)
bnumber = bnumber * billion


mnumber = Val(Text2.Text)
mnumber = mnumber * million

knumber = Val(Text2.Text)
knumber = knumber * thousand


If Right$(Text2.Text, 1) = "b" Then
Text2.Text = Format(Val(bnumber), "#,###,###,###")
Else
If Len(Text2.Text) >= 10 Then
Text2.Text = Format(Val(Text2.Text), "#,###,###,###")
ElseIf Right$(Text2.Text, 1) = "m" Then
Text2.Text = Format(Val(mnumber), "#,###,###")
ElseIf Len(Text2.Text) <= 9 And Len(Text2.Text) >= 7 Then
Text2.Text = Format(Val(Text2.Text), "#,###,###")
ElseIf Right$(Text2.Text, 1) = "k" Then
Text2.Text = Format(Val(knumber), "#,###")
ElseIf Len(Text2.Text) <= 6 And Len(Text2.Text) >= 4 Then
Text2.Text = Format(Val(Text2.Text), "#,###")
End If
End If

End Sub
Reply With Quote
  #2 (permalink)  
Old 02-16-04, 04:35 PM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
it sounds like when you go back into the textbox the second time, it is taking 23,455 as a string, so when it converts it back to a number, it is losing everything after the comma, perhaps, when you enter the textbox convert the number back to an int, so that when you leave it again it has a proper number to convert.

hope this helps.
__________________
hyjacked
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
password textbox mpriest HTML/XHTML/XML 14 07-07-04 09:56 AM
Typing special chars without numeric keypad? wilson@cpuworks.com The Lounge 3 01-06-04 05:22 PM


All times are GMT -5. The time now is 01:19 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.