CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2006
    Posts
    42

    Textbox Formatting

    Hi - I hope this is a simple one to resolve, but I'm drawing a blank.

    I have a database with a particular field, formatted as TEXT, which when viewed via Access correctly shows contents of, eg, "100.00"

    I have written a VB program to display the contents of the database in various textboxes, but instead of showing the proper contents ("100.00"), the textbox displays "100".

    What am I missing?

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Textbox Formatting

    You could try the VB Format function to set things right.
    Code:
    Format(TextBoxName.Text, "00000.00") 'this will force the box to show the two decimals
    Here's a bit more insight into the workings of the VB Format function
    http://www.codeguru.com/forum/showpo...53&postcount=2

    Hope it helps!

  3. #3
    Join Date
    Jan 2006
    Posts
    42

    Re: Textbox Formatting

    Thanks very much for the reply, HannesTG, but I'm not sure its what I'm looking for.

    Correct me if I'm wrong - using the Format function, I'd have to assign the formatted contents of the textbox to a string, then replace the original textbox contents with the string - all during run-time. I'd have to do this everytime the DB contents changed.

    I'd prefer to format the textbox once (during design time if possible) so that any subsequent changes made to the DB will automatically be shown correctly in the textbox.
    Last edited by Laurel; January 13th, 2006 at 08:01 AM.

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Textbox Formatting

    I think you need the functionality of the masked edit control.
    To add it to the toolbox:
    Select Project menu->Components->check mirosoft masked edit control.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Textbox Formatting

    Quote Originally Posted by Laurel
    Thanks very much for the reply, HannesTG, but I'm not sure its what I'm looking for.
    No problem!

    Quote Originally Posted by Laurel
    Correct me if I'm wrong - using the Format function, I'd have to assign the formatted contents of the textbox to a string, then replace the original textbox contents with the string - all during run-time. I'd have to do this everytime the DB contents changed.
    You are right!

    Quote Originally Posted by Laurel
    I'd prefer to format the textbox once (during design time if possible) so that any subsequent changes made to the DB will automatically be shown correctly in the textbox.
    hspc 's advice is spot on then, but here's more info on the Masked Edit Control
    http://msdn.microsoft.com/library/de...bjMaskEdit.asp

    Have fun!

  6. #6
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Textbox Formatting

    Quote Originally Posted by Laurel
    Correct me if I'm wrong - using the Format function, I'd have to assign the formatted contents of the textbox to a string, then replace the original textbox contents with the string - all during run-time. I'd have to do this everytime the DB contents changed.
    Half right. You don't need a separate string for that.
    Code:
    txtField.Text = Format(txtField.Text, "#####.00")
    That's if you have it in the TextBox before you set the format. If you're getting the value from the database through objects (i.e. with an ADODB Recordset) and not an ADODC control, you can do the Format when you're getting the information to put into the TextBox.
    Code:
    txtField.Text = Format(rsRecord.Fields("FieldName").Value, "#####.00")
    I use #####.00 instead of 00000.00 so the output would only be 100.00 instead of 00100.00.

    And a problem with the Masked Edit Control is, though you can set how it should look, the control doesn't actually keep it in that format. If you had it's Format property set to #####.00, then run the program and type in 100, it'll show as 100.00. Click back on it, and you'll see just 100 again, and trying to get the text from it will also only return 100, not 100.00.

    If you only need it for display (and possibly disallow the user from clicking in the textboxes), the Masked Edit Control could work, but using Format works well too, for one line of code.

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Textbox Formatting

    It seems to me that the database field may be a numeric data type, as the textbox won't reformat strings like the way you are seeing. Either that, or you are assigning the contents of the field to a numeric variable, then to the textbox. Do you have the textbox bound to the field?
    Quote Originally Posted by Laurel
    Correct me if I'm wrong - using the Format function, I'd have to assign the formatted contents of the textbox to a string, then replace the original textbox contents with the string - all during run-time. I'd have to do this everytime the DB contents changed.

    I'd prefer to format the textbox once (during design time if possible) so that any subsequent changes made to the DB will automatically be shown correctly in the textbox.
    If you use the Format$() function in the change event of the textbox, that should keep the format as you want.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Jan 2006
    Posts
    42

    Re: Textbox Formatting

    Wizbang - You are spot on. I originally said that the DB field was Text. I was wrong (idiot!) it is formatted as currency. Your solution works perfectly.

    HannesTG - Your original suggestion was spot on. Sorry to have doubted you. I don't know why I didn't think of just using the TEXTBOX_CHANGE event.

    HSPC & Chaos - Thanks for the suggestions. I've never come across the masked edit control - I'm sure it'll come in handy.

    All in all, thanks all for the suggestions - I've learnt a lot (I'd still like to know why a textbox won't automatically display a field formatted as currency tho' )

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Cool Re: Textbox Formatting

    No problem Laurel!
    As long as you have solved your problem and learnt something!
    Hang around more on CG, and see how much you'll learn!

  10. #10
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Textbox Formatting

    Glad to know you got it working.
    ...I'd still like to know why a textbox won't automatically display a field formatted as currency tho'
    You may also want to look into the FormatCurrency() function. When viewing in Access, the format is shown correctly because the program detects the data type of the field, and operates accordingly. A value of 100.00 is stored within the database as 100 for a numeric data type.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  11. #11
    Join Date
    Jan 2006
    Posts
    42

    Re: Textbox Formatting

    Impressive

    I'll have to come here again!

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

Featured