Simple Textbox question

Is it possible to use the Maskedbox change event for formatting the data while it is typing?  For example If I use the code below I cannot enter this value into the textbox with or without sendkey, 1.450,45 because comma and dot is not working for the decimal value. On the other hand, If I use maskedbox it is not formatting the values while it is typing. And it become very difficult when you are working with large numbers.

Private Sub Text38_Change(Index As Integer)
Text38(4).Text = Format(Text38(4).Text, "###,###.00")
SendKeys "{End}"
End Sub

vb6.0
blacklordAsked:
Who is Participating?
 
ArkCommented:
Oh, seems I understood now. You need Key_Down event also:
Dim bFromCode As Boolean
Dim bDecimal As Boolean

Private Sub Form_Load()
   Text1 = ""
End Sub

Private Sub Text1_Change()
   If bFromCode Then Exit Sub
   bFromCode = True
   Dim sFormat As String
   If Not bDecimal Then
      Text1.Text = Format(Text1.Text, "###,##0")
   Else
      If InStr(1, Text1.Text, ".") = Len(Text1.Text) Then
         Text1.Text = Format(Text1.Text, "###,##0.")
      ElseIf InStr(1, Text1.Text, ".") + 1 = Len(Text1.Text) Then
         Text1.Text = Format(Text1.Text, "###,##0.0")
      Else
         Text1.Text = Format(Text1.Text, "###,##0.00")
      End If
   End If
   If Text1.SelStart = 0 Then Text1.SelStart = Len(Text1)
   bFromCode = False
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   Select Case KeyCode
      Case vbKey0 To vbKey9  'Numeric input
      Case vbKeyBack, vbKeyDelete
      Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyHome, vbKeyEnd
      Case vbKeyDecimal, 190 'Decimal pressed - switch 'decimal' mode on
           bDecimal = True
      Case vbKeyReturn       'Enter pressed   - switch 'decimal' mode off and clear textbox
           bDecimal = False  'You can use ant other key or do thi in a button press event
           Text1 = ""
      Case Else              'restrict input
           KeyCode = 0: Beep
   End Select
End Sub
0
 
Ryan ChongCommented:
try NOT format your text value in the Change event, try do in in LostFocus or KeyDown event instead.. ?
0
 
anvCommented:
>>maskedbox it is not formatting the values while it is typing. And it become very >difficult when you are working with large numbers

hi

when u r using Masked edit box..

set the Mask property (at Design Time) to ###,###.00

now user will type only in the desired format...

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
blacklordAuthor Commented:
Then it become very difficult. Because I am talking about the millions. You have to see what your are typing.
0
 
anvCommented:
Best would be to add the format text in the validate event of the text box...
0
 
blacklordAuthor Commented:
if I set the mask property to ###,###.00, then the user should enter only 6 digit values. not more  or less. Like 12.345.678,45 or 3.456,78
thanks...
0
 
anvCommented:
hi blacklord

i'm not getting what are u talking about??

Please explain...

as in my first post i gave u th e solution for masking using maskediit box..

after ur comment i asked u to add the text box shift evnet code to validate event

now u 've posted for the maskedit box... what are u trying to convey?? please explain..
0
 
blacklordAuthor Commented:
If I use maskedbox I can only see the formatted values after typing (Lostfocus event).  I have to see the proper format while the user types.
I can see it if I am using textbox. But when I use the text box I cant enter the values like 2 decimal values like currency. I am using the code below to format the text box while its typing.

Private Sub Text38_Change(Index As Integer)
Text38(4).Text = Format(Text38(4).Text, "###,###.00")
SendKeys "{End}"
End Sub


Now as a result;
In maskedbox: I can format and enter what I want but except  in 'change' event.
In Text Box: I can't enter currency format If I use the code, but I can use the text1_change event.

Hope this helps...
0
 
blacklordAuthor Commented:
I also tyried the validate event. Didnt work like I want. Thanks...
0
 
anvCommented:
hi blacklord

i feel u r missing something here..

the thing is when u use maskedbox...the actual format is being displayed when user types in the values...
u dont have to use any event like change etc... for changeing the format...

just add the format to the mask property of the masked edit box...at design time..

now when u run the project ..

u would see..(if u have  given the mask as ###,###.00) as
___,___.00

i suppose this is what u want to display to the user..??

i hope i'm not misunderstanding ur question..

 

0
 
blacklordAuthor Commented:
okey thats good but how can the user type the number 12435.678,76 with this mask? I dont want to retrict this input area by mask. The user be able to enter millions or only the cent.
Thanks...
0
 
anvCommented:
hi blacklord..

u can use...9's in place of #'s

9 => is an optional number value

instead of text or formattedText or any other property to get the vlaue entered in the control use 'ClipText' property ..which will give the exact value entered by the user...

0
 
blacklordAuthor Commented:
Thanks any for help... But I cant understand. I am using 9 's in maskedbox but I still cant enter this value 1.000.000,56 and the user should type this 002.556,23 to write 2.556,23
0
 
anvCommented:
hi blacklord

give the maximum limit in the masked edit box...

like...if u want to add 1.000.000,56  or more value use

9.999.999,999,99  ........

its because...by doing this.. masked edit will alllow u to enter less values..but not more than the limit..

0
 
blacklordAuthor Commented:
ok, but think about flexibility and efficiency, masked this field for currency is not very good. Because for example to enter 1.456,34:

mask: 9.999.999.999.999,99
Value:0.000.000.001.546,34

user must enter this kind of value.
0
 
SteinerCommented:
I don't believe you can do this with a masked edit box, as the mask is fix there, it accepts characters only in a specific location (from left to right), which you can't give because you don't know how big the number will get.

Perhaps you'll have to create you own textbox that mimics that behaviour by processing the numbers from left to right while typing. But I believe if you allow the user to edit the number by removing or adding numbers in the right in the middle, might make this very complicated as you'll then have to rewrite the whole number AND remember where the cursor stood.
0
 
blacklordAuthor Commented:
Would there be a different component for that kind of purpose? Because I saw it once in a program.

What I need to do is format the value within the change event and also the user be able to insert comma to for decimal values. I can not make it with the following code.

Private Sub Text38_Change(Index As Integer)
Text38(4).Text = Format(Text38(4).Text, "###,###.00")
SendKeys "{End}"
End Sub
0
 
ArkCommented:
Dim bFromCode As Boolean

Private Sub Form_Load()
   Text1.Alignment = 1
   Text1 = 0
End Sub

Private Sub Text1_Change()
   If bFromCode Then Exit Sub
   bFromCode = True
   Text1.Text = Format(Text1.Text, "###,##0.00")
   Text1.SelStart = 0
   bFromCode = False
End Sub
0
 
blacklordAuthor Commented:
ark thanks for the answer but still it is not working

can anybody tell me how to format that value while I am typing into the textbox?

thanks

1,234,456,657.98
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.