Solved

Simple Textbox question

Posted on 2004-10-13
19
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 2
  • +2
19 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 12295759
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
ID: 12295895
>>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
ID: 12295911
Then it become very difficult. Because I am talking about the millions. You have to see what your are typing.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 10

Expert Comment

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

Author Comment

by:blacklord
ID: 12295979
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
ID: 12296028
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
ID: 12296179
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
ID: 12296188
I also tyried the validate event. Didnt work like I want. Thanks...
0
 
LVL 10

Expert Comment

by:anv
ID: 12296224
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
 

Author Comment

by:blacklord
ID: 12296344
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
ID: 12296469
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
ID: 12296543
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
ID: 12296617
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
ID: 12296731
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
ID: 12298683
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
ID: 12305477
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 28

Expert Comment

by:Ark
ID: 12343462
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
ID: 12345542
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 28

Accepted Solution

by:
Ark earned 150 total points
ID: 12444677
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

624 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