Results 1 to 7 of 7

Thread: Text format

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Text format

    Hi
    I want to change the text format of a text box to numeric as (##,###.00).
    It is very easy to do in the event of LostFocus, GotFocus etc.

    But I want to do this in the event of text change. As I type a number it shoud be formatted properly.

    I coded :
    Code:
    Private Sub TxtAmnt_Change()
    TxtAmnt.Text = Format(Trim(TxtAmnt.Text), "#,###.00")
    End Sub
    But this doesn't work properly.
    Any idea to solve this.

    With regards,
    Nasreen

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Text format

    You should not try to format it until all the numbers are typed. Notice how Excel works on formatted cells.
    Doctor Ed

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Text format

    Agree with Code Doc. There are 2 issues with what you are trying to do:

    1) You are changing the text property each time user enters a keystroke, which could trigger a 2nd Change event reproducing unexpected results

    2) The user will lose their cursor position each time a 2nd change event fires and the cursor will move back to the 1st character which can be very annoying for the user.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Resolved Re: Text format

    Thanks
    Solved almost
    One of my local friend forwarded me an idea.

    Code:
    Private Sub TxtAmnt_Change()
    TxtAmnt.Text = Format(Trim(TxtAmnt.Text), "#,###")
    SendKeys "{end}"
    End Sub
    This is OK.

    Nasreen

  5. #5
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Text format

    This works when only digits are inserted.

    Here's a code that allows only digits being inserted. It also prevents copy/paste (both, via keyboard and/or menu):
    Code:
    Option Explicit
      Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
      Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
      
      Private Const GWL_STYLE = (-16)
      Private Const ES_NUMBER As Long = &H2000&
    
    Private Sub Form_Load()
      Dim lStyle As Long
      
      TxtAmnt.Text = vbNullString
      
      lStyle = GetWindowLong(TxtAmnt.hwnd, GWL_STYLE)
      SetWindowLong TxtAmnt.hwnd, GWL_STYLE, lStyle Or ES_NUMBER
    End Sub
    
    Private Sub TxtAmnt_KeyPress(KeyAscii As Integer)
      Select Case KeyAscii
        Case 3, 22
          MsgBox "Copy/paste disabled!"
          
          KeyAscii = 0
      End Select
    End Sub
    
    Private Sub TxtAmnt_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Select Case Button
        Case vbRightButton
          MsgBox "Copy/paste disabled!"
      End Select
    End Sub

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Text format

    Quote Originally Posted by nasreen View Post
    Thanks
    Solved almost
    One of my local friend forwarded me an idea.

    Code:
    Private Sub TxtAmnt_Change()
    TxtAmnt.Text = Format(Trim(TxtAmnt.Text), "#,###")
    SendKeys "{end}"
    End Sub
    This is OK.

    Nasreen
    This is a very bad advice! Very annoying!
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Text format

    Quote Originally Posted by anhn View Post
    This is a very bad advice! Very annoying!
    I have to agree with AnHn. Wait a minute, I really don't have to. However, I still do.
    Doctor Ed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width