Solved

Simple Textbox question

Posted on 2004-10-13
19
277 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:blacklord
  • 9
  • 6
  • 2
  • +2
19 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
try NOT format your text value in the Change event, try do in in LostFocus or KeyDown event instead.. ?
0
 
LVL 10

Expert Comment

by:anv
Comment Utility
>>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
 

Author Comment

by:blacklord
Comment Utility
Then it become very difficult. Because I am talking about the millions. You have to see what your are typing.
0
 
LVL 10

Expert Comment

by:anv
Comment Utility
Best would be to add the format text in the validate event of the text box...
0
 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 10

Expert Comment

by:anv
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
I also tyried the validate event. Didnt work like I want. Thanks...
0
 
LVL 10

Expert Comment

by:anv
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 10

Expert Comment

by:anv
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 10

Expert Comment

by:anv
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 11

Expert Comment

by:Steiner
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
 

Author Comment

by:blacklord
Comment Utility
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
 
LVL 27

Accepted Solution

by:
Ark earned 150 total points
Comment Utility
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now