08-24-2012, 04:25 PM
Well, it took me some time to figure this one out, as I was having the same problems as you, but apparently the correct "Format String", is:
{0:0000 0000 0000 0000}
Interesting..
Anyway, I have created a class that will do the formatting thing for you! It's pretty easy to understand!
[code2=vbnet]Public Class CardFormatter
Private cardDictionary As Dictionary(Of String, String)
Public Sub New()
'Now, we need to create the dictionary
cardDictionary = New Dictionary(Of String, String)
'Add the contents
cardDictionary.Add("American Express", "{0:0000 000000 00000}")
cardDictionary.Add("Visa", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Mastercard", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Discover", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Isracard", "{0:0000 0000}")
cardDictionary.Add("Diners Club", "{0:0000 000000 0000}")
End Sub
Public Function GetCardNames() As List(Of String)
Return New List(Of String)(cardDictionary.Keys)
End Function
Public Function GetFormattedString(ByVal cardName As String, ByVal cardNumber As String) As String
Dim formatString As String = cardDictionary.Item(cardName)
Return String.Format(formatString, Long.Parse(cardNumber))
End Function
End Class[/code2]
First, put something like this at the top of your form class code:
[code2=vbnet]Private CF As New CardFormatter[/code2]
Now all you need to do is add a combobox and textbox to your form, and when the form loads:
[code2=vbnet]ComboBox1.Items.AddRange(CF.GetCardNames.ToArray)[/code2]
Then, when the user submits the text box:
[code2=vbnet]MsgBox("Credit Card Number: " & CF.GetFormattedString(ComboBox1.Text, TextBox1.Text))[/code2]
{0:0000 0000 0000 0000}
Interesting..
Anyway, I have created a class that will do the formatting thing for you! It's pretty easy to understand!
[code2=vbnet]Public Class CardFormatter
Private cardDictionary As Dictionary(Of String, String)
Public Sub New()
'Now, we need to create the dictionary
cardDictionary = New Dictionary(Of String, String)
'Add the contents
cardDictionary.Add("American Express", "{0:0000 000000 00000}")
cardDictionary.Add("Visa", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Mastercard", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Discover", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Isracard", "{0:0000 0000}")
cardDictionary.Add("Diners Club", "{0:0000 000000 0000}")
End Sub
Public Function GetCardNames() As List(Of String)
Return New List(Of String)(cardDictionary.Keys)
End Function
Public Function GetFormattedString(ByVal cardName As String, ByVal cardNumber As String) As String
Dim formatString As String = cardDictionary.Item(cardName)
Return String.Format(formatString, Long.Parse(cardNumber))
End Function
End Class[/code2]
First, put something like this at the top of your form class code:
[code2=vbnet]Private CF As New CardFormatter[/code2]
Now all you need to do is add a combobox and textbox to your form, and when the form loads:
[code2=vbnet]ComboBox1.Items.AddRange(CF.GetCardNames.ToArray)[/code2]
Then, when the user submits the text box:
[code2=vbnet]MsgBox("Credit Card Number: " & CF.GetFormattedString(ComboBox1.Text, TextBox1.Text))[/code2]