Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format() doesn't seem to work

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
Format() or String.Format() doesn't seem to format any thing.

Example:
If curString = "12122006"
Format(curString, "M/d/yyyy") would return M/d/yyyy
String.Format(curString, "M/d/yyyy") would return 12122006

Basically Format returns whatever is in quotes for the style and String.Format returns whatever is in the string you were trying to format. WTF?

-I hate Microsoft!
-Forever and always forward.
 
Look at the order of the items in your constructor and try again.
 
You are correct. Format is a hold over from VB6. String.Format is the .Net way to format a string.

Format(StringToFormat, "format to apply")

String.Format("format to apply", StringToFormat)

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks, but correcting String.Format means it now does the same thing as Format. Which is to not format anything and I get "M/d/yyyy" for the output.

-I hate Microsoft!
-Forever and always forward.
 
The formats you are attempting to use are for Date objects, not for strings. This code:

Dim s As String = "12122006"
MsgBox(Format(s, "M/d/yyyy"))

produces "M/d/yyyy"

This code:

Dim s As String = "12/12/2006"
MsgBox(Format(s, "M/d/yyyy"))

produces "M/d/yyyy"

This code:

Dim s As String = "12/12/2006"
MsgBox(Format(CType(s, Date), "M/d/yyyy"))

produces "12/26/2006"

You could try to put the forward slashes in the string, but you will have issues with many dates. For example, is "1262006" supposed to be "1/26/2006" or "12/6/2006"?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Since I can't seem to do this let me start with the end result and you guys tell me what I need or can use. I need to ensure that the data entered into a text box is correct before passing it to a database so I don't get an error. Something like an Input Mask. Any suggestions?

-I hate Microsoft!
-Forever and always forward.
 
What format does the data in your textbox need to be to be valid so it can be passed to the database?
 
How about Regular Expressions? I'm not that good at them myself, but perhaps someone else with a little more experience could provide some insight as to if Regex might be the way to go?
 
I would suggest you use a DateTimePicker control. That way, you will be guaranteed to have a valid date.

Also, I suggest you format your date in ISO format when sending to the database. There are regional and language settings that can cause problems. The ISO format for dates is yyyymmdd. That is... no spaces, no dash, no slash, no nuthin. Today's date in ISO Format looks like, '20060706'. This is especially true if the database you are using is SQL Server. If it's anything else, the same may hold true but you would have to check.

jebenson makes a good point about the 'interpretation' of the date. I can tell you this, without question, that if SQL Server saw a string like this... '12122006' and tried converting it to a date, you would get an error. According to the string, that date would represent day 6, month 20, year 1212. Since month is greater than 12, you get an error. Here's an interesting thread from the SQL Server forum that explains this pretty well.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Sorry, I copied but forgot to paste. [blush]

thread183-1240616

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George, you seem to be in the wrong forum or are you going to do all the forums now? ;-)


Christiaan Baes
Belgium

"My new site" - Me
 
Thanks everyone. I guess there isn't an input mask. As I need to limit length of some fields, have phone numbers, dates, time, Number only, etc. Though format wouldn't have helped for number only.

Since I couldn't find an input mask I went created this code in the KeyPress event for number only though it feels sloppy to me.

Code:
 If e.Shift Then
            e.SuppressKeyPress = True
        End If

        Select Case e.KeyValue
            Case 48 To 57
                '0 to 9
            Case 96 To 105
                '0 to 9 10 Key
            Case 46, 8
                'Delet, Backspace
            Case 110, 190
                '. and . 10 Key for decimal numbers
            Case 13
                SpecialEvent()
            Case Else
                e.SuppressKeyPress = True
        End Select

I guess I could do something in the key down and catch the lenght of the field and force a /, -, :, ect in the correct spot depending on what type I want.....


-I hate Microsoft!
-Forever and always forward.
 
You can limit the length of TextBoxes.. they have a length property in case you didnt see that.

 
What I have so far is:
Code:
Select Case Len(Date_lbl.Text)
                    Case 2
                        Date_lbl.Text = Date_lbl.Text & "/"
                    Case 5
                        Date_lbl.Text = Date_lbl.Text & "/"
                    Case 11 To 20
                        If e.KeyValue = "110" Or e.KeyValue = "190" Then
                            Date_lbl.Text = "00/00/0000"
                        Else
                            e.SuppressKeyPress = True
                        End If
                End Select

This is in the keydown. My problem is that once the length is 2 and it adds the / the cursor is then placed at the front of the text. Is there any way to force it to the end?

I wanted to do something with _/__/____ sort of thing, but all the key events happen before the text is changed....hmm I guess I could try it in the text change event, but I'll still need to be able to position the cursor in the text box.

-I hate Microsoft!
-Forever and always forward.
 
No, I didn't see the lenght. I was so into everything else I didn't even look. I just assumed. Thanks.

-I hate Microsoft!
-Forever and always forward.
 
I didn't even think of looking at com objects. DOH! Thanks.

-I hate Microsoft!
-Forever and always forward.
 
you're absolutely right that keypress is the worst way of doing it.

what if the user pastes text in?
what if they use drag and drop?

you're much better off accepting they're going to type rubbish into the field and only validating it in the, wait for it, Validating event.

Code:
protected void someControl_Validating(object sender, CancelEventArgs e)
{
  int retVal;
  if ( !int.TryParse(((TextBox)sender).Text, retVal) )
  {
    e.Cancel = true;
  }
}

mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top